use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Prefix {
#[default]
Eq,
Ne,
Gt,
Lt,
Ge,
Le,
Sa,
Eb,
Ap,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum PrefixError {
#[error("the value opens with none of the prefixes eq, ne, gt, lt, ge, le, sa, eb and ap")]
Unknown,
}
impl Prefix {
pub const ALL: [Self; 9] = [
Self::Eq,
Self::Ne,
Self::Gt,
Self::Lt,
Self::Ge,
Self::Le,
Self::Sa,
Self::Eb,
Self::Ap,
];
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Self::Eq => "eq",
Self::Ne => "ne",
Self::Gt => "gt",
Self::Lt => "lt",
Self::Ge => "ge",
Self::Le => "le",
Self::Sa => "sa",
Self::Eb => "eb",
Self::Ap => "ap",
}
}
pub fn split(text: &str) -> Result<(Self, &str), PrefixError> {
for prefix in Self::ALL {
if let Some(rest) = text.strip_prefix(prefix.code()) {
return Ok((prefix, rest));
}
}
if text.starts_with(|character: char| character.is_ascii_alphabetic()) {
Err(PrefixError::Unknown)
} else {
Ok((Self::Eq, text))
}
}
}
impl fmt::Display for Prefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.code())
}
}