Skip to main content

semver_common/models/
alert.rs

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