easy_configuration_format/
errors.rs

1/// Errors while parsing settings
2#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3pub struct ParseEntryError {
4	/// Line number of invalid entry (using 1-based indexing)
5	pub line: usize,
6	/// Error message / reason for being invalid
7	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/// Errors while formatting settings
30/// 
31/// Right now the only error that can occur is having a key specified in the layout that isn't defined in the settings hashmap
32#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct FormatEntryError {
34	/// Name of missing key
35	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/// Errors when trying to retrieve settings
57#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
58pub enum RetrieveSettingError {
59	/// Error for attempting to retrieve a non-existent setting key
60	Missing {
61		/// The key that was queried and is missing
62		key: String,
63	},
64	/// Error for attempting to retrieve a setting value as one type when it is storing another type
65	WrongSingularType {
66		/// The key that was queried
67		key: String,
68		/// The expected type of the key's value
69		expected: String,
70		/// The encountered type of the key's value
71		encountered: String,
72	},
73	/// Error for attempting to retrieve a setting value as a certain set of types when it is storing another type
74	WrongMultipleType {
75		/// The key that was queried
76		key: String,
77		/// The expected types of the key's value
78		expected: Vec<String>,
79		/// The encountered type of the key's value
80		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}