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("Attempted to use an invalid accidental")]
17    InvalidAccidental,
18    #[error("Attempted to name an invalid pitch class: {0}")]
19    InvalidPitchClass(isize),
20    #[error("Mismatched pitch classes, received {0} while expecting {1}")]
21    MismatchedPitchClasses(isize, isize),
22    #[error("Unable to parse the string into the configured type")]
23    FromStrParseError,
24    #[error("Unable to parse the string into the desired pitch class: {0}")]
25    InvalidPitchClassParse(&'static str),
26    #[error("Mismatched symbols: expected {0}, found {1}")]
27    MismatchedSymbols(char, char),
28    #[error("Invalid Chord")]
29    InvalidChord,
30    #[cfg(feature = "alloc")]
31    #[error("Invalid Intervals: {0}")]
32    IncompatibleIntervals(String),
33    #[error("Invalid Note")]
34    InvalidNote,
35    #[error(transparent)]
36    AnyError(#[from] anyhow::Error),
37    #[error("The impossible has occurred")]
38    Infallible(#[from] core::convert::Infallible),
39    #[error(transparent)]
40    FmtError(#[from] core::fmt::Error),
41    #[cfg(feature = "alloc")]
42    #[error(transparent)]
43    BoxError(#[from] Box<dyn core::error::Error + Send + Sync>),
44    #[cfg(feature = "std")]
45    #[error(transparent)]
46    IOError(#[from] std::io::Error),
47    #[error(transparent)]
48    #[cfg(feature = "serde_json")]
49    JsonError(#[from] serde_json::Error),
50    #[cfg(feature = "alloc")]
51    #[error("Unknown Error: {0}")]
52    Unknown(String),
53}
54
55#[cfg(feature = "alloc")]
56impl From<&str> for Error {
57    fn from(s: &str) -> Self {
58        Error::Unknown(String::from(s))
59    }
60}
61
62#[cfg(feature = "alloc")]
63impl From<String> for Error {
64    fn from(s: String) -> Self {
65        Error::Unknown(s)
66    }
67}