json_keypath_iter/style/
mod.rs

1mod builder;
2mod preset;
3pub use builder::StyleBuilder;
4pub use preset::PresetStyle;
5
6/// Used by `Iterator` to format `Element.path` and
7/// determine whether or not to yield object and/or array values
8#[derive(Debug)]
9pub struct Style<'a> {
10    object_key_prefix: &'a str,
11    object_key_suffix: &'a str,
12    object_keys_in_path: bool,
13    skip_object_parents: bool,
14    array_key_prefix: &'a str,
15    array_key_suffix: &'a str,
16    array_keys_in_path: bool,
17    skip_array_parents: bool,
18}
19
20impl<'a> Style<'a> {
21    pub fn object_format(&self, base_path: &String, key: &String) -> String {
22        if self.object_keys_in_path {
23            format!(
24                "{}{}{}{}",
25                base_path, self.object_key_prefix, key, self.object_key_suffix,
26            )
27        } else {
28            format!(
29                "{}{}{}",
30                base_path, self.object_key_prefix, self.object_key_suffix,
31            )
32        }
33    }
34
35    pub fn array_format(&self, base_path: &String, index: usize) -> String {
36        if self.array_keys_in_path {
37            format!(
38                "{}{}{}{}",
39                base_path, self.array_key_prefix, index, self.array_key_suffix,
40            )
41        } else {
42            format!(
43                "{}{}{}",
44                base_path, self.array_key_prefix, self.array_key_suffix,
45            )
46        }
47    }
48
49    pub fn should_skip_object_parents(&self) -> bool {
50        self.skip_object_parents
51    }
52
53    pub fn should_skip_array_parents(&self) -> bool {
54        self.skip_array_parents
55    }
56}