sophia_api/ns/_namespace.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
use super::*;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
/// A custom namespace.
///
/// The [`get`](Namespace::get) method can be used to create a new IRI by concatenating a suffix to this namespace's IRI.
pub struct Namespace<T: Borrow<str>>(IriRef<T>);
impl<T: Borrow<str>> Namespace<T> {
/// Build a custom namespace based on the given IRI.
///
/// `iri` must be a valid IRI, otherwise this constructor returns an error.
pub fn new(iri: T) -> Result<Self, InvalidIri> {
IriRef::new(iri).map(Namespace)
}
/// Build a custom namespace based on the given IRI,
/// without checking that it is valid.
/// If it is not, it may result in undefined behaviour.
pub fn new_unchecked(iri: T) -> Self {
Namespace(IriRef::new_unchecked(iri))
}
/// Build an IRI by appending `suffix` to this namespace.
///
/// Return an error if the concatenation produces an invalid IRI.
pub fn get<'s>(&'s self, suffix: &'s str) -> Result<NsTerm<'s>, InvalidIri> {
let ns_term = NsTerm {
ns: self.0.as_ref(),
suffix,
};
IriRef::new(ns_term.to_string())?;
Ok(ns_term)
}
/// Build an IRI by appending `suffix` to this namespace,
/// without checking that the resulting IRI is valid.
/// If it is not, it may result in undefined behaviour.
pub fn get_unchecked<'s>(&'s self, suffix: &'s str) -> NsTerm<'s> {
NsTerm {
ns: self.0.as_ref(),
suffix,
}
}
/// Consume this Namespace and return the inner [`IriRef`].
pub fn inner(self) -> IriRef<T> {
self.0
}
}
impl Namespace<&'static str> {
/// `const` constructor for [`Namespace`]
///
/// # Precondition
/// `iri` must be a valid IRI reference (possibly relative),
/// otherwise undefined behaviour may occur.
pub const fn new_unchecked_const(iri: &'static str) -> Self {
Namespace(IriRef::new_unchecked_const(iri))
}
}
impl<T: Borrow<str>> From<IriRef<T>> for Namespace<T> {
fn from(other: IriRef<T>) -> Self {
Namespace(other)
}
}
impl<T: Borrow<str>> std::ops::Deref for Namespace<T> {
type Target = IriRef<T>;
fn deref(&self) -> &IriRef<T> {
&self.0
}
}