tmf_client/common/
tmf_error.rs

1
2//! Error Module
3
4use thiserror::Error;
5
6/// TMF Error
7#[derive(Debug,Error)]
8pub enum TMFError {
9    /// NoConnection Error when making a request to the TMF API
10    #[error("TMFError: {0}")]
11    NoConnection(String),
12    /// Unknown Error when making a request to the TMF API
13    #[error("TMFError: {0}")]
14    Unknown(String),
15    /// Serialization Error when serializing or deserializing a TMF object
16    #[error("TMFError: {0}")]
17    Serialization(String)
18}
19
20impl From<&str> for TMFError {
21    fn from(value: &str) -> Self {
22        TMFError::Unknown(value.to_string())
23    }    
24}
25
26impl From<reqwest::Error> for TMFError {
27    fn from(value: reqwest::Error) -> Self {
28        TMFError::NoConnection(value.to_string())
29    }
30}
31
32impl From<TMFError> for String {
33    fn from(value: TMFError) -> Self {
34        value.to_string()
35    }
36}
37
38impl From<serde_json::Error> for TMFError {
39    fn from(value: serde_json::Error) -> Self {
40        TMFError::Serialization(value.to_string())
41    }
42}
43
44impl From<std::io::Error> for TMFError {
45    fn from(value: std::io::Error) -> Self {
46        TMFError::NoConnection(value.to_string())
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    #[test]
54    fn test_error_from_str() {
55        let err = TMFError::from("ThisIsAnError");
56
57        assert_eq!(err.to_string(),"TMFError: ThisIsAnError".to_string());
58    }
59
60    #[test]
61    fn test_string_from_error() {
62        let err= TMFError::from("ThisIsAnError");
63
64        let s : String = err.into();
65        assert_eq!(s,"TMFError: ThisIsAnError".to_string());
66    }
67}