use thiserror::Error;
#[must_use]
pub fn escape_control_chars(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for c in text.chars() {
let escape = match c {
'\t' | '\n' => false,
'\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}' => true,
c => c.is_control(),
};
if escape {
out.push_str(&format!("\\u{:04x}", u32::from(c)));
} else {
out.push(c);
}
}
out
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TeiError {
#[error("TEI is empty")]
Empty,
#[error(
"the legacy `urn:tei:` form is not supported; use `tei://<domain-name>/<type>/<unique-identifier>`"
)]
LegacyUrn,
#[error("TEI scheme must be the lowercase string `tei`")]
Scheme,
#[error("TEI must be `tei://<domain-name>/<type>/<unique-identifier>`")]
Structure,
#[error("TEI domain name is not an ASCII DNS name without a port")]
Domain,
#[error("TEI type must be lowercase ASCII letters and digits, starting with a letter")]
Type,
#[error("TEI unique identifier must be one path segment and must not contain `/`")]
Identifier,
#[error("TEI unique identifier contains an invalid percent-escape")]
PercentEncoding,
#[error("TEI identifier is not valid for type `{tei_type}`: {reason}")]
#[non_exhaustive]
Registry {
tei_type: String,
reason: String,
},
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ModelError {
#[error("JSON parse error: {}", escape_control_chars(.0))]
Json(String),
#[error("{}", escape_control_chars(.0))]
Invalid(String),
}
impl From<serde_json::Error> for ModelError {
fn from(value: serde_json::Error) -> Self {
Self::Json(value.to_string())
}
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PolicyError {
#[error("URL scheme is not allowed: {0}")]
Scheme(String),
#[error("URL must not contain userinfo")]
Userinfo,
#[error("URL must not contain a query or fragment")]
QueryOrFragment,
#[error("URL is missing a host")]
Host,
#[error("API base URL must not have a trailing slash")]
TrailingSlash,
#[error("destination address is not allowed: {0}")]
Destination(String),
#[error("hostname is not allowed: {0}")]
Hostname(String),
}