Skip to main content

yaml_subset/yaml/
array_data.rs

1use super::insert::Additive;
2use super::YamlInsert;
3use super::{AliasedYaml, HashData, HashElement, Yaml};
4use crate::path::Condition;
5use crate::yaml::{MapYaml, Pretty, VisitYaml};
6use crate::YamlPath;
7
8#[derive(Debug, Clone, PartialEq)]
9pub enum ArrayData {
10    InlineComment(String),
11    Comment(String),
12    Element(AliasedYaml),
13}
14
15impl YamlInsert for ArrayData {
16    fn edit_hash_structure<F>(&mut self, path: &YamlPath, f: &F) -> usize
17    where
18        F: Fn(&mut Vec<HashData>, String, Option<usize>) -> usize,
19    {
20        match self {
21            ArrayData::Element(a) => a.edit_hash_structure(path, f),
22            _ => 0,
23        }
24    }
25    fn for_hash<F, R, A: Additive>(&mut self, path: &YamlPath, f: &F, r: &R) -> A
26    where
27        F: Fn(&mut HashElement) -> A,
28        R: Fn(&mut Yaml) -> A,
29    {
30        match self {
31            ArrayData::Element(a) => match path {
32                YamlPath::Root(_conditions) => A::zero(),
33                YamlPath::Key(_key, _conditions, None) => a.for_hash(path, f, r),
34                YamlPath::AllIndexes(_, Some(other)) => a.for_hash(&*other, f, r),
35                YamlPath::AllIndexes(_, None) => a.for_hash(&YamlPath::Root(Vec::new()), f, r),
36                YamlPath::Indexes(_, _, Some(other)) => a.for_hash(&*other, f, r),
37                YamlPath::Indexes(_, _, None) => a.for_hash(&YamlPath::Root(Vec::new()), f, r),
38                _ => A::zero(),
39            },
40            _ => A::zero(),
41        }
42    }
43}
44
45impl ArrayData {
46    pub fn element(e: AliasedYaml) -> ArrayData {
47        ArrayData::Element(e)
48    }
49    pub fn fits_conditions(&self, conditions: &Vec<Condition>) -> bool {
50        match self {
51            ArrayData::Element(e) => e.fits_conditions(conditions),
52            _ => false,
53        }
54    }
55}
56
57impl VisitYaml for ArrayData {
58    fn visit_yaml<F: FnMut(&AliasedYaml)>(&self, f: &mut F) {
59        if let ArrayData::Element(e) = self {
60            e.visit_yaml(f);
61        }
62    }
63}
64
65impl MapYaml for ArrayData {
66    fn map_yaml<F: FnMut(Yaml) -> Yaml>(self, f: &mut F) -> Self {
67        match self {
68            ArrayData::Element(e) => ArrayData::Element(e.map_yaml(f)),
69            other => other,
70        }
71    }
72}
73
74impl Pretty for ArrayData {
75    fn pretty_with_options(self, in_inline: bool, child_of_array: bool) -> Self {
76        match self {
77            ArrayData::Element(e) => ArrayData::Element(e.pretty_with_options(in_inline, child_of_array)),
78            o => o,
79        }
80    }
81}