REST API cleanup

This commit is contained in:
mdecimus
2024-04-01 19:48:59 +02:00
parent 35562bb9fd
commit 223bd59bab
50 changed files with 2722 additions and 2531 deletions

View File

@@ -43,15 +43,15 @@ pub struct Config {
#[serde(tag = "type")]
pub enum ConfigWarning {
Missing,
AppliedDefault(String),
AppliedDefault { default: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "type")]
pub enum ConfigError {
Parse(String),
Build(String),
Macro(String),
Parse { error: String },
Build { error: String },
Macro { error: String },
}
#[derive(Debug, Default, PartialEq, Eq)]
@@ -99,7 +99,9 @@ impl Config {
} else {
self.errors.insert(
key.clone(),
ConfigError::Macro(format!("Unknown key {location:?}")),
ConfigError::Macro {
error: format!("Unknown key {location:?}"),
},
);
}
}
@@ -110,9 +112,9 @@ impl Config {
Err(_) => {
self.errors.insert(
key.clone(),
ConfigError::Macro(format!(
ConfigError::Macro { error : format!(
"Failed to obtain environment variable {location:?}"
)),
)},
);
}
},
@@ -126,9 +128,11 @@ impl Config {
Err(err) => {
self.errors.insert(
key.clone(),
ConfigError::Macro(format!(
ConfigError::Macro {
error: format!(
"Failed to read file {file_name:?}: {err}"
)),
),
},
);
continue 'outer;
}
@@ -136,9 +140,11 @@ impl Config {
Err(err) => {
self.errors.insert(
key.clone(),
ConfigError::Macro(format!(
"Failed to read file {file_name:?}: {err}"
)),
ConfigError::Macro {
error: format!(
"Failed to read file {file_name:?}: {err}"
),
},
);
continue 'outer;
}
@@ -175,14 +181,14 @@ impl Config {
pub fn log_errors(&self, use_stderr: bool) {
for (key, err) in &self.errors {
let message = match err {
ConfigError::Parse(err) => {
format!("Failed to parse setting {key:?}: {err}")
ConfigError::Parse { error } => {
format!("Failed to parse setting {key:?}: {error}")
}
ConfigError::Build(err) => {
format!("Build error for key {key:?}: {err}")
ConfigError::Build { error } => {
format!("Build error for key {key:?}: {error}")
}
ConfigError::Macro(err) => {
format!("Macro expansion error for setting {key:?}: {err}")
ConfigError::Macro { error } => {
format!("Macro expansion error for setting {key:?}: {error}")
}
};
if !use_stderr {
@@ -196,7 +202,7 @@ impl Config {
pub fn log_warnings(&self, use_stderr: bool) {
for (key, warn) in &self.warnings {
let message = match warn {
ConfigWarning::AppliedDefault(default) => {
ConfigWarning::AppliedDefault { default } => {
format!("WARNING: Missing setting {key:?}, applied default {default:?}")
}
ConfigWarning::Missing => {

View File

@@ -168,8 +168,9 @@ impl Config {
Ok(value) => {
results.push((key.to_string(), value));
}
Err(err) => {
self.errors.insert(key.to_string(), ConfigError::Parse(err));
Err(error) => {
self.errors
.insert(key.to_string(), ConfigError::Parse { error });
}
}
}
@@ -191,8 +192,12 @@ impl Config {
if let Some(value) = self.keys.get(&key) {
Some(value.as_str())
} else {
self.errors
.insert(key, ConfigError::Parse("Missing property".to_string()));
self.errors.insert(
key,
ConfigError::Parse {
error: "Missing property".to_string(),
},
);
None
}
}
@@ -200,8 +205,9 @@ impl Config {
pub fn try_parse_value<T: ParseValue>(&mut self, key: impl AsKey, value: &str) -> Option<T> {
match T::parse_value(value) {
Ok(value) => Some(value),
Err(err) => {
self.errors.insert(key.as_key(), ConfigError::Parse(err));
Err(error) => {
self.errors
.insert(key.as_key(), ConfigError::Parse { error });
None
}
}
@@ -252,13 +258,21 @@ impl Config {
}
pub fn new_parse_error(&mut self, key: impl AsKey, details: impl Into<String>) {
self.errors
.insert(key.as_key(), ConfigError::Parse(details.into()));
self.errors.insert(
key.as_key(),
ConfigError::Parse {
error: details.into(),
},
);
}
pub fn new_build_error(&mut self, key: impl AsKey, details: impl Into<String>) {
self.errors
.insert(key.as_key(), ConfigError::Build(details.into()));
self.errors.insert(
key.as_key(),
ConfigError::Build {
error: details.into(),
},
);
}
pub fn new_missing_property(&mut self, key: impl AsKey) {