use crate::parsers::error::ExpectedError;
use crate::parsers::NumericError;
use crate::ParserError;
#[derive(Clone, Debug, thiserror::Error, Eq, PartialEq)]
pub enum ModularParserError {
#[error("Expected end of input after parsing third version number component, but got: '{}'", char::from(*.got))]
ExpectedEndOfInput {
got: u8,
},
#[error(
"Expected the dot-separator '.', but got '{}'",
.got.map(|c| String::from(char::from(c))).unwrap_or_else(|| "EOI".to_string()),
)]
ExpectedSeparator {
got: Option<u8>,
},
#[error(
"Expected 0-9, but got '{}'",
.got.map(|c| String::from(char::from(c))).unwrap_or_else(|| "EOI".to_string()),
)]
ExpectedNumericToken {
got: Option<u8>,
},
#[error(transparent)]
NumberError(#[from] NumberError),
}
#[derive(Clone, Debug, thiserror::Error, Eq, PartialEq)]
pub enum NumberError {
#[error("Number may not start with a leading zero, unless the complete component is '0'")]
LeadingZero,
#[error("Overflow: Found number component which would be larger than the maximum supported number (max={})", u64::MAX)]
Overflow,
}
impl From<ModularParserError> for ParserError {
fn from(value: ModularParserError) -> Self {
match value {
ModularParserError::ExpectedEndOfInput { got } => {
ParserError::Expected(ExpectedError::EndOfInput {
at: None,
got: char::from(got),
})
}
ModularParserError::ExpectedNumericToken { got } => {
ParserError::Expected(ExpectedError::Numeric {
at: None,
got: got.map(char::from),
})
}
ModularParserError::ExpectedSeparator { got } => {
ParserError::Expected(ExpectedError::Separator {
at: None,
got: got.map(char::from),
})
}
ModularParserError::NumberError(e) => match e {
NumberError::LeadingZero => ParserError::Numeric(NumericError::LeadingZero),
NumberError::Overflow => ParserError::Numeric(NumericError::Overflow),
},
}
}
}