settings: SectionError implements Display and Error

A section that did not fit the settings type is reported in
LoadReport::section_errors, but SectionError had no Display, so an app
could only show it with Debug. It now formats as "settings section
[<name>] was reset to defaults: <message>" and implements
std::error::Error, as MigrateError does.
This commit is contained in:
dotCooCoo committed 2026-11-20 10:43:19 -08:00
1 parent a20dfd1623
commit 1463b8caea
2 files changed
+21

No files matched your search

+8
View File
@@ -38,6 +38,14 @@ pub struct SectionError {
pub message: String,
}
impl fmt::Display for SectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "settings section [{}] was reset to defaults: {}", self.section, self.message)
}
}
impl std::error::Error for SectionError {}
/// Why the store will not write `config.toml`.
#[derive(Debug)]
pub enum ReadOnlyReason {
+13
View File
@@ -47,6 +47,19 @@ fn bad_value_only_resets_its_section() {
assert!(config_path(tmp.path()).exists(), "the file is not moved aside");
}
#[test]
fn section_error_displays_its_section_and_message() {
let tmp = tempfile::tempdir().unwrap();
write_config(tmp.path(), "schema_version = 2\n[appearance]\ntheme = \"sepia\"\n");
let (_, report) = open_in(tmp.path());
let error = &report.section_errors[0];
let text = error.to_string();
assert!(text.starts_with("settings section [appearance] was reset to defaults: "), "{text}");
assert!(text.ends_with(&error.message), "{text}");
let source: &dyn std::error::Error = error;
assert!(source.source().is_none());
}
#[test]
fn many_unknown_keys_load_in_linear_time() {
// Each key was once tried against every key accepted before it.