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
57
58
59
60
61
62
63
64
use super::*;

/// Included preset stylings
pub enum PresetStyle {
    /// This yields a path that looks like: `["some_key"][123]`
    ///
    /// The Iterator also yields only non-object and non-array values with this style
    SquareBrackets,
    /// This yields a path that looks like: `.some_key[123]`
    ///
    /// The Iterator also yields only non-object and non-array values with this style
    CommonJs,
    /// This yields a path that looks like: `->'some_key'->123`
    ///
    /// The Iterator also yields only non-object and non-array values with this style
    PostgresJson,
}

impl<'a> From<PresetStyle> for Style<'a> {
    fn from(style: PresetStyle) -> Style<'a> {
        let builder: StyleBuilder<'a> = style.into();
        builder.build()
    }
}

impl<'a> From<PresetStyle> for StyleBuilder<'a> {
    fn from(style: PresetStyle) -> StyleBuilder<'a> {
        match style {
            PresetStyle::SquareBrackets => {
                return StyleBuilder::new()
                    .object_key_prefix("[\"")
                    .object_key_suffix("\"]")
                    .show_object_keys_in_path()
                    .skip_object_parents()
                    .array_key_prefix("[")
                    .array_key_suffix("]")
                    .show_array_keys_in_path()
                    .skip_array_parents();
            }
            PresetStyle::CommonJs => {
                return StyleBuilder::new()
                    .object_key_prefix(".")
                    .object_key_suffix("")
                    .show_object_keys_in_path()
                    .skip_object_parents()
                    .array_key_prefix("[")
                    .array_key_suffix("]")
                    .show_array_keys_in_path()
                    .skip_array_parents();
            }
            PresetStyle::PostgresJson => {
                return StyleBuilder::new()
                    .object_key_prefix("->'")
                    .object_key_suffix("'")
                    .show_object_keys_in_path()
                    .skip_object_parents()
                    .array_key_prefix("->")
                    .array_key_suffix("")
                    .show_array_keys_in_path()
                    .skip_array_parents();
            }
        }
    }
}