pub struct Literal(/* private fields */);
Expand description
An owned RDF literal.
The default string formatter is returning an N-Triples, Turtle, and SPARQL compatible representation:
use oxrdf::vocab::xsd;
use oxrdf::Literal;
assert_eq!(
"\"foo\\nbar\"",
Literal::new_simple_literal("foo\nbar").to_string()
);
assert_eq!(
r#""1999-01-01"^^<http://www.w3.org/2001/XMLSchema#date>"#,
Literal::new_typed_literal("1999-01-01", xsd::DATE).to_string()
);
assert_eq!(
r#""foo"@en"#,
Literal::new_language_tagged_literal("foo", "en")?.to_string()
);
Implementations§
Source§impl Literal
impl Literal
Sourcepub fn new_simple_literal(value: impl Into<String>) -> Literal
pub fn new_simple_literal(value: impl Into<String>) -> Literal
Builds an RDF simple literal.
Sourcepub fn new_typed_literal(
value: impl Into<String>,
datatype: impl Into<NamedNode>,
) -> Literal
pub fn new_typed_literal( value: impl Into<String>, datatype: impl Into<NamedNode>, ) -> Literal
Sourcepub fn new_language_tagged_literal(
value: impl Into<String>,
language: impl Into<String>,
) -> Result<Literal, LanguageTagParseError>
pub fn new_language_tagged_literal( value: impl Into<String>, language: impl Into<String>, ) -> Result<Literal, LanguageTagParseError>
Builds an RDF language-tagged string.
Sourcepub fn new_language_tagged_literal_unchecked(
value: impl Into<String>,
language: impl Into<String>,
) -> Literal
pub fn new_language_tagged_literal_unchecked( value: impl Into<String>, language: impl Into<String>, ) -> Literal
Builds an RDF language-tagged string.
It is the responsibility of the caller to check that language
is valid BCP47 language tag,
and is lowercase.
Literal::new_language_tagged_literal()
is a safe version of this constructor and should be used for untrusted data.
Sourcepub fn value(&self) -> &str
pub fn value(&self) -> &str
The literal lexical form.
Sourcepub fn language(&self) -> Option<&str>
pub fn language(&self) -> Option<&str>
The literal language tag if it is a language-tagged string.
Language tags are defined by the BCP47. They are normalized to lowercase by this implementation.
Sourcepub fn datatype(&self) -> NamedNodeRef<'_>
pub fn datatype(&self) -> NamedNodeRef<'_>
The literal datatype.
The datatype of language-tagged string is always rdf:langString. The datatype of simple literals is xsd:string.
Sourcepub fn is_plain(&self) -> bool
pub fn is_plain(&self) -> bool
Checks if this literal could be seen as an RDF 1.0 plain literal.
It returns true if the literal is a language-tagged string or has the datatype xsd:string.
pub fn as_ref(&self) -> LiteralRef<'_>
Trait Implementations§
Source§impl<'a> From<&'a Literal> for LiteralRef<'a>
impl<'a> From<&'a Literal> for LiteralRef<'a>
Source§fn from(node: &'a Literal) -> LiteralRef<'a>
fn from(node: &'a Literal) -> LiteralRef<'a>
Source§impl From<DayTimeDuration> for Literal
impl From<DayTimeDuration> for Literal
Source§fn from(value: DayTimeDuration) -> Literal
fn from(value: DayTimeDuration) -> Literal
Source§impl From<GYearMonth> for Literal
impl From<GYearMonth> for Literal
Source§fn from(value: GYearMonth) -> Literal
fn from(value: GYearMonth) -> Literal
Source§impl<'a> From<LiteralRef<'a>> for Literal
impl<'a> From<LiteralRef<'a>> for Literal
Source§fn from(node: LiteralRef<'a>) -> Literal
fn from(node: LiteralRef<'a>) -> Literal
Source§impl From<YearMonthDuration> for Literal
impl From<YearMonthDuration> for Literal
Source§fn from(value: YearMonthDuration) -> Literal
fn from(value: YearMonthDuration) -> Literal
Source§impl FromStr for Literal
impl FromStr for Literal
Source§fn from_str(s: &str) -> Result<Literal, <Literal as FromStr>::Err>
fn from_str(s: &str) -> Result<Literal, <Literal as FromStr>::Err>
Parses a literal from its NTriples serialization
use oxrdf::vocab::xsd;
use oxrdf::{Literal, NamedNode};
use std::str::FromStr;
assert_eq!(
Literal::from_str("\"ex\\n\"")?,
Literal::new_simple_literal("ex\n")
);
assert_eq!(
Literal::from_str("\"ex\"@en")?,
Literal::new_language_tagged_literal("ex", "en")?
);
assert_eq!(
Literal::from_str("\"2020\"^^<http://www.w3.org/2001/XMLSchema#gYear>")?,
Literal::new_typed_literal(
"2020",
NamedNode::new("http://www.w3.org/2001/XMLSchema#gYear")?
)
);
assert_eq!(
Literal::from_str("true")?,
Literal::new_typed_literal("true", xsd::BOOLEAN)
);
assert_eq!(
Literal::from_str("+122")?,
Literal::new_typed_literal("+122", xsd::INTEGER)
);
assert_eq!(
Literal::from_str("-122.23")?,
Literal::new_typed_literal("-122.23", xsd::DECIMAL)
);
assert_eq!(
Literal::from_str("-122e+1")?,
Literal::new_typed_literal("-122e+1", xsd::DOUBLE)
);