mecomp_core/
errors.rs

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use mecomp_storage::errors::Error;
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Errors that can occur with finding the config or data directories.
#[derive(Error, Debug)]
pub enum DirectoryError {
    #[error("Unable to find the config directory for mecomp.")]
    Config,
    #[error("Unable to find the data directory for mecomp.")]
    Data,
}

/// Errors that can occur with the library.
#[derive(Error, Debug)]
pub enum LibraryError {
    #[error("Database error: {0}")]
    Database(#[from] Error),
    #[error("IO error: {0}")]
    IO(#[from] std::io::Error),
    #[error("Decoder error: {0}")]
    #[cfg(feature = "audio")]
    Decoder(#[from] rodio::decoder::DecoderError),
}

#[derive(Error, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub enum SerializableLibraryError {
    #[error("Database error: {0}")]
    Database(String),
    #[error("IO error: {0}")]
    IO(String),
    #[error("Decoder error: {0}")]
    Decoder(String),
    #[error("Library Rescan already in progress.")]
    RescanInProgress,
    #[error("Library Analysis already in progress.")]
    AnalysisInProgress,
    #[error("Collection Reclustering already in progress.")]
    ReclusterInProgress,
}

impl From<Error> for SerializableLibraryError {
    fn from(e: Error) -> Self {
        Self::Database(e.to_string())
    }
}

impl From<std::io::Error> for SerializableLibraryError {
    fn from(e: std::io::Error) -> Self {
        Self::IO(e.to_string())
    }
}

#[cfg(feature = "audio")]
impl From<rodio::decoder::DecoderError> for SerializableLibraryError {
    fn from(e: rodio::decoder::DecoderError) -> Self {
        Self::Decoder(e.to_string())
    }
}

impl From<LibraryError> for SerializableLibraryError {
    fn from(e: LibraryError) -> Self {
        match e {
            LibraryError::Database(e) => Self::Database(e.to_string()),
            LibraryError::IO(e) => Self::IO(e.to_string()),
            #[cfg(feature = "audio")]
            LibraryError::Decoder(e) => Self::Decoder(e.to_string()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_str_eq;
    use rstest::rstest;

    #[rstest]
    #[case(
        LibraryError::from(Error::NoId),
        "Database error: Item is missing an Id."
    )]
    #[case(
        LibraryError::from(std::io::Error::new(std::io::ErrorKind::Other, "test")),
        "IO error: test"
    )]
    #[case(
        LibraryError::from(rodio::decoder::DecoderError::DecodeError("test")),
        "Decoder error: test"
    )]
    fn test_serializable_library_error(#[case] input: LibraryError, #[case] expected: String) {
        let actual = SerializableLibraryError::from(input).to_string();
        assert_str_eq!(actual, expected);
    }

    #[rstest]
    #[case(Error::NoId, LibraryError::Database(Error::NoId).into())]
    #[case(std::io::Error::new(std::io::ErrorKind::Other, "test"), LibraryError::IO(std::io::Error::new(std::io::ErrorKind::Other, "test")).into())]
    #[case(rodio::decoder::DecoderError::DecodeError("test"), LibraryError::Decoder(rodio::decoder::DecoderError::DecodeError("test")).into())]
    fn test_serializable_library_error_from<T: Into<SerializableLibraryError>>(
        #[case] from: T,
        #[case] to: SerializableLibraryError,
    ) {
        let actual = from.into();
        assert_eq!(actual, to);
    }
}