use crate::named_node::NamedNode; use crate::vocab::rdf; use crate::vocab::xsd; use crate::NamedNodeRef; use oxilangtag::{LanguageTag, LanguageTagParseError}; #[cfg(feature = "oxsdatatypes")] use oxsdatatypes::*; use std::borrow::Cow; use std::fmt; use std::fmt::Write; use std::option::Option; /// An owned RDF [literal](https://www.w3.org/TR/rdf11-concepts/#dfn-literal). /// /// The default string formatter is returning an N-Triples, Turtle, and SPARQL compatible representation: /// ``` /// # use oxilangtag::LanguageTagParseError; /// use oxrdf::Literal; /// use oxrdf::vocab::xsd; /// /// assert_eq!( /// "\"foo\\nbar\"", /// Literal::new_simple_literal("foo\nbar").to_string() /// ); /// /// assert_eq!( /// "\"1999-01-01\"^^", /// Literal::new_typed_literal("1999-01-01", xsd::DATE).to_string() /// ); /// /// assert_eq!( /// "\"foo\"@en", /// Literal::new_language_tagged_literal("foo", "en")?.to_string() /// ); /// # Result::<(), LanguageTagParseError>::Ok(()) /// ``` #[derive(Eq, PartialEq, Debug, Clone, Hash)] pub struct Literal(LiteralContent); #[derive(PartialEq, Eq, Debug, Clone, Hash)] enum LiteralContent { String(String), LanguageTaggedString { value: String, language: String }, TypedLiteral { value: String, datatype: NamedNode }, } impl Literal { /// Builds an RDF [simple literal](https://www.w3.org/TR/rdf11-concepts/#dfn-simple-literal). #[inline] pub fn new_simple_literal(value: impl Into) -> Self { Self(LiteralContent::String(value.into())) } /// Builds an RDF [literal](https://www.w3.org/TR/rdf11-concepts/#dfn-literal) with a [datatype](https://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri). #[inline] pub fn new_typed_literal(value: impl Into, datatype: impl Into) -> Self { let value = value.into(); let datatype = datatype.into(); Self(if datatype == xsd::STRING { LiteralContent::String(value) } else { LiteralContent::TypedLiteral { value, datatype } }) } /// Builds an RDF [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string). #[inline] pub fn new_language_tagged_literal( value: impl Into, language: impl Into, ) -> Result { let mut language = language.into(); language.make_ascii_lowercase(); Ok(Self::new_language_tagged_literal_unchecked( value, LanguageTag::parse(language)?.into_inner(), )) } /// Builds an RDF [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string). /// /// It is the responsibility of the caller to check that `language` /// is valid [BCP47](https://tools.ietf.org/html/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. #[inline] pub fn new_language_tagged_literal_unchecked( value: impl Into, language: impl Into, ) -> Self { Self(LiteralContent::LanguageTaggedString { value: value.into(), language: language.into(), }) } /// The literal [lexical form](https://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form). #[inline] pub fn value(&self) -> &str { self.as_ref().value() } /// The literal [language tag](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tag) if it is a [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string). /// /// Language tags are defined by the [BCP47](https://tools.ietf.org/html/bcp47). /// They are normalized to lowercase by this implementation. #[inline] pub fn language(&self) -> Option<&str> { self.as_ref().language() } /// The literal [datatype](https://www.w3.org/TR/rdf11-concepts/#dfn-datatype-iri). /// /// The datatype of [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string) is always [rdf:langString](http://www.w3.org/1999/02/22-rdf-syntax-ns#langString). /// The datatype of [simple literals](https://www.w3.org/TR/rdf11-concepts/#dfn-simple-literal) is [xsd:string](http://www.w3.org/2001/XMLSchema#string). #[inline] pub fn datatype(&self) -> NamedNodeRef<'_> { self.as_ref().datatype() } /// Checks if this literal could be seen as an RDF 1.0 [plain literal](https://www.w3.org/TR/rdf-concepts/#dfn-plain-literal). /// /// It returns true if the literal is a [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string) /// or has the datatype [xsd:string](http://www.w3.org/2001/XMLSchema#string). #[inline] pub fn is_plain(&self) -> bool { self.as_ref().is_plain() } #[inline] pub fn as_ref(&self) -> LiteralRef<'_> { LiteralRef(match &self.0 { LiteralContent::String(value) => LiteralRefContent::String(value), LiteralContent::LanguageTaggedString { value, language } => { LiteralRefContent::LanguageTaggedString { value, language } } LiteralContent::TypedLiteral { value, datatype } => LiteralRefContent::TypedLiteral { value, datatype: datatype.as_ref(), }, }) } /// Extract components from this literal (value, datatype and language tag). #[inline] pub fn destruct(self) -> (String, Option, Option) { match self.0 { LiteralContent::String(s) => (s, None, None), LiteralContent::LanguageTaggedString { value, language } => { (value, None, Some(language)) } LiteralContent::TypedLiteral { value, datatype } => (value, Some(datatype), None), } } } impl fmt::Display for Literal { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.as_ref().fmt(f) } } impl<'a> From<&'a str> for Literal { #[inline] fn from(value: &'a str) -> Self { Self(LiteralContent::String(value.into())) } } impl From for Literal { #[inline] fn from(value: String) -> Self { Self(LiteralContent::String(value)) } } impl<'a> From> for Literal { #[inline] fn from(value: Cow<'a, str>) -> Self { Self(LiteralContent::String(value.into())) } } impl From for Literal { #[inline] fn from(value: bool) -> Self { Self(LiteralContent::TypedLiteral { value: value.to_string(), datatype: xsd::BOOLEAN.into(), }) } } impl From for Literal { #[inline] fn from(value: i128) -> Self { Self(LiteralContent::TypedLiteral { value: value.to_string(), datatype: xsd::INTEGER.into(), }) } } impl From for Literal { #[inline] fn from(value: i64) -> Self { Self(LiteralContent::TypedLiteral { value: value.to_string(), datatype: xsd::INTEGER.into(), }) } } impl From for Literal { #[inline] fn from(value: i32) -> Self { Self(LiteralContent::TypedLiteral { value: value.to_string(), datatype: xsd::INTEGER.into(), }) } } impl From for Literal { #[inline] fn from(value: i16) -> Self { Self(LiteralContent::TypedLiteral { value: value.to_string(), datatype: xsd::INTEGER.into(), }) } } impl From for Literal { #[inline] fn from(value: u64) -> Self { Self(LiteralContent::TypedLiteral { value: value.to_string(), datatype: xsd::INTEGER.into(), }) } } impl From for Literal { #[inline] fn from(value: u32) -> Self { Self(LiteralContent::TypedLiteral { value: value.to_string(), datatype: xsd::INTEGER.into(), }) } } impl From for Literal { #[inline] fn from(value: u16) -> Self { Self(LiteralContent::TypedLiteral { value: value.to_string(), datatype: xsd::INTEGER.into(), }) } } impl From for Literal { #[inline] fn from(value: f32) -> Self { Self(LiteralContent::TypedLiteral { value: if value == f32::INFINITY { "INF".to_string() } else if value == f32::NEG_INFINITY { "-INF".to_string() } else { value.to_string() }, datatype: xsd::FLOAT.into(), }) } } impl From for Literal { #[inline] fn from(value: f64) -> Self { Self(LiteralContent::TypedLiteral { value: if value == f64::INFINITY { "INF".to_string() } else if value == f64::NEG_INFINITY { "-INF".to_string() } else { value.to_string() }, datatype: xsd::DOUBLE.into(), }) } } #[cfg(feature = "oxsdatatypes")] impl From for Literal { #[inline] fn from(value: Boolean) -> Self { Self::new_typed_literal(value.to_string(), xsd::BOOLEAN) } } #[cfg(feature = "oxsdatatypes")] impl From for Literal { #[inline] fn from(value: Float) -> Self { Self::new_typed_literal(value.to_string(), xsd::FLOAT) } } #[cfg(feature = "oxsdatatypes")] impl From for Literal { #[inline] fn from(value: Double) -> Self { Self::new_typed_literal(value.to_string(), xsd::DOUBLE) } } #[cfg(feature = "oxsdatatypes")] impl From for Literal { #[inline] fn from(value: Integer) -> Self { Self::new_typed_literal(value.to_string(), xsd::INTEGER) } } #[cfg(feature = "oxsdatatypes")] impl From for Literal { #[inline] fn from(value: Decimal) -> Self { Self::new_typed_literal(value.to_string(), xsd::DECIMAL) } } #[cfg(feature = "oxsdatatypes")] impl From for Literal { #[inline] fn from(value: DateTime) -> Self { Self::new_typed_literal(value.to_string(), xsd::DATE_TIME) } } #[cfg(feature = "oxsdatatypes")] impl From