1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// error_mod.rs

//! Error library for this crate using thiserror
//!
//! I am using the crate thiserror to create an enum for all library errors.  
//! It mostly forwards the source "from" error.  
//! The library never writes to the screen, because it contains only the logic.  
//! Is the bin project that knows if it is CLI, TUI or GUI and it presents the errors to the user and developer.  
//! Then in the bin project I use the crate anyhow.  

/// Enum of possible errors from this library
#[derive(thiserror::Error, Debug)]
pub enum LibError {
    #[error("SerdeJsonError: {0}")]
    SerdeJsonError(#[from] serde_json::Error),

    #[error("InfallibleError: {0}")]
    InfallibleError(#[from] std::convert::Infallible),

    #[error("StdIoError: {0}")]
    StdIoError(#[from] std::io::Error),

    #[error("ParseIntError: {0}")]
    ParseIntError(#[from] std::num::ParseIntError),

    #[error("{0}")]
    ErrorFromString(String),

    #[error("{0}")]
    ErrorFromStr(&'static str),
    //#[error("unknown error")]
    //UnknownError,
}

/// Result type alias with fixed LibError using thiserror
///
/// It makes simpler to write returns from functions.
pub type ResultWithLibError<T, E = LibError> = core::result::Result<T, E>;