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::Pretty;
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 Pretty for ArrayData {
58    fn pretty(self) -> Self {
59        match self {
60            ArrayData::Element(e) => ArrayData::Element(e.pretty()),
61            o => o,
62        }
63    }
64}