Skip to main content

semver_common/models/
alert.rs

1use chrono::ParseError;
2use regex::Error as RegexError;
3use serde_json::Error as SerdeError;
4use std::{
5    convert::From, fmt::Display, io::Error as IOError, num::ParseIntError, string::FromUtf8Error,
6};
7
8/// Alert is a wrapper for all the various error types that may be returned by various
9/// crate functions.
10#[derive(Debug, Clone)]
11pub struct Alert {
12    val: String,
13}
14
15impl From<ParseError> for Alert {
16    /// chrono::ParseError
17    fn from(value: ParseError) -> Self {
18        Alert {
19            val: format!("chrono::ParseError: {}", value),
20        }
21    }
22}
23
24impl From<RegexError> for Alert {
25    /// regex::Error
26    fn from(value: RegexError) -> Self {
27        Alert {
28            val: format!("regex::Error: {}", value),
29        }
30    }
31}
32
33impl From<IOError> for Alert {
34    /// io::error
35    fn from(value: IOError) -> Self {
36        Alert {
37            val: format!("io::Error: {}", value),
38        }
39    }
40}
41
42impl From<ParseIntError> for Alert {
43    /// num::ParseIntError
44    fn from(value: ParseIntError) -> Self {
45        Alert {
46            val: format!("num::ParseIntError: {}", value),
47        }
48    }
49}
50
51impl From<SerdeError> for Alert {
52    /// serde_json::Error
53    fn from(value: SerdeError) -> Self {
54        Alert {
55            val: format!("serde_json::Error: {}", value),
56        }
57    }
58}
59
60impl From<FromUtf8Error> for Alert {
61    /// string::FromUtf8Error
62    fn from(value: FromUtf8Error) -> Self {
63        Alert {
64            val: format!("string::FromUtf8Error: {}", value),
65        }
66    }
67}
68
69impl From<&str> for Alert {
70    /// &str
71    fn from(value: &str) -> Self {
72        Alert {
73            val: String::from(value),
74        }
75    }
76}
77
78impl From<String> for Alert {
79    /// String
80    fn from(value: String) -> Self {
81        Alert { val: value }
82    }
83}
84
85impl From<&String> for Alert {
86    /// &String
87    fn from(value: &String) -> Self {
88        Alert { val: value.clone() }
89    }
90}
91
92impl Display for Alert {
93    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94        writeln!(f, "{}", self.val)
95    }
96}