use std::borrow::Cow;
use thiserror::Error;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LengthExpectation {
Exact(usize),
RangeInclusive {
min: usize,
max: usize,
},
}
impl std::fmt::Display for LengthExpectation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Exact(n) => write!(f, "exactly {n}"),
Self::RangeInclusive { min, max } => write!(f, "{min}..={max}"),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum IdentifierError {
#[error("invalid length: expected {expected}, got {actual}")]
InvalidLength {
expected: LengthExpectation,
actual: usize,
},
#[error("invalid character {character:?} at position {position}")]
InvalidCharacter {
position: usize,
character: char,
},
#[error("invalid checksum")]
InvalidChecksum,
#[error("invalid format: {description}")]
InvalidFormat {
description: Cow<'static, str>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("unknown enum value {value:?}: not a variant defined in this BO4E schema version")]
pub struct UnknownVariant {
pub value: String,
}
impl UnknownVariant {
pub fn new(value: impl Into<String>) -> Self {
Self {
value: value.into(),
}
}
}
#[cfg(feature = "validate")]
impl From<UnknownVariant> for garde::Error {
fn from(e: UnknownVariant) -> Self {
garde::Error::new(e.to_string())
}
}
#[cfg(feature = "validate")]
impl From<IdentifierError> for garde::Error {
fn from(e: IdentifierError) -> Self {
match e {
IdentifierError::InvalidChecksum => garde::Error::new("invalid checksum"),
other => garde::Error::new(other.to_string()),
}
}
}