pub struct Literal(_);
Expand description
An owned RDF literal.
The default string formatter is returning an N-Triples, Turtle, and SPARQL compatible representation:
use oxrdf::Literal;
use oxrdf::vocab::xsd;
assert_eq!(
"\"foo\\nbar\"",
Literal::new_simple_literal("foo\nbar").to_string()
);
assert_eq!(
"\"1999-01-01\"^^<http://www.w3.org/2001/XMLSchema#date>",
Literal::new_typed_literal("1999-01-01", xsd::DATE).to_string()
);
assert_eq!(
"\"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>) -> Self
pub fn new_simple_literal(value: impl Into<String>) -> Self
Builds an RDF simple literal.
sourcepub fn new_language_tagged_literal(
value: impl Into<String>,
language: impl Into<String>
) -> Result<Self, LanguageTagParseError>
pub fn new_language_tagged_literal( value: impl Into<String>, language: impl Into<String> ) -> Result<Self, LanguageTagParseError>
Builds an RDF language-tagged string.
sourcepub fn new_language_tagged_literal_unchecked(
value: impl Into<String>,
language: impl Into<String>
) -> Self
pub fn new_language_tagged_literal_unchecked( value: impl Into<String>, language: impl Into<String> ) -> Self
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§impl From<DayTimeDuration> for Literal
impl From<DayTimeDuration> for Literal
source§fn from(value: DayTimeDuration) -> Self
fn from(value: DayTimeDuration) -> Self
source§impl From<GYearMonth> for Literal
impl From<GYearMonth> for Literal
source§fn from(value: GYearMonth) -> Self
fn from(value: GYearMonth) -> Self
source§impl<'a> From<LiteralRef<'a>> for Literal
impl<'a> From<LiteralRef<'a>> for Literal
source§fn from(node: LiteralRef<'a>) -> Self
fn from(node: LiteralRef<'a>) -> Self
source§impl From<YearMonthDuration> for Literal
impl From<YearMonthDuration> for Literal
source§fn from(value: YearMonthDuration) -> Self
fn from(value: YearMonthDuration) -> Self
source§impl FromStr for Literal
impl FromStr for Literal
source§fn from_str(s: &str) -> Result<Self, TermParseError>
fn from_str(s: &str) -> Result<Self, TermParseError>
Parses a literal from its NTriples or Turtle serialization
use oxrdf::{Literal, NamedNode, vocab::xsd};
use std::str::FromStr;
assert_eq!(Literal::from_str("\"ex\\n\"").unwrap(), Literal::new_simple_literal("ex\n"));
assert_eq!(Literal::from_str("\"ex\"@en").unwrap(), Literal::new_language_tagged_literal("ex", "en").unwrap());
assert_eq!(Literal::from_str("\"2020\"^^<http://www.w3.org/2001/XMLSchema#gYear>").unwrap(), Literal::new_typed_literal("2020", NamedNode::new("http://www.w3.org/2001/XMLSchema#gYear").unwrap()));
assert_eq!(Literal::from_str("true").unwrap(), Literal::new_typed_literal("true", xsd::BOOLEAN));
assert_eq!(Literal::from_str("+122").unwrap(), Literal::new_typed_literal("+122", xsd::INTEGER));
assert_eq!(Literal::from_str("-122.23").unwrap(), Literal::new_typed_literal("-122.23", xsd::DECIMAL));
assert_eq!(Literal::from_str("-122e+1").unwrap(), Literal::new_typed_literal("-122e+1", xsd::DOUBLE));
§type Err = TermParseError
type Err = TermParseError
source§impl PartialEq<Literal> for Literal
impl PartialEq<Literal> for Literal
source§impl PartialEq<Literal> for LiteralRef<'_>
impl PartialEq<Literal> for LiteralRef<'_>
source§impl PartialEq<LiteralRef<'_>> for Literal
impl PartialEq<LiteralRef<'_>> for Literal
source§fn eq(&self, other: &LiteralRef<'_>) -> bool
fn eq(&self, other: &LiteralRef<'_>) -> bool
self
and other
values to be equal, and is used
by ==
.