sophia_iri/_trait.rs
1//! IRI related traits.
2use super::{Iri, IriRef};
3use std::borrow::Borrow;
4
5/// Marker trait guaranteeing that the underlying `str` is a valid IRI
6/// (i.e. absolute or relative, with an optional fragment identifier)
7pub trait IsIriRef: Borrow<str> {}
8
9/// Marker trait guaranteeing that the underlying `str` is a valid IRI-reference
10/// (i.e. absolute, with an optional fragment identifier)
11pub trait IsIri: IsIriRef {}
12
13/// Automatic trait for [`IsIriRef`], providing cheap conversion to [`IriRef`].
14pub trait AsIriRef {
15 /// Extract an [`IriRef`] wrapping the underlying `str`.
16 fn as_iri_ref(&self) -> IriRef<&str>;
17}
18
19impl<T: IsIriRef> AsIriRef for T {
20 fn as_iri_ref(&self) -> IriRef<&str> {
21 IriRef::new_unchecked(self.borrow())
22 }
23}
24
25/// Automatic trait for [`IsIri`], providing cheap conversion to [`Iri`].
26pub trait AsIri {
27 /// Extract an [`Iri`] wrapping the underlying `str`.
28 fn as_iri(&self) -> Iri<&str>;
29}
30
31impl<T: IsIri> AsIri for T {
32 fn as_iri(&self) -> Iri<&str> {
33 Iri::new_unchecked(self.borrow())
34 }
35}