use std::{convert::Infallible, error, fmt};
use aliri_braid::braid;
#[derive(Debug)]
pub enum InvalidScopeToken {
EmptyString,
InvalidCharacter { position: usize, value: u8 },
}
impl fmt::Display for InvalidScopeToken {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::EmptyString => f.write_str("scope cannot be empty"),
Self::InvalidCharacter { position, value } => f.write_fmt(format_args!(
"invalid scope character at position {}: {:02x}",
position, value
)),
}
}
}
impl From<Infallible> for InvalidScopeToken {
#[inline(always)]
fn from(x: Infallible) -> Self {
match x {}
}
}
impl error::Error for InvalidScopeToken {}
#[braid(serde, validator, ref_doc = "A borrowed reference to a [`ScopeToken`]")]
pub struct ScopeToken;
impl aliri_braid::Validator for ScopeToken {
type Error = InvalidScopeToken;
fn validate(s: &str) -> Result<(), Self::Error> {
if s.is_empty() {
Err(InvalidScopeToken::EmptyString)
} else if let Some((position, &value)) = s
.as_bytes()
.iter()
.enumerate()
.find(|(_, &b)| b <= 0x20 || b == 0x22 || b == 0x5C || 0x7F <= b)
{
Err(InvalidScopeToken::InvalidCharacter { position, value })
} else {
Ok(())
}
}
}