srdf::srdf_parser

Trait RDFNodeParse

Source
pub trait RDFNodeParse<RDF: FocusRDF> {
    type Output;

Show 13 methods // Required method fn parse_impl(&mut self, rdf: &mut RDF) -> PResult<Self::Output>; // Provided methods fn parse(&mut self, node: &IriS, rdf: RDF) -> PResult<Self::Output> { ... } fn by_ref(&mut self) -> ByRef<'_, Self> where Self: Sized { ... } fn flat_map<F, O>(self, f: F) -> FlatMap<Self, F> where Self: Sized, F: FnMut(Self::Output) -> PResult<O> { ... } fn and_then<F, O, E>(self, f: F) -> AndThen<Self, F> where Self: Sized, F: FnMut(Self::Output) -> Result<O, E>, E: Into<RDFParseError> { ... } fn map<F, B>(self, f: F) -> Map<Self, F> where Self: Sized, F: FnMut(Self::Output) -> B { ... } fn and<P2>(self, parser: P2) -> (Self, P2) where Self: Sized, P2: RDFNodeParse<RDF> { ... } fn then<N, F>(self, f: F) -> Then<Self, F> where Self: Sized, F: FnMut(Self::Output) -> N, N: RDFNodeParse<RDF> { ... } fn then_ref<N, F>(self, f: F) -> ThenRef<Self, F> where Self: Sized, F: FnMut(&Self::Output) -> N, N: RDFNodeParse<RDF> { ... } fn then_mut<N, F>(self, f: F) -> ThenMut<Self, F> where Self: Sized, F: FnMut(&mut Self::Output) -> N, N: RDFNodeParse<RDF> { ... } fn or<P2>(self, parser: P2) -> Or<Self, P2> where Self: Sized, P2: RDFNodeParse<RDF, Output = Self::Output> { ... } fn focus(self, node: &RDF::Term) -> SetFocus<RDF> where Self: Sized { ... } fn with<P, A>(self, parser: P) -> With<Self, P> where Self: Sized, P: RDFNodeParse<RDF, Output = A> { ... }
}
Expand description

By implementing the RDFNodeParse trait a type says that it can be used to parse RDF data which have a focus node. RDF data with a focus node have to implement the FocusRDF trait.

Required Associated Types§

Source

type Output

The type which is returned if the parser is successful.

Required Methods§

Source

fn parse_impl(&mut self, rdf: &mut RDF) -> PResult<Self::Output>

Parses the current focus node without modifying the state

Provided Methods§

Source

fn parse(&mut self, node: &IriS, rdf: RDF) -> PResult<Self::Output>

Entry point to the parser. It moves the focus node of rdf to node and runs the parser.

Returns the parsed result if the parser succeeds, or an error otherwise.

Source

fn by_ref(&mut self) -> ByRef<'_, Self>
where Self: Sized,

Source

fn flat_map<F, O>(self, f: F) -> FlatMap<Self, F>
where Self: Sized, F: FnMut(Self::Output) -> PResult<O>,

Uses f to map over the output of self. If f returns an error the parser fails.

use srdf::{RDFNodeParse, RDFFormat, RDFParseError, ReaderMode, property_string, PResult};
    let s = r#"prefix : <http://example.org/>
    :x :p "1" .
  "#;
  let mut graph = SRDFGraph::from_str(s, &RDFFormat::Turtle, None, &ReaderMode::default()).unwrap();
  let x = iri!("http://example.org/x");
  let p = iri!("http://example.org/p");
  fn cnv_int(s: String) -> PResult<isize> {
     s.parse().map_err(|_| RDFParseError::Custom{ msg: format!("Error converting {s}")})
  }
  let mut parser = property_string(&p).flat_map(cnv_int);
  assert_eq!(parser.parse(&x, graph).unwrap(), 1)
Source

