1use crate::Severity;
2use crate::Severity::{Error, Info, Warning};
3use colored::Colorize;
4use std::fmt::{Display, Formatter};
5
6#[derive(Clone, Debug)]
14pub struct Code {
15 severity: Severity,
16 code: String,
17 message: String,
18}
19
20impl Code {
21 pub fn new(severity: Severity, code: String, message: String) -> Self {
25 Self {
26 severity,
27 code,
28 message,
29 }
30 }
31
32 pub fn from<S, S0, S1>(severity: S, code: S0, message: S1) -> Self
34 where
35 S: Into<Severity>,
36 S0: Into<String>,
37 S1: Into<String>,
38 {
39 let severity: Severity = severity.into();
40 let code: String = code.into();
41 let message: String = message.into();
42 Self::new(severity, code, message)
43 }
44
45 pub fn error<S0, S1>(code: S0, message: S1) -> Self
47 where
48 S0: Into<String>,
49 S1: Into<String>,
50 {
51 Self::from(Error, code, message)
52 }
53
54 pub fn warning<S0, S1>(code: S0, message: S1) -> Self
56 where
57 S0: Into<String>,
58 S1: Into<String>,
59 {
60 Self::from(Warning, code, message)
61 }
62
63 pub fn info<S0, S1>(code: S0, message: S1) -> Self
65 where
66 S0: Into<String>,
67 S1: Into<String>,
68 {
69 Self::from(Info, code, message)
70 }
71}
72
73impl Code {
74 pub fn severity(&self) -> Severity {
78 self.severity
79 }
80
81 pub fn code(&self) -> &str {
83 self.code.as_str()
84 }
85
86 pub fn message(&self) -> &str {
88 self.message.as_str()
89 }
90}
91
92impl Display for Code {
93 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
94 write!(
95 f,
96 "{}{}{}{}{}",
97 self.severity,
98 "[".color(self.severity.color()),
99 self.code.color(self.severity.color()),
100 "]: ".color(self.severity.color()),
101 self.message.bright_white().bold()
102 )
103 }
104}