rstmt_core/error/
err.rs

1/*
2    Appellation: err <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5
6///
7#[derive(
8    Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, strum::EnumIs, strum::VariantNames,
9)]
10#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
11
12pub enum Error {
13    InvalidInterval(String),
14    MusicError(String),
15    ParseError(String),
16    Unknown(String),
17}
18
19impl Error {
20    pub fn invalid_interval(msg: impl ToString) -> Self {
21        Self::InvalidInterval(msg.to_string())
22    }
23
24    pub fn music_error(msg: impl ToString) -> Self {
25        Self::MusicError(msg.to_string())
26    }
27
28    pub fn parse_error(msg: impl ToString) -> Self {
29        Self::ParseError(msg.to_string())
30    }
31
32    pub fn unknown_error(msg: impl ToString) -> Self {
33        Self::Unknown(msg.to_string())
34    }
35
36    pub fn kind(&self) -> &str {
37        match self {
38            Self::InvalidInterval(_) => "InvalidInterval",
39            Self::MusicError(_) => "MusicError",
40            Self::ParseError(_) => "ParseError",
41            Self::Unknown(_) => "Unknown",
42        }
43    }
44
45    pub fn msg(&self) -> &str {
46        match self {
47            Self::InvalidInterval(msg) => msg,
48            Self::MusicError(msg) => msg,
49            Self::ParseError(msg) => msg,
50            Self::Unknown(msg) => msg,
51        }
52    }
53}
54
55impl core::fmt::Display for Error {
56    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
57        write!(f, "{}: {}", self.kind(), self.msg())
58    }
59}
60
61#[cfg(feature = "std")]
62impl std::error::Error for Error {}