fn and_then<F, O, E>(self, f: F) -> AndThen<Self, F>
where Self: Sized, F: FnMut(Self::Output) -> Result<O, E>, E: Into<RDFParseError>,

Parses with self and applies f on the result if self parses successfully. f may optionally fail with an error which is automatically converted to a RDFParseError.

use srdf::{RDFNodeParse, RDFFormat, RDFParseError, ReaderMode, property_string};
let s = r#"prefix : <http://example.org/>
       :x :p "1" .
  "#;
let mut graph = SRDFGraph::from_str(s, &RDFFormat::Turtle, None, &ReaderMode::default()).unwrap();
let x = iri!("http://example.org/x");
let p = iri!("http://example.org/p");


struct IntConversionError(String);

fn cnv_int(s: String) -> Result<isize, IntConversionError> {
   s.parse().map_err(|_| IntConversionError(s))
}

impl Into<RDFParseError> for IntConversionError {
    fn into(self) -> RDFParseError {
        RDFParseError::Custom { msg: format!("Int conversion error: {}", self.0)}
    }
}

let mut parser = property_string(&p).and_then(cnv_int);
assert_eq!(parser.parse(&x, graph).unwrap(), 1)
Source

fn map<F, B>(self, f: F) -> Map<Self, F>
where Self: Sized, F: FnMut(Self::Output) -> B,

Uses f to map over the parsed value.

use srdf::{RDFNodeParse, RDFFormat, ReaderMode, property_integer};
let s = r#"prefix : <http://example.org/>
         :x :p 1 .
 "#;
let mut graph = SRDFGraph::from_str(s, &RDFFormat::Turtle, None, &ReaderMode::default()).unwrap();
let p = iri!("http://example.org/p");
let mut parser = property_integer(&p).map(|n| n + 1);
assert_eq!(parser.parse(&iri!("http://example.org/x"), graph).unwrap(), 2)
Source

fn and<P2>(self, parser: P2) -> (Self, P2)
where Self: Sized, P2: RDFNodeParse<RDF>,

Parses self followed by p. Succeeds if both parsers succeed, otherwise fails. Returns a tuple with both values on success.

let s = r#"prefix : <http://example.org/>
      :x :p true ;
         :q 1    .
    "#;
let mut graph = SRDFGraph::from_str(s, &RDFFormat::Turtle, None, &ReaderMode::default()).unwrap();
let x = IriS::new_unchecked("http://example.org/x");
let p = IriS::new_unchecked("http://example.org/p");
let q = IriS::new_unchecked("http://example.org/q");
let mut parser = property_bool(&p).and(property_integer(&q));
assert_eq!(parser.parse(&x, graph).unwrap(), (true, 1))
Source

fn then<N, F>(self, f: F) -> Then<Self, F>
where Self: Sized, F: FnMut(Self::Output) -> N, N: RDFNodeParse<RDF>,

Parses using self and then passes the value to f which returns a parser used to parse the rest of the input.

Source

fn then_ref<N, F>(self, f: F) -> ThenRef<Self, F>
where Self: Sized, F: FnMut(&Self::Output) -> N, N: RDFNodeParse<RDF>,

Parses using self and then passes a reference to the value to f which returns a parser used to parse the rest of the input.

Source

fn then_mut<N, F>(self, f: F) -> ThenMut<Self, F>
where Self: Sized, F: FnMut(&mut Self::Output) -> N, N: RDFNodeParse<RDF>,

Parses using self and then passes a reference to the mutable value to f which returns a parser used to parse the rest of the input.

use srdf::{RDFNodeParse, RDFFormat, ReaderMode, ok, property_integers};
    let s = r#"prefix : <http://example.org/>
      :x :p 1, 2, 3 .
    "#;
    let mut graph = SRDFGraph::from_str(s, &RDFFormat::Turtle, None, &ReaderMode::default()).unwrap();
    let x = IriS::new_unchecked("http://example.org/x");
    let p = IriS::new_unchecked("http://example.org/p");
    let mut parser = property_integers(&p).then_mut(move |ns| {
        ns.extend(vec![4, 5]);
        ok(ns)
     });
    assert_eq!(parser.parse(&x, graph).unwrap(), HashSet::from([1, 2, 3, 4, 5]))
