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§
Required Methods§
Sourcefn parse_impl(&mut self, rdf: &mut RDF) -> PResult<Self::Output>
fn parse_impl(&mut self, rdf: &mut RDF) -> PResult<Self::Output>
Parses the current focus node without modifying the state
Provided Methods§
Sourcefn parse(&mut self, node: &IriS, rdf: RDF) -> PResult<Self::Output>
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.
fn by_ref(&mut self) -> ByRef<'_, Self>where
Self: Sized,
Sourcefn flat_map<F, O>(self, f: F) -> FlatMap<Self, F>
fn flat_map<F, O>(self, f: F) -> FlatMap<Self, F>
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)
Sourcefn and_then<F, O, E>(self, f: F) -> AndThen<Self, F>
fn and_then<F, O, E>(self, f: F) -> AndThen<Self, F>
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)
Sourcefn map<F, B>(self, f: F) -> Map<Self, F>
fn map<F, B>(self, f: F) -> Map<Self, F>
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)
Sourcefn and<P2>(self, parser: P2) -> (Self, P2)where
Self: Sized,
P2: RDFNodeParse<RDF>,
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))
Sourcefn then<N, F>(self, f: F) -> Then<Self, F>
fn then<N, F>(self, f: F) -> Then<Self, F>
Parses using self
and then passes the value to f
which returns a parser used to parse
the rest of the input.
Sourcefn then_ref<N, F>(self, f: F) -> ThenRef<Self, F>
fn then_ref<N, F>(self, f: F) -> ThenRef<Self, F>
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.
Sourcefn then_mut<N, F>(self, f: F) -> ThenMut<Self, F>
fn then_mut<N, F>(self, f: F) -> ThenMut<Self, F>
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]))
Sourcefn or<P2>(self, parser: P2) -> Or<Self, P2>
fn or<P2>(self, parser: P2) -> Or<Self, P2>
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)
Sourcefn focus(self, node: &RDF::Term) -> SetFocus<RDF>where
Self: Sized,
fn focus(self, node: &RDF::Term) -> SetFocus<RDF>where
Self: Sized,
Sets the focus node and returns ()
Sourcefn with<P, A>(self, parser: P) -> With<Self, P>where
Self: Sized,
P: RDFNodeParse<RDF, Output = A>,
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
)