Skip to main content

yaml_subset/yaml/
hash_element.rs

1use super::insert::Additive;
2use super::YamlInsert;
3use super::{AliasedYaml, HashData, Yaml};
4use crate::yaml::{MapYaml, Pretty, VisitYaml};
5use crate::YamlPath;
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct HashElement {
9    pub key: String,
10    pub value: AliasedYaml,
11}
12
13impl YamlInsert for HashElement {
14    fn edit_hash_structure<F>(&mut self, path: &YamlPath, f: &F) -> usize
15    where
16        F: Fn(&mut Vec<HashData>, String, Option<usize>) -> usize,
17    {
18        self.value.edit_hash_structure(path, f)
19    }
20    fn for_hash<F, R, A: Additive>(&mut self, path: &YamlPath, f: &F, r: &R) -> A
21    where
22        F: Fn(&mut HashElement) -> A,
23        R: Fn(&mut Yaml) -> A,
24    {
25        match path {
26            YamlPath::Root(_conditions) => f(self),
27            YamlPath::Key(key, _conditions, None) => {
28                if key == &self.key {
29                    f(self)
30                } else {
31                    A::zero()
32                }
33            }
34            YamlPath::Key(_key, _conditions, Some(other)) => self.value.for_hash(&*other, f, r),
35            _ => A::zero(),
36        }
37    }
38}
39
40impl VisitYaml for HashElement {
41    fn visit_yaml<F: FnMut(&AliasedYaml)>(&self, f: &mut F) {
42        self.value.visit_yaml(f);
43    }
44}
45
46impl MapYaml for HashElement {
47    fn map_yaml<F: FnMut(Yaml) -> Yaml>(self, f: &mut F) -> Self {
48        Self { key: self.key, value: self.value.map_yaml(f) }
49    }
50}
51
52impl Pretty for HashElement {
53    fn pretty_with_options(self, in_inline: bool, _child_of_array: bool) -> Self {
54        Self {
55            key: self.key,
56            value: self.value.pretty_with_options(in_inline, false),
57        }
58    }
59}