Source

fn or<P2>(self, parser: P2) -> Or<Self, P2>
where Self: Sized, P2: RDFNodeParse<RDF, Output = Self::Output>,

Returns a parser which attempts to parse using self. If self fails then it attempts parser.

 let s = r#"prefix : <http://example.org/>
      :x :p 1, 2 ;
         :q true .
    "#;
 let mut graph = SRDFGraph::from_str(s, &RDFFormat::Turtle, None, &ReaderMode::default()).unwrap();
 let x = IriS::new_unchecked("http://example.org/x");
 let p = IriS::new_unchecked("http://example.org/p");
 let q = IriS::new_unchecked("http://example.org/q");
 let mut parser = property_bool(&p).or(property_bool(&q));
 assert_eq!(parser.parse(&x, graph).unwrap(), true)
Source

fn focus(self, node: &RDF::Term) -> SetFocus<RDF>
where Self: Sized,

Sets the focus node and returns ()

Source

fn with<P, A>(self, parser: P) -> With<Self, P>
where Self: Sized, P: RDFNodeParse<RDF, Output = A>,

Discards the value of the current parser and returns the value of parser

let s = r#"prefix : <http://example.org/>
           :x :p :y .
"#;
let mut graph = SRDFGraph::from_str(s, &RDFFormat::Turtle, None, &ReaderMode::default()).unwrap();
let p = IriS::new_unchecked("http://example.org/p");
let x = IriS::new_unchecked("http://example.org/x");
assert_eq!(
  property_value(&p).with(ok(&1))
  .parse(&x, graph).unwrap(),
  1
)

Implementations on Foreign Types§

Source§

impl<RDF, P1, P2, A, B> RDFNodeParse<RDF> for (P1, P2)
where RDF: FocusRDF, P1: RDFNodeParse<RDF, Output = A>, P2: RDFNodeParse<RDF, Output = B>,

Source§

type Output = (A, B)

Source§

fn parse_impl(&mut self, rdf: &mut RDF) -> PResult<Self::Output>

Implementors§

Source§

impl<'p, RDF, P, O> RDFNodeParse<RDF> for ByRef<'p, P>
where RDF: FocusRDF, P: RDFNodeParse<RDF, Output = O>,

Source§

impl<RDF> RDFNodeParse<RDF> for GetFocus<RDF>
where RDF: FocusRDF,

Source§

type Output = <RDF as SRDFBasic>::Term

Source§

impl<RDF> RDFNodeParse<RDF> for Neighs<RDF>
where RDF: FocusRDF,

Source§

type Output = HashMap<<RDF as SRDFBasic>::IRI, HashSet<<RDF as SRDFBasic>::Term>>

Source§

impl<RDF> RDFNodeParse<RDF> for PropertyValue<RDF>
where RDF: FocusRDF,

Source§

type Output = <RDF as SRDFBasic>::Term

Source§

impl<RDF> RDFNodeParse<RDF> for PropertyValueDebug<RDF>
where RDF: FocusRDF + Debug,

Source§

type Output = <RDF as SRDFBasic>::Term

Source§

impl<RDF> RDFNodeParse<RDF> for PropertyValues<RDF>
where RDF: FocusRDF,

Source§

impl<RDF> RDFNodeParse<RDF> for RDFList<RDF>
where RDF: FocusRDF,

Source§

type Output = Vec<<RDF as SRDFBasic>::Term>

Source§

impl<RDF> RDFNodeParse<RDF> for SetFocus<RDF>
where RDF: FocusRDF,

Source§

