narinfo-rs/src/error.rs

27 lines
812 B
Rust

#[derive(Debug)]
/// The error type returned by all the parsing functions in this crate.
pub enum ParsingError<'a> {
InvalidIntValue { line: &'a str },
UnknownKey { key: &'a str },
InvalidLine { line: &'a str },
InvalidSignature(&'a str),
MissingField(&'static str),
}
impl From<derive_builder::UninitializedFieldError> for ParsingError<'static> {
fn from(e: derive_builder::UninitializedFieldError) -> Self {
Self::MissingField(e.field_name())
}
}
impl<'a> ParsingError<'a> {
pub(crate) fn try_parse_int<'b>(value: &'b str, line: &'a str) -> ParsingResult<'a, usize> {
value
.parse()
.map_err(|_| ParsingError::InvalidIntValue { line })
}
}
/// The result type returned by all the parsing functions in this crate.
pub type ParsingResult<'a, T> = core::result::Result<T, ParsingError<'a>>;