Fix spurious setting error log messages (#2498)

Fixes a bug introduced in
https://github.com/zed-industries/zed/pull/2448, where error messages
would be logged if the user config didn't specify certain fields like
`journal` or `telemetry`.
This commit is contained in:
Max Brunsfeld 2023-05-22 11:44:21 -07:00 committed by GitHub
commit e129ed2d91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 57 deletions

View file

@ -339,7 +339,7 @@ pub struct TelemetrySettings {
pub metrics: bool, pub metrics: bool,
} }
#[derive(Clone, Serialize, Deserialize, JsonSchema)] #[derive(Default, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TelemetrySettingsContent { pub struct TelemetrySettingsContent {
pub diagnostics: Option<bool>, pub diagnostics: Option<bool>,
pub metrics: Option<bool>, pub metrics: Option<bool>,

View file

@ -10,17 +10,16 @@ pub struct EditorSettings {
pub show_scrollbars: ShowScrollbars, pub show_scrollbars: ShowScrollbars,
} }
#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Default)] #[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum ShowScrollbars { pub enum ShowScrollbars {
#[default]
Auto, Auto,
System, System,
Always, Always,
Never, Never,
} }
#[derive(Clone, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct EditorSettingsContent { pub struct EditorSettingsContent {
pub cursor_blink: Option<bool>, pub cursor_blink: Option<bool>,
pub hover_popover_enabled: Option<bool>, pub hover_popover_enabled: Option<bool>,

View file

@ -49,7 +49,7 @@ pub struct CopilotSettings {
pub disabled_globs: Vec<GlobMatcher>, pub disabled_globs: Vec<GlobMatcher>,
} }
#[derive(Clone, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct AllLanguageSettingsContent { pub struct AllLanguageSettingsContent {
#[serde(default)] #[serde(default)]
pub features: Option<FeaturesContent>, pub features: Option<FeaturesContent>,

View file

@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use settings::Setting; use settings::Setting;
use std::sync::Arc; use std::sync::Arc;
#[derive(Clone, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct ProjectSettings { pub struct ProjectSettings {
#[serde(default)] #[serde(default)]
pub lsp: HashMap<Arc<str>, LspSettings>, pub lsp: HashMap<Arc<str>, LspSettings>,

View file

@ -25,7 +25,7 @@ pub trait Setting: 'static {
const KEY: Option<&'static str>; const KEY: Option<&'static str>;
/// The type that is stored in an individual JSON file. /// The type that is stored in an individual JSON file.
type FileContent: Clone + Serialize + DeserializeOwned + JsonSchema; type FileContent: Clone + Default + Serialize + DeserializeOwned + JsonSchema;
/// The logic for combining together values from one or more JSON files into the /// The logic for combining together values from one or more JSON files into the
/// final value for this setting. /// final value for this setting.
@ -460,11 +460,12 @@ impl SettingsStore {
// If the global settings file changed, reload the global value for the field. // If the global settings file changed, reload the global value for the field.
if changed_local_path.is_none() { if changed_local_path.is_none() {
setting_value.set_global_value(setting_value.load_setting( if let Some(value) = setting_value
&default_settings, .load_setting(&default_settings, &user_settings_stack, cx)
&user_settings_stack, .log_err()
cx, {
)?); setting_value.set_global_value(value);
}
} }
// Reload the local values for the setting. // Reload the local values for the setting.
@ -495,14 +496,12 @@ impl SettingsStore {
continue; continue;
} }
setting_value.set_local_value( if let Some(value) = setting_value
path.clone(), .load_setting(&default_settings, &user_settings_stack, cx)
setting_value.load_setting( .log_err()
&default_settings, {
&user_settings_stack, setting_value.set_local_value(path.clone(), value);
cx, }
)?,
);
} }
} }
} }
@ -536,7 +535,12 @@ impl<T: Setting> AnySettingValue for SettingValue<T> {
fn deserialize_setting(&self, mut json: &serde_json::Value) -> Result<DeserializedSetting> { fn deserialize_setting(&self, mut json: &serde_json::Value) -> Result<DeserializedSetting> {
if let Some(key) = T::KEY { if let Some(key) = T::KEY {
json = json.get(key).unwrap_or(&serde_json::Value::Null); if let Some(value) = json.get(key) {
json = value;
} else {
let value = T::FileContent::default();
return Ok(DeserializedSetting(Box::new(value)));
}
} }
let value = T::FileContent::deserialize(json)?; let value = T::FileContent::deserialize(json)?;
Ok(DeserializedSetting(Box::new(value))) Ok(DeserializedSetting(Box::new(value)))
@ -826,37 +830,6 @@ mod tests {
store.register_setting::<UserSettings>(cx); store.register_setting::<UserSettings>(cx);
store.register_setting::<TurboSetting>(cx); store.register_setting::<TurboSetting>(cx);
store.register_setting::<MultiKeySettings>(cx); store.register_setting::<MultiKeySettings>(cx);
// error - missing required field in default settings
store
.set_default_settings(
r#"{
"user": {
"name": "John Doe",
"age": 30,
"staff": false
}
}"#,
cx,
)
.unwrap_err();
// error - type error in default settings
store
.set_default_settings(
r#"{
"turbo": "the-wrong-type",
"user": {
"name": "John Doe",
"age": 30,
"staff": false
}
}"#,
cx,
)
.unwrap_err();
// valid default settings.
store store
.set_default_settings( .set_default_settings(
r#"{ r#"{
@ -1126,7 +1099,7 @@ mod tests {
staff: bool, staff: bool,
} }
#[derive(Clone, Serialize, Deserialize, JsonSchema)] #[derive(Default, Clone, Serialize, Deserialize, JsonSchema)]
struct UserSettingsJson { struct UserSettingsJson {
name: Option<String>, name: Option<String>,
age: Option<u32>, age: Option<u32>,
@ -1170,7 +1143,7 @@ mod tests {
key2: String, key2: String,
} }
#[derive(Clone, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
struct MultiKeySettingsJson { struct MultiKeySettingsJson {
key1: Option<String>, key1: Option<String>,
key2: Option<String>, key2: Option<String>,
@ -1203,7 +1176,7 @@ mod tests {
Hour24, Hour24,
} }
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
struct JournalSettingsJson { struct JournalSettingsJson {
pub path: Option<String>, pub path: Option<String>,
pub hour_format: Option<HourFormat>, pub hour_format: Option<HourFormat>,
@ -1223,7 +1196,7 @@ mod tests {
} }
} }
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
struct LanguageSettings { struct LanguageSettings {
#[serde(default)] #[serde(default)]
languages: HashMap<String, LanguageSettingEntry>, languages: HashMap<String, LanguageSettingEntry>,

View file

@ -17,7 +17,7 @@ pub struct WorkspaceSettings {
pub git: GitSettings, pub git: GitSettings,
} }
#[derive(Clone, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
pub struct WorkspaceSettingsContent { pub struct WorkspaceSettingsContent {
pub active_pane_magnification: Option<f32>, pub active_pane_magnification: Option<f32>,
pub confirm_quit: Option<bool>, pub confirm_quit: Option<bool>,