minco_config/
diagnostic.rs1use serde::{Deserialize, Serialize};
2use std::{error::Error, fmt};
3
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct ConfigDiagnostic {
7 pub code: String,
8 pub message: String,
9 #[serde(default, skip_serializing_if = "Option::is_none")]
10 pub path: Option<String>,
11 #[serde(default, skip_serializing_if = "Option::is_none")]
12 pub source: Option<String>,
13}
14
15impl ConfigDiagnostic {
16 pub(crate) fn new(code: &str, message: impl Into<String>) -> Self {
17 Self {
18 code: code.into(),
19 message: message.into(),
20 path: None,
21 source: None,
22 }
23 }
24
25 pub(crate) fn with_path(mut self, path: impl Into<String>) -> Self {
26 self.path = Some(path.into());
27 self
28 }
29
30 pub(crate) fn with_source(mut self, source: impl Into<String>) -> Self {
31 self.source = Some(source.into());
32 self
33 }
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct ConfigurationError {
39 diagnostics: Vec<ConfigDiagnostic>,
40}
41
42impl ConfigurationError {
43 pub(crate) fn new(diagnostics: Vec<ConfigDiagnostic>) -> Self {
44 debug_assert!(!diagnostics.is_empty());
45 Self { diagnostics }
46 }
47
48 pub fn from_diagnostic(diagnostic: ConfigDiagnostic) -> Self {
51 Self {
52 diagnostics: vec![diagnostic],
53 }
54 }
55
56 pub fn diagnostics(&self) -> &[ConfigDiagnostic] {
58 &self.diagnostics
59 }
60}
61
62impl fmt::Display for ConfigurationError {
63 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
64 write!(
65 formatter,
66 "configuration rejected with {} diagnostic(s)",
67 self.diagnostics.len()
68 )
69 }
70}
71
72impl Error for ConfigurationError {}