pub trait GraphState: Sized + Send + Sync + 'static {
type Update: Default + Send + 'static;
fn apply(&mut self, update: Self::Update);
fn reset_ephemeral(&mut self) {}
}
#[doc(hidden)]
pub fn __merge_json(target: &mut serde_json::Value, source: serde_json::Value) {
use serde_json::Value;
match (target, source) {
(Value::Object(t_map), Value::Object(s_map)) => {
for (k, v) in s_map {
__merge_json(t_map.entry(k).or_insert(Value::Null), v);
}
}
(slot, source) => {
*slot = source;
}
}
}
#[cfg(test)]
mod tests {
use super::__merge_json;
use serde_json::json;
#[test]
fn merge_objects_keys_union() {
let mut t = json!({"a": 1, "b": 2});
__merge_json(&mut t, json!({"b": 20, "c": 3}));
assert_eq!(t, json!({"a": 1, "b": 20, "c": 3}));
}
#[test]
fn merge_nested_objects() {
let mut t = json!({"x": {"a": 1, "b": 2}});
__merge_json(&mut t, json!({"x": {"b": 20, "c": 3}}));
assert_eq!(t, json!({"x": {"a": 1, "b": 20, "c": 3}}));
}
#[test]
fn merge_arrays_replace() {
let mut t = json!([1, 2, 3]);
__merge_json(&mut t, json!([9]));
assert_eq!(t, json!([9]));
}
#[test]
fn merge_scalar_replace() {
let mut t = json!(1);
__merge_json(&mut t, json!("hello"));
assert_eq!(t, json!("hello"));
}
}