rstmt_core/
error.rs

1/*
2    Appellation: error <module>
3    Contrib: @FL03
4*/
5//! this module defines the [`Error`] type and provides a type alias for a
6//! [`Result`](core::result::Result) with an [`Error`].
7#[cfg(feature = "alloc")]
8use alloc::{boxed::Box, string::String};
9
10/// a type alias for a [`Result`](core::result::Result) with a [`Error`] type
11pub type Result<T = ()> = core::result::Result<T, Error>;
12
13/// The [`Error`] enum represents various errors that can occur in the application.
14#[derive(Debug, thiserror::Error)]
15pub enum Error {
16    #[error("Invalid Chord")]
17    InvalidChord,
18    #[cfg(feature = "alloc")]
19    #[error("Invalid Intervals: {0}")]
20    InvalidIntervals(String),
21    #[error("Invalid Note")]
22    InvalidNote,
23    #[cfg(feature = "anyhow")]
24    #[error(transparent)]
25    AnyError(#[from] anyhow::Error),
26    #[error(transparent)]
27    FmtError(#[from] core::fmt::Error),
28    #[cfg(feature = "alloc")]
29    #[error(transparent)]
30    BoxError(#[from] Box<dyn core::error::Error + Send + Sync>),
31    #[cfg(feature = "std")]
32    #[error(transparent)]
33    IOError(#[from] std::io::Error),
34    #[error(transparent)]
35    #[cfg(feature = "serde_json")]
36    JsonError(#[from] serde_json::Error),
37    #[cfg(feature = "alloc")]
38    #[error("Unknown Error: {0}")]
39    Unknown(String),
40}
41
42#[cfg(feature = "alloc")]
43impl From<&str> for Error {
44    fn from(s: &str) -> Self {
45        Error::Unknown(String::from(s))
46    }
47}
48
49#[cfg(feature = "alloc")]
50impl From<String> for Error {
51    fn from(s: String) -> Self {
52        Error::Unknown(s)
53    }
54}