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.
71 lines
1.6 KiB
71 lines
1.6 KiB
7 years ago
|
use std::fmt;
|
||
|
use std::str::FromStr;
|
||
|
use std::sync::Arc;
|
||
|
use url::Url;
|
||
6 years ago
|
use Error;
|
||
|
use Result;
|
||
7 years ago
|
|
||
|
/// A RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
|
||
6 years ago
|
///
|
||
|
/// The common way to build it is to use the `FromStr::from_str` trait method.
|
||
|
/// This method takes care of usual IRI normalization and validation.
|
||
|
///
|
||
|
/// The default string formatter is returning a N-Triples, Turtle and SPARQL compatible representation:
|
||
|
/// ```
|
||
|
/// use rudf::model::NamedNode;
|
||
|
/// use std::str::FromStr;
|
||
|
///
|
||
|
/// assert_eq!(
|
||
|
/// "<http://example.com/foo>",
|
||
|
/// NamedNode::from_str("http://example.com/foo").unwrap().to_string()
|
||
|
/// )
|
||
|
/// ```
|
||
|
///
|
||
7 years ago
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
|
||
|
pub struct NamedNode {
|
||
|
iri: Arc<Url>,
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for NamedNode {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||
|
write!(f, "<{}>", self.iri)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl NamedNode {
|
||
|
/// Builds a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
|
||
|
pub fn new(iri: impl Into<Url>) -> Self {
|
||
|
Self {
|
||
|
iri: Arc::new(iri.into()),
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
pub fn as_str(&self) -> &str {
|
||
7 years ago
|
self.iri.as_str()
|
||
|
}
|
||
|
|
||
6 years ago
|
pub fn as_url(&self) -> &Url {
|
||
7 years ago
|
&self.iri
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<Url> for NamedNode {
|
||
|
fn from(url: Url) -> Self {
|
||
|
Self { iri: Arc::new(url) }
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
impl From<NamedNode> for Url {
|
||
|
fn from(named_node: NamedNode) -> Self {
|
||
|
Arc::try_unwrap(named_node.iri).unwrap_or_else(|iri| (*iri).clone())
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
impl FromStr for NamedNode {
|
||
6 years ago
|
type Err = Error;
|
||
7 years ago
|
|
||
6 years ago
|
fn from_str(s: &str) -> Result<Self> {
|
||
7 years ago
|
Ok(NamedNode::new(Url::parse(s)?))
|
||
|
}
|
||
|
}
|