impl<RDF> RDFNodeParse<RDF> for SubjectsPropertyValue<RDF>
where RDF: FocusRDF,

Source§

impl<RDF> RDFNodeParse<RDF> for Term<RDF>
where RDF: FocusRDF,

Source§

type Output = <RDF as SRDFBasic>::Term

Source§

impl<RDF, A, B, P1, P2> RDFNodeParse<RDF> for With<P1, P2>
where RDF: FocusRDF, P1: RDFNodeParse<RDF, Output = A>, P2: RDFNodeParse<RDF, Output = B>,

Source§

impl<RDF, A, B, P, F> RDFNodeParse<RDF> for Map<P, F>
where RDF: FocusRDF, P: RDFNodeParse<RDF, Output = A>, F: FnMut(A) -> B,

Source§

impl<RDF, A, P> RDFNodeParse<RDF> for ParserNodes<RDF, P>
where RDF: FocusRDF, P: RDFNodeParse<RDF, Output = A>,

Source§

impl<RDF, P1, P2, A> RDFNodeParse<RDF> for CombineVec<P1, P2>
where RDF: FocusRDF, P1: RDFNodeParse<RDF, Output = Vec<A>>, P2: RDFNodeParse<RDF, Output = Vec<A>>,

Source§

impl<RDF, P1, P2, O> RDFNodeParse<RDF> for Or<P1, P2>
where RDF: FocusRDF, P1: RDFNodeParse<RDF, Output = O>, P2: RDFNodeParse<RDF, Output = O>,

Source§

impl<RDF, P> RDFNodeParse<RDF> for Optional<P>
where RDF: FocusRDF, P: RDFNodeParse<RDF>,

Source§

impl<RDF, P> RDFNodeParse<RDF> for Satisfy<RDF, P>
where RDF: FocusRDF, P: FnMut(&RDF::Term) -> bool,

Source§

impl<RDF, P, A> RDFNodeParse<RDF> for ParseByType<IriS, P>
where RDF: FocusRDF, P: RDFNodeParse<RDF, Output = A>,

Source§

impl<RDF, P, A> RDFNodeParse<RDF> for ParseRDFList<P>
where RDF: FocusRDF, P: RDFNodeParse<RDF, Output = A>,

Source§

impl<RDF, P, F, N> RDFNodeParse<RDF> for Then<P, F>
where RDF: FocusRDF, P: RDFNodeParse<RDF>, F: FnMut(P::Output) -> N, N: RDFNodeParse<RDF>,

Source§

type Output = <N as RDFNodeParse<RDF>>::Output

Source§

impl<RDF, P, F, N> RDFNodeParse<RDF> for ThenMut<P, F>
where RDF: FocusRDF, P: RDFNodeParse<RDF>, F: FnMut(&mut P::Output) -> N, N: RDFNodeParse<RDF>,

Source§

type Output = <N as RDFNodeParse<RDF>>::Output

Source§

impl<RDF, P, F, N> RDFNodeParse<RDF> for ThenRef<P, F>
where RDF: FocusRDF, P: RDFNodeParse<RDF>, F: FnMut(&P::Output) -> N, N: RDFNodeParse<RDF>,

Source§

type Output = <N as RDFNodeParse<RDF>>::Output

Source§

impl<RDF, P, F, O> RDFNodeParse<RDF> for FlatMap<P, F>
where RDF: FocusRDF, P: RDFNodeParse<RDF>, F: FnMut(P::Output) -> PResult<O>,

Source§

impl<RDF, P, F, O, E> RDFNodeParse<RDF> for AndThen<P, F>
where RDF: FocusRDF, P: RDFNodeParse<RDF>, F: FnMut(P::Output) -> Result<O, E>, E: Into<RDFParseError>,

Source§

impl<RDF, P, O> RDFNodeParse<RDF> for Not<P>
where RDF: FocusRDF, P: RDFNodeParse<RDF, Output = O>, O: Debug,