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.
204 lines
5.0 KiB
204 lines
5.0 KiB
5 years ago
|
use oxiri::{Iri, IriParseError};
|
||
5 years ago
|
use rio_api::model as rio;
|
||
7 years ago
|
use std::fmt;
|
||
|
|
||
4 years ago
|
/// An owned RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
|
||
6 years ago
|
///
|
||
4 years ago
|
/// The default string formatter is returning an N-Triples, Turtle and SPARQL compatible representation:
|
||
6 years ago
|
/// ```
|
||
3 years ago
|
/// use oxrdf::NamedNode;
|
||
6 years ago
|
///
|
||
|
/// assert_eq!(
|
||
|
/// "<http://example.com/foo>",
|
||
4 years ago
|
/// NamedNode::new("http://example.com/foo")?.to_string()
|
||
|
/// );
|
||
3 years ago
|
/// # Result::<_,oxrdf::IriParseError>::Ok(())
|
||
6 years ago
|
/// ```
|
||
7 years ago
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
|
||
|
pub struct NamedNode {
|
||
5 years ago
|
iri: String,
|
||
7 years ago
|
}
|
||
|
|
||
|
impl NamedNode {
|
||
4 years ago
|
/// Builds and validate an RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
|
||
4 years ago
|
pub fn new(iri: impl Into<String>) -> Result<Self, IriParseError> {
|
||
5 years ago
|
Ok(Self::new_from_iri(Iri::parse(iri.into())?))
|
||
|
}
|
||
|
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
pub(crate) fn new_from_iri(iri: Iri<String>) -> Self {
|
||
5 years ago
|
Self::new_unchecked(iri.into_inner())
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
/// Builds an RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) from a string.
|
||
5 years ago
|
///
|
||
5 years ago
|
/// It is the caller's responsibility to ensure that `iri` is a valid IRI.
|
||
5 years ago
|
///
|
||
4 years ago
|
/// [`NamedNode::new()`] is a safe version of this constructor and should be used for untrusted data.
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
pub fn new_unchecked(iri: impl Into<String>) -> Self {
|
||
5 years ago
|
Self { iri: iri.into() }
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
#[inline]
|
||
6 years ago
|
pub fn as_str(&self) -> &str {
|
||
7 years ago
|
self.iri.as_str()
|
||
|
}
|
||
|
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
pub fn into_string(self) -> String {
|
||
|
self.iri
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn as_ref(&self) -> NamedNodeRef<'_> {
|
||
|
NamedNodeRef::new_unchecked(&self.iri)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
impl fmt::Display for NamedNode {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
|
self.as_ref().fmt(f)
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
impl PartialEq<str> for NamedNode {
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
fn eq(&self, other: &str) -> bool {
|
||
|
self.as_str() == other
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<NamedNode> for str {
|
||
4 years ago
|
#[inline]
|
||
5 years ago
|
fn eq(&self, other: &NamedNode) -> bool {
|
||
|
self == other.as_str()
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
impl PartialEq<&str> for NamedNode {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &&str) -> bool {
|
||
|
self == *other
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<NamedNode> for &str {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &NamedNode) -> bool {
|
||
|
*self == other
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
/// A borrowed RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
|
||
|
///
|
||
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::NamedNodeRef;
|
||
4 years ago
|
///
|
||
|
/// assert_eq!(
|
||
|
/// "<http://example.com/foo>",
|
||
|
/// NamedNodeRef::new("http://example.com/foo")?.to_string()
|
||
|
/// );
|
||
3 years ago
|
/// # Result::<_,oxrdf::IriParseError>::Ok(())
|
||
4 years ago
|
/// ```
|
||
|
#[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<Self, IriParseError> {
|
||
|
Ok(Self::new_from_iri(Iri::parse(iri)?))
|
||
|
}
|
||
|
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
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.
|
||
|
///
|
||
4 years ago
|
/// [`NamedNode::new()`] is a safe version of this constructor and should be used for untrusted data.
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub const fn new_unchecked(iri: &'a str) -> Self {
|
||
|
Self { iri }
|
||
|
}
|
||
|
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub const fn as_str(self) -> &'a str {
|
||
|
self.iri
|
||
|
}
|
||
|
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
pub fn into_owned(self) -> NamedNode {
|
||
|
NamedNode::new_unchecked(self.iri)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for NamedNodeRef<'_> {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
3 years ago
|
rio::NamedNode { iri: self.as_str() }.fmt(f)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl From<NamedNodeRef<'_>> for NamedNode {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
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 PartialEq<NamedNode> for NamedNodeRef<'_> {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &NamedNode) -> bool {
|
||
|
self.as_str() == other.as_str()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<NamedNodeRef<'_>> for NamedNode {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &NamedNodeRef<'_>) -> bool {
|
||
|
self.as_str() == other.as_str()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<str> for NamedNodeRef<'_> {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &str) -> bool {
|
||
|
self.as_str() == other
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<NamedNodeRef<'_>> for str {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &NamedNodeRef<'_>) -> bool {
|
||
|
self == other.as_str()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<&str> for NamedNodeRef<'_> {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &&str) -> bool {
|
||
|
self == *other
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl PartialEq<NamedNodeRef<'_>> for &str {
|
||
4 years ago
|
#[inline]
|
||
4 years ago
|
fn eq(&self, other: &NamedNodeRef<'_>) -> bool {
|
||
|
*self == other
|
||
|
}
|
||
|
}
|