stratum_node_config/
error.rs1use std::fmt;
4
5use serde::ser::{Serialize, SerializeSeq, Serializer};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
9#[serde(rename_all = "snake_case")]
10#[non_exhaustive]
11pub enum ValidationErrorKind {
12 Syntax,
15 TooLarge,
18 MissingSchemaVersion,
20 UnsupportedSchemaVersion,
22 UnknownKey,
24 WrongType,
26 OutOfRange,
28 InvalidValue,
30 Conflict,
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize)]
39#[non_exhaustive]
40pub struct ValidationError {
41 kind: ValidationErrorKind,
42 path: Option<String>,
43 message: String,
44}
45
46impl ValidationError {
47 pub(crate) fn new(
48 kind: ValidationErrorKind,
49 path: Option<&str>,
50 message: impl Into<String>,
51 ) -> Self {
52 Self {
53 kind,
54 path: path.map(str::to_owned),
55 message: message.into(),
56 }
57 }
58
59 pub(crate) fn at(kind: ValidationErrorKind, path: &str, message: impl Into<String>) -> Self {
60 Self::new(kind, Some(path), message)
61 }
62
63 pub fn kind(&self) -> ValidationErrorKind {
65 self.kind
66 }
67
68 pub fn path(&self) -> Option<&str> {
71 self.path.as_deref()
72 }
73
74 pub fn message(&self) -> &str {
76 &self.message
77 }
78}
79
80impl fmt::Display for ValidationError {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 match &self.path {
83 Some(path) => write!(f, "{path}: {}", self.message),
84 None => f.write_str(&self.message),
85 }
86 }
87}
88
89impl std::error::Error for ValidationError {}
90
91#[derive(Debug, Clone, PartialEq, Eq, Hash)]
97#[non_exhaustive]
98pub struct ValidationErrors {
99 errors: Vec<ValidationError>,
100}
101
102impl ValidationErrors {
103 pub(crate) fn one(error: ValidationError) -> Self {
105 Self {
106 errors: vec![error],
107 }
108 }
109
110 pub(crate) fn from_vec(errors: Vec<ValidationError>) -> Option<Self> {
113 (!errors.is_empty()).then_some(Self { errors })
114 }
115
116 pub fn as_slice(&self) -> &[ValidationError] {
118 &self.errors
119 }
120
121 pub fn iter(&self) -> std::slice::Iter<'_, ValidationError> {
123 self.errors.iter()
124 }
125
126 pub fn len(&self) -> usize {
128 self.errors.len()
129 }
130
131 pub fn is_empty(&self) -> bool {
133 self.errors.is_empty()
134 }
135
136 pub fn into_vec(self) -> Vec<ValidationError> {
138 self.errors
139 }
140}
141
142impl fmt::Display for ValidationErrors {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 let n = self.errors.len();
145 let noun = if n == 1 { "error" } else { "errors" };
146 write!(f, "invalid configuration ({n} {noun})")?;
147 for e in &self.errors {
148 write!(f, "; {e}")?;
149 }
150 Ok(())
151 }
152}
153
154impl std::error::Error for ValidationErrors {}
155
156impl Serialize for ValidationErrors {
157 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
158 let mut seq = serializer.serialize_seq(Some(self.errors.len()))?;
159 for e in &self.errors {
160 seq.serialize_element(e)?;
161 }
162 seq.end()
163 }
164}
165
166impl<'a> IntoIterator for &'a ValidationErrors {
167 type Item = &'a ValidationError;
168 type IntoIter = std::slice::Iter<'a, ValidationError>;
169
170 fn into_iter(self) -> Self::IntoIter {
171 self.errors.iter()
172 }
173}
174
175impl IntoIterator for ValidationErrors {
176 type Item = ValidationError;
177 type IntoIter = std::vec::IntoIter<ValidationError>;
178
179 fn into_iter(self) -> Self::IntoIter {
180 self.errors.into_iter()
181 }
182}
183
184#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
190#[non_exhaustive]
191pub enum PreviewError {
192 #[error("base document is not valid toml: {0}")]
195 Syntax(ValidationError),
196 #[error("document is larger than the {limit}-byte limit")]
200 TooLarge {
201 limit: usize,
203 },
204 #[error("unknown configuration path {path:?}")]
206 UnknownPath {
207 path: String,
209 },
210 #[error("more than one change for {path}")]
212 DuplicatePath {
213 path: String,
215 },
216 #[error("cannot edit {path}: the base document does not hold a table or value there")]
219 NotEditable {
220 path: String,
222 },
223}