Skip to main content

ephemeris_documents/json/
node.rs

1//! The syntax tree value as a node of the generic merge.
2
3use super::cst::{Kind, Object, Value};
4use crate::merge::Node;
5
6impl Node for Value {
7    fn is_container(&self) -> bool {
8        self.is_object()
9    }
10
11    fn keys(&self) -> impl Iterator<Item = &str> + '_ {
12        self.as_object().into_iter().flat_map(Object::keys)
13    }
14
15    fn child(&self, key: &str) -> Option<&Self> {
16        self.as_object()?.get(key)
17    }
18
19    fn child_mut(&mut self, key: &str) -> Option<&mut Self> {
20        self.as_object_mut()?.descend(key)
21    }
22
23    fn adopt(&mut self, key: &str, source: &Self) {
24        if let (Some(object), Some(node)) = (self.as_object_mut(), source.child(key)) {
25            object.insert(key, node.clone());
26        }
27    }
28
29    fn adopt_shell(&mut self, key: &str, source: &Self) {
30        if let (Some(object), Some(node)) = (self.as_object_mut(), source.child(key)) {
31            object.insert(
32                key,
33                Self::new(node.leading.clone(), Kind::Object(Object::new())),
34            );
35        }
36    }
37
38    fn remove(&mut self, key: &str) {
39        if let Some(object) = self.as_object_mut() {
40            object.remove(key);
41        }
42    }
43}