Skip to content

Commit

Permalink
Remove vendored code for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
krlvi committed Dec 18, 2024
1 parent 25e6fb9 commit 105343e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 266 deletions.
1 change: 0 additions & 1 deletion crates/gitbutler-settings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ anyhow = "1.0.93"
serde = { workspace = true, features = ["std"] }
serde_json = { version = "1.0", features = ["std", "arbitrary_precision"] }
serde_json_lenient = "0.2.3"
gitbutler-settings-util.path = "vendor/util"
gitbutler-fs.workspace = true

[[test]]
Expand Down
67 changes: 58 additions & 9 deletions crates/gitbutler-settings/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,57 @@ pub fn json_difference(
}
}

// tests
/// Based on Zed `merge_non_null_json_value_into`
/// Note: This doesn't merge arrays.
pub fn merge_non_null_json_value(source: serde_json::Value, target: &mut serde_json::Value) {
use serde_json::Value;
if let Value::Object(source_object) = source {
let target_object = if let Value::Object(target) = target {
target
} else {
*target = serde_json::json!({});
target.as_object_mut().expect("object was just set")
};
for (key, value) in source_object {
if let Some(target) = target_object.get_mut(&key) {
merge_non_null_json_value(value, target);
} else if !value.is_null() {
target_object.insert(key, value);
}
}
} else if !source.is_null() {
*target = source
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_does_not_merge_null_values() {
let source = serde_json::json!({"a": null, "b": true });
let mut target = serde_json::json!({});
merge_non_null_json_value(source, &mut target);
assert_eq!(target, serde_json::json!({"b": true }));
}

#[test]
fn it_does_not_merge_arrays() {
let source = serde_json::json!({"a": null, "b": [1,2,3]});
let mut target = serde_json::json!({"a": {"b": 1}, "b": [42]});
merge_non_null_json_value(source, &mut target);
assert_eq!(target, serde_json::json!({"a": {"b": 1}, "b": [1,2,3] }));
}

#[test]
fn it_merges_nested_objects_correctly() {
let source = serde_json::json!({"a": {"b": {"c": 42}}});
let mut target = serde_json::json!({});
merge_non_null_json_value(source.clone(), &mut target);
assert_eq!(target, source);
}

#[test]
pub fn test_difference_existing_key() {
use serde_json::json;
Expand All @@ -54,7 +103,7 @@ mod tests {
}
});
assert_eq!(
super::json_difference(current, &update),
json_difference(current, &update),
json!({
"e": {
"f": 5
Expand Down Expand Up @@ -88,7 +137,7 @@ mod tests {
"g": 5
});
assert_eq!(
super::json_difference(current, &update),
json_difference(current, &update),
json!({
"g": 5
})
Expand Down Expand Up @@ -118,7 +167,7 @@ mod tests {
"l": 8
}
});
assert_eq!(super::json_difference(current, &update), update);
assert_eq!(json_difference(current, &update), update);
}

#[test]
Expand All @@ -144,7 +193,7 @@ mod tests {
"f": 4
}
});
assert_eq!(super::json_difference(current, &update), json!({}));
assert_eq!(json_difference(current, &update), json!({}));
}

#[test]
Expand Down Expand Up @@ -172,7 +221,7 @@ mod tests {
"g": 5
});
assert_eq!(
super::json_difference(current, &update),
json_difference(current, &update),
json!({
"e": {
"f": null
Expand All @@ -191,15 +240,15 @@ mod tests {
let update = json!({
"a": null
});
assert_eq!(super::json_difference(current, &update), json!({}));
assert_eq!(json_difference(current, &update), json!({}));
}

#[test]
pub fn test_empty_object() {
use serde_json::json;
let current = json!({});
let update = json!({});
assert_eq!(super::json_difference(current, &update), json!({}));
assert_eq!(json_difference(current, &update), json!({}));
}

#[test]
Expand All @@ -209,6 +258,6 @@ mod tests {
let update = json!({
"a": 1
});
assert_eq!(super::json_difference(current, &update), update);
assert_eq!(json_difference(current, &update), update);
}
}
7 changes: 3 additions & 4 deletions crates/gitbutler-settings/src/persistence.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::path::PathBuf;

use crate::app_settings::AppSettings;
use crate::json::json_difference;
use crate::json::{json_difference, merge_non_null_json_value};
use anyhow::Result;
use gitbutler_settings_util::merge_non_null_json_value_into;
use serde_json::json;

static DEFAULTS: &str = include_str!("../assets/defaults.jsonc");
Expand All @@ -21,7 +20,7 @@ impl AppSettings {
let customizations = serde_json_lenient::from_str(&std::fs::read_to_string(config_path)?)?;

// Merge the customizations into the settings
merge_non_null_json_value_into(customizations, &mut settings);
merge_non_null_json_value(customizations, &mut settings);
Ok(serde_json::from_value(settings)?)
}

Expand All @@ -48,7 +47,7 @@ impl AppSettings {

// Merge the new customizations into the existing ones
// TODO: This will nuke any comments in the file
merge_non_null_json_value_into(diff, &mut customizations);
merge_non_null_json_value(diff, &mut customizations);
gitbutler_fs::write(config_path, customizations.to_string())?;
Ok(())
}
Expand Down
10 changes: 0 additions & 10 deletions crates/gitbutler-settings/vendor/util/Cargo.toml

This file was deleted.

Loading

0 comments on commit 105343e

Please sign in to comment.