use std::{
error::Error,
fmt::{self, Display, Formatter},
};
use fluent_uri::UriRef;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UriReference(String);
impl UriReference {
pub fn new(value: impl Into<String>) -> Result<Self, UriReferenceError> {
let value = value.into();
if value.is_empty() {
return Err(UriReferenceError::Empty);
}
UriRef::parse(value.as_str()).map_err(|_| UriReferenceError::Invalid)?;
Ok(Self(value))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for UriReference {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Display for UriReference {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum UriReferenceError {
Empty,
Invalid,
}
impl Display for UriReferenceError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => formatter.write_str("URI reference must not be empty"),
Self::Invalid => formatter.write_str("value is not an RFC 3986 URI reference"),
}
}
}
impl Error for UriReferenceError {}