sms_2dm/
error.rs

1use std::num::ParseIntError;
2
3use num_traits::ParseFloatError;
4
5/// Errors encountered by sms-2dm.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    #[error("The card `{0}` is expected but missing")]
9    MissingCard(String),
10    #[error("The card `{0}` is seen more times than expected")]
11    ExtraneousCard(String),
12    #[error("An empty line is encountered")]
13    EmptyLine,
14    #[error("Expected a `{expect}` card but found a `{actual}` card")]
15    WrongCardTag { expect: String, actual: String },
16    #[error("{0}")]
17    InvalidInt(#[from] ParseIntError),
18    #[error("{0}")]
19    InvalidFloat(ParseFloatError),
20    #[error("A value was expected but missing")]
21    MissingValue,
22    #[error("An extraneous value was encountered: {0}")]
23    ExtraneousValue(String),
24}
25impl From<ParseFloatError> for Error {
26    fn from(err: ParseFloatError) -> Self {
27        Self::InvalidFloat(err)
28    }
29}
30
31/// Handle a weak error, depending on configured features.
32///
33/// - If `warn` feature is enabled, this function will log the error.
34/// - If `strict` feature is enabled, this function will return the error.
35pub(crate) fn weak_error(err: Error) -> Result<(), Error> {
36    #[cfg(feature = "warn")]
37    log::warn!("{err}");
38
39    if cfg!(feature = "strict") {
40        Err(err)
41    } else {
42        Ok(())
43    }
44}