use fluent_uri::{
pct_enc::{encoder::Fragment, EStr, EString},
Uri, UriRef,
};
use std::sync::LazyLock;
use crate::Error;
pub use fluent_uri::pct_enc::encoder::Path;
pub fn resolve_against(base: &Uri<&str>, uri: &str) -> Result<Uri<String>, Error> {
if uri.starts_with('#') && base.as_str().ends_with(uri) {
return Ok(base.to_owned());
}
let without_fragment;
let base = if base.has_fragment() {
without_fragment = base.strip_fragment();
&without_fragment
} else {
base
};
Ok(UriRef::parse(uri)
.map_err(|error| Error::uri_reference_parsing_error(uri, error))?
.resolve_against(base)
.map_err(|error| Error::uri_resolving_error(uri, *base, error))?
.normalize())
}
pub fn from_str(uri: &str) -> Result<Uri<String>, Error> {
let uriref = UriRef::parse(uri)
.map_err(|error| Error::uri_reference_parsing_error(uri, error))?
.normalize();
if uriref.has_scheme() {
Ok(Uri::try_from(uriref.as_str())
.map_err(|error| Error::uri_parsing_error(uriref.as_str(), error))?
.into())
} else {
Ok(uriref
.resolve_against(&DEFAULT_ROOT_URI.borrow())
.map_err(|error| Error::uri_resolving_error(uri, DEFAULT_ROOT_URI.borrow(), error))?)
}
}
pub(crate) static DEFAULT_ROOT_URI: LazyLock<Uri<String>> =
LazyLock::new(|| Uri::parse("json-schema:///".to_string()).expect("Invalid URI"));
pub type EncodedString = EStr<Fragment>;
pub type EncodedBuffer = EString<Fragment>;