yaml_subset/yaml/
aliased.rs

1use super::insert::Additive;
2use super::YamlInsert;
3use super::YamlTypes;
4use super::{HashData, HashElement, Yaml};
5use crate::path::Condition;
6use crate::yaml::Pretty;
7use crate::YamlPath;
8use std::fmt::Write;
9
10#[derive(Debug, Clone, PartialEq)]
11pub struct AliasedYaml {
12    pub alias: Option<String>,
13    pub value: Yaml,
14}
15
16impl AliasedYaml {
17    pub fn fits_conditions(&self, conditions: &Vec<Condition>) -> bool {
18        self.value.fits_conditions(conditions)
19    }
20}
21
22impl YamlTypes for AliasedYaml {
23    fn as_string(&self) -> Option<String> {
24        self.value.as_string()
25    }
26}
27
28impl YamlInsert for AliasedYaml {
29    fn edit_hash_structure<F>(&mut self, path: &YamlPath, f: &F) -> usize
30    where
31        F: Fn(&mut Vec<HashData>, String, Option<usize>) -> usize,
32    {
33        self.value.edit_hash_structure(path, f)
34    }
35    fn for_hash<F, R, A: Additive>(&mut self, path: &YamlPath, f: &F, r: &R) -> A
36    where
37        F: Fn(&mut HashElement) -> A,
38        R: Fn(&mut Yaml) -> A,
39    {
40        self.value.for_hash(path, f, r)
41    }
42}
43
44#[derive(Debug, Clone, Copy)]
45pub enum Parent {
46    Hash,
47    Array,
48}
49
50impl AliasedYaml {
51    pub fn format(
52        &self,
53        f: &mut String,
54        spaces: usize,
55        parent: Option<Parent>,
56    ) -> std::fmt::Result {
57        if let Some(alias) = self.alias.as_ref() {
58            write!(f, " &{}", alias)?;
59            self.value.format(f, spaces + 2, None)
60        } else {
61            self.value.format(f, spaces + 2, parent)
62        }
63    }
64}
65
66impl Pretty for AliasedYaml {
67    fn pretty(self) -> Self {
68        Self {
69            alias: self.alias,
70            value: self.value.pretty(),
71        }
72    }
73}