use core::fmt;
use crate::escape::{EscapeError, split, unescape};
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum TokenError {
#[error(transparent)]
Escape(#[from] EscapeError),
#[error("the token names neither a system nor a code")]
Empty,
#[error("the token carries more than one unescaped `|`")]
Bars,
#[error("an :of-type value is not system|code|value")]
OfTypeParts,
#[error("an :of-type value leaves a part empty")]
OfTypeEmpty,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Token {
Code(String),
SystemCode {
system: String,
code: String,
},
NoSystem(String),
System(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum System<'token> {
Any,
Absent,
Is(&'token str),
}
impl Token {
pub fn parse(text: &str) -> Result<Self, TokenError> {
let parts = split(text, '|');
let token = match parts.as_slice() {
[code] => Self::Code(unescape(code)?),
[system, code] => {
let system = unescape(system)?;
let code = unescape(code)?;
match (system.is_empty(), code.is_empty()) {
(true, true) => return Err(TokenError::Empty),
(true, false) => Self::NoSystem(code),
(false, true) => Self::System(system),
(false, false) => Self::SystemCode { system, code },
}
}
_ => return Err(TokenError::Bars),
};
if matches!(&token, Self::Code(code) if code.is_empty()) {
return Err(TokenError::Empty);
}
Ok(token)
}
#[must_use]
pub fn system(&self) -> System<'_> {
match self {
Self::Code(_) => System::Any,
Self::NoSystem(_) => System::Absent,
Self::SystemCode { system, .. } | Self::System(system) => System::Is(system),
}
}
#[must_use]
pub fn code(&self) -> Option<&str> {
match self {
Self::Code(code) | Self::NoSystem(code) | Self::SystemCode { code, .. } => Some(code),
Self::System(_) => None,
}
}
}
impl fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Code(_) => f.debug_struct("Code").finish_non_exhaustive(),
Self::SystemCode { system, .. } => f
.debug_struct("SystemCode")
.field("system", system)
.finish_non_exhaustive(),
Self::NoSystem(_) => f.debug_struct("NoSystem").finish_non_exhaustive(),
Self::System(system) => f.debug_tuple("System").field(system).finish(),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct OfType {
system: String,
code: String,
value: String,
}
impl OfType {
pub fn parse(text: &str) -> Result<Self, TokenError> {
let parts = split(text, '|');
let [system, code, value] = parts.as_slice() else {
return Err(TokenError::OfTypeParts);
};
let of_type = Self {
system: unescape(system)?,
code: unescape(code)?,
value: unescape(value)?,
};
if of_type.system.is_empty() || of_type.code.is_empty() || of_type.value.is_empty() {
return Err(TokenError::OfTypeEmpty);
}
Ok(of_type)
}
#[must_use]
pub fn system(&self) -> &str {
&self.system
}
#[must_use]
pub fn code(&self) -> &str {
&self.code
}
#[must_use]
pub fn value(&self) -> &str {
&self.value
}
}
impl fmt::Debug for OfType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OfType")
.field("system", &self.system)
.field("code", &self.code)
.finish_non_exhaustive()
}
}