reda_lib/
error.rs

1use std::num::ParseFloatError;
2
3
4#[derive(Debug, thiserror::Error)]
5pub enum LibError {
6    #[error("except group")]
7    ExceptLibGroup,
8
9    #[error("library param should only a name")]
10    LibraryParamLenUnmatch,
11
12    #[error("expect attr {0} but not found")]
13    MissAttr(String),
14
15    #[error("unsupport direction literal {0}")]
16    UnsupportDirection(String),
17
18    #[error(transparent)]
19    Io(#[from] std::io::Error),
20
21    #[error(transparent)]
22    ParseFloat(ParseFloatError),
23
24    #[error("parse error: {0}")]
25    Parse(String),
26
27    #[error("value kind unmatch, expect {0} but got {1}")]
28    ValueKindUnmatch(&'static str, &'static str),
29
30    #[error("{0} >> {1}")]
31    Context(String, Box<LibError>),
32}
33
34pub type LibResult<T> = Result<T, LibError>;
35
36pub trait LibErrorContext {
37    fn context<S: Into<String>>(self, f: impl Fn() -> S) -> Self;
38}
39
40impl<T> LibErrorContext for Result<T, LibError> {
41    fn context<S: Into<String>>(self, f: impl Fn() -> S) -> Self {
42        self.map_err(|e| LibError::Context(f().into(), Box::new(e)))
43    }
44}