use super::{RelativeReference, Uri};
use iri_string::{
format::ToDedicatedString,
types::{IriReferenceStr, UriReferenceString},
};
pub struct UriReference(UriReferenceString);
impl UriReference {
pub fn to_uri(&self) -> Result<Uri, RelativeReference> {
self.0
.to_iri()
.map(Uri::from)
.map_err(RelativeReference::from)
}
}
impl From<&RelativeReference> for UriReference {
fn from(value: &RelativeReference) -> Self {
Self(value.0.to_owned().into())
}
}
impl From<&Uri> for UriReference {
fn from(value: &Uri) -> Self {
Self(value.0.to_owned().into())
}
}
impl TryFrom<&str> for UriReference {
type Error = String;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let iri_reference = IriReferenceStr::new(value).or(Err(format!(
"String '{value}' is not a valid IRI Reference."
)))?;
Ok(Self(iri_reference.encode_to_uri().to_dedicated_string()))
}
}