justcsv/
error.rs

1use derive_more::From;
2
3/// Crate `Result` type alias
4pub type Result<T> = core::result::Result<T, Error>;
5
6/// Crate error type
7#[derive(Debug, From)]
8pub enum Error {
9    /// Any error with description
10    #[from]
11    Custom(String),
12    // -- Externals
13    /// Some IO fail
14    #[from]
15    Io(std::io::Error),
16    /// `nom` parsing fail
17    #[from]
18    Nom(nom::error::Error<&'static str>),
19    // -- Internals
20    /// Basically is a normal completion
21    StreamComplete,
22    /// Stream ended but no correct CSV parsed
23    UnexpectedEof,
24    /// User calls for headers to write but there were some records written into stream
25    WriteHeadersAfterRecords,
26    /// Owned variant of a `nom` error
27    NomFailed(String),
28}
29
30impl Error {
31    /// Create crate `Error` from anything implementing `Display`
32    pub fn custom(val: impl std::fmt::Display) -> Self {
33        Self::Custom(val.to_string())
34    }
35}
36
37impl From<&str> for Error {
38    fn from(val: &str) -> Self {
39        Self::Custom(val.to_string())
40    }
41}
42
43impl core::fmt::Display for Error {
44    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
45        write!(fmt, "{self:?}")
46    }
47}
48
49impl std::error::Error for Error {}