settings_ui: Soft fail on no default & fix language default loading (#39709)
Closes #ISSUE Release Notes: - N/A *or* Added/Fixed/Improved ...
This commit is contained in:
@@ -534,12 +534,15 @@ impl SettingsStore {
|
||||
overrides
|
||||
}
|
||||
|
||||
/// Checks the given file, and files that the passed file overrides for the given field.
|
||||
/// Returns the first file found that contains the value.
|
||||
/// The value will only be None if no file contains the value.
|
||||
/// I.e. if no file contains the value, returns `(File::Default, None)`
|
||||
pub fn get_value_from_file<T>(
|
||||
&self,
|
||||
target_file: SettingsFile,
|
||||
pick: fn(&SettingsContent) -> &Option<T>,
|
||||
type_name: &'static str,
|
||||
) -> (SettingsFile, &T) {
|
||||
) -> (SettingsFile, Option<&T>) {
|
||||
// TODO: Add a metadata field for overriding the "overrides" tag, for contextually different settings
|
||||
// e.g. disable AI isn't overridden, or a vec that gets extended instead or some such
|
||||
|
||||
@@ -565,11 +568,11 @@ impl SettingsStore {
|
||||
continue;
|
||||
};
|
||||
if let Some(value) = pick(content).as_ref() {
|
||||
return (file, value);
|
||||
return (file, Some(value));
|
||||
}
|
||||
}
|
||||
|
||||
unreachable!("{type_name}: doesn't have a default value");
|
||||
(SettingsFile::Default, None)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1715,25 +1718,17 @@ mod tests {
|
||||
let default_value = get(&store.default_settings).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
store.get_value_from_file(
|
||||
SettingsFile::Local(local.clone()),
|
||||
get,
|
||||
"preferred line length"
|
||||
),
|
||||
(SettingsFile::User, &0)
|
||||
store.get_value_from_file(SettingsFile::Local(local.clone()), get),
|
||||
(SettingsFile::User, Some(&0))
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_value_from_file(SettingsFile::User, get, "preferred line length"),
|
||||
(SettingsFile::User, &0)
|
||||
store.get_value_from_file(SettingsFile::User, get),
|
||||
(SettingsFile::User, Some(&0))
|
||||
);
|
||||
store.set_user_settings(r#"{}"#, cx).unwrap();
|
||||
assert_eq!(
|
||||
store.get_value_from_file(
|
||||
SettingsFile::Local(local.clone()),
|
||||
get,
|
||||
"preferred line length"
|
||||
),
|
||||
(SettingsFile::Default, &default_value)
|
||||
store.get_value_from_file(SettingsFile::Local(local.clone()), get),
|
||||
(SettingsFile::Default, Some(&default_value))
|
||||
);
|
||||
store
|
||||
.set_local_settings(
|
||||
@@ -1745,16 +1740,12 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
store.get_value_from_file(
|
||||
SettingsFile::Local(local.clone()),
|
||||
get,
|
||||
"preferred line length"
|
||||
),
|
||||
(SettingsFile::Local(local), &80)
|
||||
store.get_value_from_file(SettingsFile::Local(local.clone()), get),
|
||||
(SettingsFile::Local(local), Some(&80))
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_value_from_file(SettingsFile::User, get, "preferred line length"),
|
||||
(SettingsFile::Default, &default_value)
|
||||
store.get_value_from_file(SettingsFile::User, get),
|
||||
(SettingsFile::Default, Some(&default_value))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1830,20 +1821,12 @@ mod tests {
|
||||
|
||||
// each local child should only inherit from it's parent
|
||||
assert_eq!(
|
||||
store.get_value_from_file(
|
||||
SettingsFile::Local(local_2_child),
|
||||
get,
|
||||
"preferred_line_length"
|
||||
),
|
||||
(SettingsFile::Local(local_2), &2)
|
||||
store.get_value_from_file(SettingsFile::Local(local_2_child), get),
|
||||
(SettingsFile::Local(local_2), Some(&2))
|
||||
);
|
||||
assert_eq!(
|
||||
store.get_value_from_file(
|
||||
SettingsFile::Local(local_1_child.clone()),
|
||||
get,
|
||||
"preferred_line_length"
|
||||
),
|
||||
(SettingsFile::Local(local_1.clone()), &1)
|
||||
store.get_value_from_file(SettingsFile::Local(local_1_child.clone()), get),
|
||||
(SettingsFile::Local(local_1.clone()), Some(&1))
|
||||
);
|
||||
|
||||
// adjacent children should be treated as siblings not inherit from each other
|
||||
@@ -1868,12 +1851,8 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
store.get_value_from_file(
|
||||
SettingsFile::Local(local_1_adjacent_child.clone()),
|
||||
get,
|
||||
"preferred_line_length"
|
||||
),
|
||||
(SettingsFile::Local(local_1.clone()), &1)
|
||||
store.get_value_from_file(SettingsFile::Local(local_1_adjacent_child.clone()), get),
|
||||
(SettingsFile::Local(local_1.clone()), Some(&1))
|
||||
);
|
||||
store
|
||||
.set_local_settings(
|
||||
@@ -1894,12 +1873,8 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
store.get_value_from_file(
|
||||
SettingsFile::Local(local_1_child),
|
||||
get,
|
||||
"preferred_line_length"
|
||||
),
|
||||
(SettingsFile::Local(local_1), &1)
|
||||
store.get_value_from_file(SettingsFile::Local(local_1_child), get),
|
||||
(SettingsFile::Local(local_1), Some(&1))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user