1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
mod builder;
mod preset;
pub use builder::StyleBuilder;
pub use preset::PresetStyle;

/// Used by `Iterator` to format `Element.path` and
/// determine whether or not to yield object and/or array values
#[derive(Debug)]
pub struct Style<'a> {
    object_key_prefix: &'a str,
    object_key_suffix: &'a str,
    object_keys_in_path: bool,
    skip_object_parents: bool,
    array_key_prefix: &'a str,
    array_key_suffix: &'a str,
    array_keys_in_path: bool,
    skip_array_parents: bool,
}

impl<'a> Style<'a> {
    pub fn object_format(&self, base_path: &String, key: &String) -> String {
        if self.object_keys_in_path {
            format!(
                "{}{}{}{}",
                base_path, self.object_key_prefix, key, self.object_key_suffix,
            )
        } else {
            format!(
                "{}{}{}",
                base_path, self.object_key_prefix, self.object_key_suffix,
            )
        }
    }

    pub fn array_format(&self, base_path: &String, index: usize) -> String {
        if self.array_keys_in_path {
            format!(
                "{}{}{}{}",
                base_path, self.array_key_prefix, index, self.array_key_suffix,
            )
        } else {
            format!(
                "{}{}{}",
                base_path, self.array_key_prefix, self.array_key_suffix,
            )
        }
    }

    pub fn should_skip_object_parents(&self) -> bool {
        self.skip_object_parents
    }

    pub fn should_skip_array_parents(&self) -> bool {
        self.skip_array_parents
    }
}