use oxiri::{Iri, IriParseError}; use rio_api::model as rio; use std::fmt; /// An owned RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) /// /// The default string formatter is returning a N-Triples, Turtle and SPARQL compatible representation: /// ``` /// use oxigraph::model::NamedNode; /// /// assert_eq!( /// "", /// NamedNode::new("http://example.com/foo")?.to_string() /// ); /// # Result::<_,oxigraph::model::IriParseError>::Ok(()) /// ``` #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)] pub struct NamedNode { iri: String, } impl NamedNode { /// Builds and validate an RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) pub fn new(iri: impl Into) -> Result { Ok(Self::new_from_iri(Iri::parse(iri.into())?)) } #[inline] pub(crate) fn new_from_iri(iri: Iri) -> Self { Self::new_unchecked(iri.into_inner()) } /// Builds an RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) from a string. /// /// It is the caller's responsibility to ensure that `iri` is a valid IRI. /// /// Except if you really know what you do, you should use [`parse`](#method.parse). #[inline] pub fn new_unchecked(iri: impl Into) -> Self { Self { iri: iri.into() } } #[inline] pub fn as_str(&self) -> &str { self.iri.as_str() } #[inline] pub fn into_string(self) -> String { self.iri } #[inline] pub fn as_ref(&self) -> NamedNodeRef<'_> { NamedNodeRef::new_unchecked(&self.iri) } } impl fmt::Display for NamedNode { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.as_ref().fmt(f) } } impl PartialEq for NamedNode { #[inline] fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl PartialEq for str { #[inline] fn eq(&self, other: &NamedNode) -> bool { self == other.as_str() } } impl PartialEq<&str> for NamedNode { #[inline] fn eq(&self, other: &&str) -> bool { self == *other } } impl PartialEq for &str { #[inline] fn eq(&self, other: &NamedNode) -> bool { *self == other } } /// A borrowed RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) /// /// The default string formatter is returning a N-Triples, Turtle and SPARQL compatible representation: /// ``` /// use oxigraph::model::NamedNodeRef; /// /// assert_eq!( /// "", /// NamedNodeRef::new("http://example.com/foo")?.to_string() /// ); /// # Result::<_,oxigraph::model::IriParseError>::Ok(()) /// ``` #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)] pub struct NamedNodeRef<'a> { iri: &'a str, } impl<'a> NamedNodeRef<'a> { /// Builds and validate an RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) pub fn new(iri: &'a str) -> Result { Ok(Self::new_from_iri(Iri::parse(iri)?)) } #[inline] pub(crate) fn new_from_iri(iri: Iri<&'a str>) -> Self { Self::new_unchecked(iri.into_inner()) } /// Builds an RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) from a string. /// /// It is the caller's responsibility to ensure that `iri` is a valid IRI. /// /// Except if you really know what you do, you should use [`parse`](#method.parse). #[inline] pub const fn new_unchecked(iri: &'a str) -> Self { Self { iri } } #[inline] pub const fn as_str(self) -> &'a str { self.iri } #[inline] pub fn into_owned(self) -> NamedNode { NamedNode::new_unchecked(self.iri) } } impl fmt::Display for NamedNodeRef<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { rio::NamedNode::from(*self).fmt(f) } } impl From> for NamedNode { #[inline] fn from(node: NamedNodeRef<'_>) -> Self { node.into_owned() } } impl<'a> From<&'a NamedNode> for NamedNodeRef<'a> { fn from(node: &'a NamedNode) -> Self { node.as_ref() } } impl<'a> From> for rio::NamedNode<'a> { #[inline] fn from(node: NamedNodeRef<'a>) -> Self { rio::NamedNode { iri: node.as_str() } } } impl PartialEq for NamedNodeRef<'_> { #[inline] fn eq(&self, other: &NamedNode) -> bool { self.as_str() == other.as_str() } } impl PartialEq> for NamedNode { #[inline] fn eq(&self, other: &NamedNodeRef<'_>) -> bool { self.as_str() == other.as_str() } } impl PartialEq for NamedNodeRef<'_> { #[inline] fn eq(&self, other: &str) -> bool { self.as_str() == other } } impl PartialEq> for str { #[inline] fn eq(&self, other: &NamedNodeRef<'_>) -> bool { self == other.as_str() } } impl PartialEq<&str> for NamedNodeRef<'_> { #[inline] fn eq(&self, other: &&str) -> bool { self == *other } } impl PartialEq> for &str { #[inline] fn eq(&self, other: &NamedNodeRef<'_>) -> bool { *self == other } }