Skip to main content

ephemeris_documents/yaml/
node.rs

1//! The YAML source tree as a node of the generic merge.
2
3use super::source::{Entry, Mapping};
4use crate::merge::Node;
5
6impl Node for Entry {
7    fn is_container(&self) -> bool {
8        self.value().mapping().is_some()
9    }
10
11    fn keys(&self) -> impl Iterator<Item = &str> + '_ {
12        self.value().mapping().into_iter().flat_map(Mapping::keys)
13    }
14
15    fn child(&self, key: &str) -> Option<&Self> {
16        self.value().mapping()?.get(key)
17    }
18
19    fn child_mut(&mut self, key: &str) -> Option<&mut Self> {
20        self.value_mut().mapping_mut()?.get_mut(key)
21    }
22
23    fn adopt(&mut self, key: &str, source: &Self) {
24        let Some(node) = source.child(key) else {
25            return;
26        };
27        if let Some(mapping) = self.value_mut().mapping_mut() {
28            mapping.insert(node.clone().named(key));
29        }
30    }
31
32    fn adopt_shell(&mut self, key: &str, source: &Self) {
33        let Some(node) = source.child(key) else {
34            return;
35        };
36        if let Some(mapping) = self.value_mut().mapping_mut() {
37            mapping.insert(node.empty_with_decor().named(key));
38        }
39    }
40
41    fn remove(&mut self, key: &str) {
42        if let Some(mapping) = self.value_mut().mapping_mut() {
43            mapping.remove(key);
44        }
45    }
46}