easy_configuration_format/
errors.rs1#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3pub struct ParseEntryError {
4 pub line: usize,
6 pub message: String,
8}
9
10impl ParseEntryError {
11 pub(crate) fn new(raw_line: usize, message: impl Into<String>) -> Self {
12 Self {
13 line: raw_line + 1,
14 message: message.into(),
15 }
16 }
17}
18
19impl std::error::Error for ParseEntryError {}
20
21impl std::fmt::Display for ParseEntryError {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 write!(f, "Invalid configuration entry at line {}: {}", self.line, self.message)
24 }
25}
26
27
28
29#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct FormatEntryError {
34 pub missing_key: String,
36}
37
38impl FormatEntryError {
39 pub(crate) fn new(missing_key: impl Into<String>) -> Self {
40 Self {
41 missing_key: missing_key.into(),
42 }
43 }
44}
45
46impl std::error::Error for FormatEntryError {}
47
48impl std::fmt::Display for FormatEntryError {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 write!(f, "Failed to format configuration entry, no value found for key {}", self.missing_key)
51 }
52}
53
54
55
56#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
58pub enum RetrieveSettingError {
59 Missing {
61 key: String,
63 },
64 WrongSingularType {
66 key: String,
68 expected: String,
70 encountered: String,
72 },
73 WrongMultipleType {
75 key: String,
77 expected: Vec<String>,
79 encountered: String,
81 },
82}
83
84impl RetrieveSettingError {
85 pub(crate) fn new_missing(key: impl Into<String>) -> Self {
86 Self::Missing { key: key.into() }
87 }
88 pub(crate) fn new_wrong_singular_type(key: impl Into<String>, expected: impl Into<String>, encountered: impl Into<String>) -> Self {
89 Self::WrongSingularType { key: key.into(), expected: expected.into(), encountered: encountered.into() }
90 }
91 pub(crate) fn new_wrong_multiple_type(key: impl Into<String>, expected: Vec<String>, encountered: impl Into<String>) -> Self {
92 Self::WrongMultipleType { key: key.into(), expected, encountered: encountered.into() }
93 }
94}
95
96impl std::error::Error for RetrieveSettingError {}
97
98impl std::fmt::Display for RetrieveSettingError {
99 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100 match self {
101 Self::Missing { key } => write!(f, "Setting '{key}' does not exist"),
102 Self::WrongSingularType { key, expected, encountered } => write!(f, "Setting '{key}' was expected to be of type '{expected}', but is of type '{encountered}'"),
103 Self::WrongMultipleType { key, expected, encountered } => {
104 write!(f, "Setting '{key}' was expected to be of type ")?;
105 match expected.len() {
106 0 => unreachable!(),
107 1 => write!(f, "{}", expected[0])?,
108 2 => write!(f, "{} or {}", expected[0], expected[1])?,
109 n => {
110 for expected_type in expected.iter().take(n - 1) {
111 write!(f, "{expected_type}, ")?;
112 }
113 write!(f, "or {}", expected[n - 1])?;
114 }
115 }
116 write!(f, " but found type '{encountered}'")?;
117 Ok(())
118 }
119 }
120 }
121}