Fork of https://github.com/oxigraph/oxigraph.git for the purpose of NextGraph project
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
524 lines
17 KiB
524 lines
17 KiB
3 years ago
|
use crate::named_node::NamedNode;
|
||
|
use crate::vocab::rdf;
|
||
|
use crate::vocab::xsd;
|
||
|
use crate::NamedNodeRef;
|
||
5 years ago
|
use oxilangtag::{LanguageTag, LanguageTagParseError};
|
||
5 years ago
|
use rio_api::model as rio;
|
||
7 years ago
|
use std::borrow::Cow;
|
||
|
use std::fmt;
|
||
|
use std::option::Option;
|
||
|
|
||
4 years ago
|
/// An owned RDF [literal](https://www.w3.org/TR/rdf11-concepts/#dfn-literal)
|
||
6 years ago
|
///
|
||
4 years ago
|
/// The default string formatter is returning an N-Triples, Turtle and SPARQL compatible representation:
|
||
6 years ago
|
/// ```
|
||
5 years ago
|
/// # use oxilangtag::LanguageTagParseError;
|
||
3 years ago
|
/// use oxrdf::Literal;
|
||
|
/// use oxrdf::vocab::xsd;
|
||
6 years ago
|
///
|
||
|
/// assert_eq!(
|
||
5 years ago
|
/// "\"foo\\nbar\"",
|
||
|
/// Literal::new_simple_literal("foo\nbar").to_string()
|
||
6 years ago
|
/// );
|
||
|
///
|
||
|
/// assert_eq!(
|
||
|
/// "\"1999-01-01\"^^<http://www.w3.org/2001/XMLSchema#date>",
|
||
4 years ago
|
/// Literal::new_typed_literal("1999-01-01", xsd::DATE).to_string()
|
||
6 years ago
|
/// );
|
||
|
///
|
||
|
/// assert_eq!(
|
||
|
/// "\"foo\"@en",
|
||
5 years ago
|
/// Literal::new_language_tagged_literal("foo", "en")?.to_string()
|
||
6 years ago
|
/// );
|
||
5 years ago
|
/// # Result::<(), LanguageTagParseError>::Ok(())
|
||
6 years ago
|
/// ```
|
||
5 years ago
|
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
|
||
6 years ago
|
pub struct Literal(LiteralContent);
|
||
|
|
||
5 years ago
|
#[derive(PartialEq, Eq, Debug, Clone, Hash)]
|
||
6 years ago
|
enum LiteralContent {
|
||
7 years ago
|
String(String),
|
||
5 years ago
|
LanguageTaggedString { value: String, language: String },
|
||
|
TypedLiteral { value: String, datatype: NamedNode },
|
||
7 years ago
|
}
|
||
|
|
||
|
impl Literal {
|
||
5 years ago
|
/// Builds an RDF [simple literal](https://www.w3.org/TR/rdf11-concepts/#dfn-simple-literal)
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
pub fn new_simple_literal(value: impl Into<String>) -> Self {
|
||
3 years ago
|
Self(LiteralContent::String(value.into()))
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
/// 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)
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
pub fn new_typed_literal(value: impl Into<String>, datatype: impl Into<NamedNode>) -> Self {
|
||
7 years ago
|
let value = value.into();
|
||
|
let datatype = datatype.into();
|
||
3 years ago
|
Self(if datatype == xsd::STRING {
|
||
6 years ago
|
LiteralContent::String(value)
|
||
7 years ago
|
} else {
|
||
6 years ago
|
LiteralContent::TypedLiteral { value, datatype }
|
||
|
})
|
||
7 years ago
|
}
|
||
|
|
||
5 years ago
|
/// Builds an RDF [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string)
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
pub fn new_language_tagged_literal(
|
||
|
value: impl Into<String>,
|
||
5 years ago
|
language: impl Into<String>,
|
||
5 years ago
|
) -> Result<Self, LanguageTagParseError> {
|
||
5 years ago
|
let mut language = language.into();
|
||
|
language.make_ascii_lowercase();
|
||
4 years ago
|
Ok(Self::new_language_tagged_literal_unchecked(
|
||
|
value,
|
||
|
LanguageTag::parse(language)?.into_inner(),
|
||
|
))
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/// Builds an RDF [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string)
|
||
5 years ago
|
///
|
||
5 years ago
|
/// It is the responsibility of the caller to check that `language`
|
||
5 years ago
|
/// is valid [BCP47](https://tools.ietf.org/html/bcp47) language tag,
|
||
|
/// and is lowercase.
|
||
5 years ago
|
///
|
||
4 years ago
|
/// [`Literal::new_language_tagged_literal()`] is a safe version of this constructor and should be used for untrusted data.
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
pub fn new_language_tagged_literal_unchecked(
|
||
5 years ago
|
value: impl Into<String>,
|
||
|
language: impl Into<String>,
|
||
|
) -> Self {
|
||
3 years ago
|
Self(LiteralContent::LanguageTaggedString {
|
||
7 years ago
|
value: value.into(),
|
||
5 years ago
|
language: language.into(),
|
||
6 years ago
|
})
|
||
7 years ago
|
}
|
||
|
|
||
|
/// The literal [lexical form](https://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form)
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
pub fn value(&self) -> &str {
|
||
4 years ago
|
self.as_ref().value()
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
/// 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).
|
||
5 years ago
|
///
|
||
6 years ago
|
/// Language tags are defined by the [BCP47](https://tools.ietf.org/html/bcp47).
|
||
5 years ago
|
/// They are normalized to lowercase by this implementation.
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
pub fn language(&self) -> Option<&str> {
|
||
4 years ago
|
self.as_ref().language()
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
/// 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).
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn datatype(&self) -> NamedNodeRef<'_> {
|
||
|
self.as_ref().datatype()
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
/// Checks if this literal could be seen as an RDF 1.0 [plain literal](https://www.w3.org/TR/rdf-concepts/#dfn-plain-literal).
|
||
6 years ago
|
///
|
||
|
/// It returns true if the literal is a [language-tagged string](https://www.w3.org/TR/rdf11-concepts/#dfn-language-tagged-string)
|
||
6 years ago
|
/// or has the datatype [xsd:string](http://www.w3.org/2001/XMLSchema#string).
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
pub fn is_plain(&self) -> bool {
|
||
4 years ago
|
self.as_ref().is_plain()
|
||
|
}
|
||
|
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
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(),
|
||
|
},
|
||
|
})
|
||
7 years ago
|
}
|
||
5 years ago
|
|
||
|
/// Extract components from this literal
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
pub fn destruct(self) -> (String, Option<NamedNode>, Option<String>) {
|
||
|
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),
|
||
|
}
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
impl fmt::Display for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
4 years ago
|
self.as_ref().fmt(f)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> From<&'a str> for Literal {
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
fn from(value: &'a str) -> Self {
|
||
3 years ago
|
Self(LiteralContent::String(value.into()))
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<String> for Literal {
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
fn from(value: String) -> Self {
|
||
3 years ago
|
Self(LiteralContent::String(value))
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
impl<'a> From<Cow<'a, str>> for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn from(value: Cow<'a, str>) -> Self {
|
||
3 years ago
|
Self(LiteralContent::String(value.into()))
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
impl From<bool> for Literal {
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
fn from(value: bool) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
5 years ago
|
value: value.to_string(),
|
||
4 years ago
|
datatype: xsd::BOOLEAN.into(),
|
||
5 years ago
|
})
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
impl From<i128> for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn from(value: i128) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
5 years ago
|
value: value.to_string(),
|
||
4 years ago
|
datatype: xsd::INTEGER.into(),
|
||
5 years ago
|
})
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<i64> for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn from(value: i64) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
5 years ago
|
value: value.to_string(),
|
||
4 years ago
|
datatype: xsd::INTEGER.into(),
|
||
5 years ago
|
})
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<i32> for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn from(value: i32) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
5 years ago
|
value: value.to_string(),
|
||
4 years ago
|
datatype: xsd::INTEGER.into(),
|
||
5 years ago
|
})
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<i16> for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn from(value: i16) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
5 years ago
|
value: value.to_string(),
|
||
4 years ago
|
datatype: xsd::INTEGER.into(),
|
||
5 years ago
|
})
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<u64> for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn from(value: u64) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
5 years ago
|
value: value.to_string(),
|
||
4 years ago
|
datatype: xsd::INTEGER.into(),
|
||
5 years ago
|
})
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<u32> for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn from(value: u32) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
5 years ago
|
value: value.to_string(),
|
||
4 years ago
|
datatype: xsd::INTEGER.into(),
|
||
5 years ago
|
})
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<u16> for Literal {
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
fn from(value: u16) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
5 years ago
|
value: value.to_string(),
|
||
4 years ago
|
datatype: xsd::INTEGER.into(),
|
||
5 years ago
|
})
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<f32> for Literal {
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
fn from(value: f32) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
3 years ago
|
value: if value == f32::INFINITY {
|
||
|
"INF".to_string()
|
||
|
} else if value == f32::NEG_INFINITY {
|
||
|
"-INF".to_string()
|
||
|
} else {
|
||
|
value.to_string()
|
||
|
},
|
||
4 years ago
|
datatype: xsd::FLOAT.into(),
|
||
5 years ago
|
})
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<f64> for Literal {
|
||
4 years ago
|
#[inline]
|
||
7 years ago
|
fn from(value: f64) -> Self {
|
||
3 years ago
|
Self(LiteralContent::TypedLiteral {
|
||
3 years ago
|
value: if value == f64::INFINITY {
|
||
|
"INF".to_string()
|
||
|
} else if value == f64::NEG_INFINITY {
|
||
|
"-INF".to_string()
|
||
|
} else {
|
||
|
value.to_string()
|
||
|
},
|
||
4 years ago
|
datatype: xsd::DOUBLE.into(),
|
||
5 years ago
|
})
|
||
7 years ago
|
}
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
/// A borrowed RDF [literal](https://www.w3.org/TR/rdf11-concepts/#dfn-literal)
|
||
|
///
|
||
4 years ago
|
/// The default string formatter is returning an N-Triples, Turtle and SPARQL compatible representation:
|
||
4 years ago
|
/// ```
|
||
3 years ago
|
/// use oxrdf::LiteralRef;
|
||
|
/// use oxrdf::vocab::xsd;
|
||
4 years ago
|
///
|
||
|
/// assert_eq!(
|
||
|
/// "\"foo\\nbar\"",
|
||
|
/// LiteralRef::new_simple_literal("foo\nbar").to_string()
|
||
|
/// );
|
||
|
///
|
||
|
/// assert_eq!(
|
||
|
/// "\"1999-01-01\"^^<http://www.w3.org/2001/XMLSchema#date>",
|
||
|
/// LiteralRef::new_typed_literal("1999-01-01", xsd::DATE).to_string()
|
||
|
/// );
|
||
|
/// ```
|
||
|
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
|
||
|
pub struct LiteralRef<'a>(LiteralRefContent<'a>);
|
||
|
|
||
|
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
|
||
|
enum LiteralRefContent<'a> {
|
||
|
String(&'a str),
|
||
|
LanguageTaggedString {
|
||
|
value: &'a str,
|
||
|
language: &'a str,
|
||
|
},
|
||
|
TypedLiteral {
|
||
|
value: &'a str,
|
||
|
datatype: NamedNodeRef<'a>,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
impl<'a> LiteralRef<'a> {
|
||
|
/// Builds an RDF [simple literal](https://www.w3.org/TR/rdf11-concepts/#dfn-simple-literal)
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn new_simple_literal(value: &'a str) -> Self {
|
||
|
LiteralRef(LiteralRefContent::String(value))
|
||
|
}
|
||
|
|
||
|
/// 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)
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn new_typed_literal(value: &'a str, datatype: impl Into<NamedNodeRef<'a>>) -> Self {
|
||
|
let datatype = datatype.into();
|
||
|
LiteralRef(if datatype == xsd::STRING {
|
||
|
LiteralRefContent::String(value)
|
||
5 years ago
|
} else {
|
||
4 years ago
|
LiteralRefContent::TypedLiteral { value, datatype }
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/// 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.
|
||
|
///
|
||
4 years ago
|
/// [`Literal::new_language_tagged_literal()`] is a safe version of this constructor and should be used for untrusted data.
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn new_language_tagged_literal_unchecked(value: &'a str, language: &'a str) -> Self {
|
||
|
LiteralRef(LiteralRefContent::LanguageTaggedString { value, language })
|
||
|
}
|
||
|
|
||
|
/// The literal [lexical form](https://www.w3.org/TR/rdf11-concepts/#dfn-lexical-form)
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn value(self) -> &'a str {
|
||
|
match self.0 {
|
||
|
LiteralRefContent::String(value)
|
||
|
| LiteralRefContent::LanguageTaggedString { value, .. }
|
||
|
| LiteralRefContent::TypedLiteral { value, .. } => 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.
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn language(self) -> Option<&'a str> {
|
||
|
match self.0 {
|
||
|
LiteralRefContent::LanguageTaggedString { language, .. } => Some(language),
|
||
|
_ => None,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// 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).
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn datatype(self) -> NamedNodeRef<'a> {
|
||
|
match self.0 {
|
||
|
LiteralRefContent::String(_) => xsd::STRING,
|
||
|
LiteralRefContent::LanguageTaggedString { .. } => rdf::LANG_STRING,
|
||
|
LiteralRefContent::TypedLiteral { datatype, .. } => 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).
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn is_plain(self) -> bool {
|
||
4 years ago
|
matches!(
|
||
|
self.0,
|
||
|
LiteralRefContent::String(_) | LiteralRefContent::LanguageTaggedString { .. }
|
||
|
)
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn into_owned(self) -> Literal {
|
||
|
Literal(match self.0 {
|
||
|
LiteralRefContent::String(value) => LiteralContent::String(value.to_owned()),
|
||
|
LiteralRefContent::LanguageTaggedString { value, language } => {
|
||
|
LiteralContent::LanguageTaggedString {
|
||
|
value: value.to_owned(),
|
||
|
language: language.to_owned(),
|
||
|
}
|
||
|
}
|
||
|
LiteralRefContent::TypedLiteral { value, datatype } => LiteralContent::TypedLiteral {
|
||
|
value: value.to_owned(),
|
||
|
datatype: datatype.into_owned(),
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/// Extract components from this literal
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn destruct(self) -> (&'a str, Option<NamedNodeRef<'a>>, Option<&'a str>) {
|
||
|
match self.0 {
|
||
|
LiteralRefContent::String(s) => (s, None, None),
|
||
|
LiteralRefContent::LanguageTaggedString { value, language } => {
|
||
|
(value, None, Some(language))
|
||
5 years ago
|
}
|
||
4 years ago
|
LiteralRefContent::TypedLiteral { value, datatype } => (value, Some(datatype), None),
|
||
5 years ago
|
}
|
||
6 years ago
|
}
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
impl fmt::Display for LiteralRef<'_> {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
4 years ago
|
match self.0 {
|
||
|
LiteralRefContent::String(value) => rio::Literal::Simple { value },
|
||
|
LiteralRefContent::LanguageTaggedString { value, language } => {
|
||
|
rio::Literal::LanguageTaggedString { value, language }
|
||
|
}
|
||
|
LiteralRefContent::TypedLiteral { value, datatype } => rio::Literal::Typed {
|
||
|
value,
|
||
|
datatype: rio::NamedNode {
|
||
|
iri: datatype.as_str(),
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
.fmt(f)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> From<&'a Literal> for LiteralRef<'a> {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn from(node: &'a Literal) -> Self {
|
||
|
node.as_ref()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> From<LiteralRef<'a>> for Literal {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn from(node: LiteralRef<'a>) -> Self {
|
||
|
node.into_owned()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> From<&'a str> for LiteralRef<'a> {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn from(value: &'a str) -> Self {
|
||
|
LiteralRef(LiteralRefContent::String(value))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<Literal> for LiteralRef<'_> {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &Literal) -> bool {
|
||
|
*self == other.as_ref()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<LiteralRef<'_>> for Literal {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &LiteralRef<'_>) -> bool {
|
||
|
self.as_ref() == *other
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
#[cfg(test)]
|
||
4 years ago
|
mod tests {
|
||
4 years ago
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn test_simple_literal_equality() {
|
||
|
assert_eq!(
|
||
|
Literal::new_simple_literal("foo"),
|
||
4 years ago
|
Literal::new_typed_literal("foo", xsd::STRING)
|
||
|
);
|
||
|
assert_eq!(
|
||
|
Literal::new_simple_literal("foo"),
|
||
|
LiteralRef::new_typed_literal("foo", xsd::STRING)
|
||
|
);
|
||
|
assert_eq!(
|
||
|
LiteralRef::new_simple_literal("foo"),
|
||
|
Literal::new_typed_literal("foo", xsd::STRING)
|
||
|
);
|
||
|
assert_eq!(
|
||
|
LiteralRef::new_simple_literal("foo"),
|
||
|
LiteralRef::new_typed_literal("foo", xsd::STRING)
|
||
|
);
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
#[test]
|
||
|
fn test_float_format() {
|
||
|
assert_eq!("INF", Literal::from(f32::INFINITY).value());
|
||
|
assert_eq!("INF", Literal::from(f64::INFINITY).value());
|
||
|
assert_eq!("-INF", Literal::from(f32::NEG_INFINITY).value());
|
||
|
assert_eq!("-INF", Literal::from(f64::NEG_INFINITY).value());
|
||
|
assert_eq!("NaN", Literal::from(f32::NAN).value());
|
||
|
assert_eq!("NaN", Literal::from(f64::NAN).value());
|
||
|
}
|
||
4 years ago
|
}
|