json_keypath_iter/style/
preset.rs

1use super::*;
2
3/// Included preset stylings
4pub enum PresetStyle {
5    /// This yields a path that looks like: `["some_key"][123]`
6    ///
7    /// The Iterator also yields only non-object and non-array values with this style
8    SquareBrackets,
9    /// This yields a path that looks like: `.some_key[123]`
10    ///
11    /// The Iterator also yields only non-object and non-array values with this style
12    CommonJs,
13    /// This yields a path that looks like: `->'some_key'->123`
14    ///
15    /// The Iterator also yields only non-object and non-array values with this style
16    PostgresJson,
17}
18
19impl<'a> From<PresetStyle> for Style<'a> {
20    fn from(style: PresetStyle) -> Style<'a> {
21        let builder: StyleBuilder<'a> = style.into();
22        builder.build()
23    }
24}
25
26impl<'a> From<PresetStyle> for StyleBuilder<'a> {
27    fn from(style: PresetStyle) -> StyleBuilder<'a> {
28        match style {
29            PresetStyle::SquareBrackets => {
30                return StyleBuilder::new()
31                    .object_key_prefix("[\"")
32                    .object_key_suffix("\"]")
33                    .show_object_keys_in_path()
34                    .skip_object_parents()
35                    .array_key_prefix("[")
36                    .array_key_suffix("]")
37                    .show_array_keys_in_path()
38                    .skip_array_parents();
39            }
40            PresetStyle::CommonJs => {
41                return StyleBuilder::new()
42                    .object_key_prefix(".")
43                    .object_key_suffix("")
44                    .show_object_keys_in_path()
45                    .skip_object_parents()
46                    .array_key_prefix("[")
47                    .array_key_suffix("]")
48                    .show_array_keys_in_path()
49                    .skip_array_parents();
50            }
51            PresetStyle::PostgresJson => {
52                return StyleBuilder::new()
53                    .object_key_prefix("->'")
54                    .object_key_suffix("'")
55                    .show_object_keys_in_path()
56                    .skip_object_parents()
57                    .array_key_prefix("->")
58                    .array_key_suffix("")
59                    .show_array_keys_in_path()
60                    .skip_array_parents();
61            }
62        }
63    }
64}