1use std::sync::Arc;
2
3use crate::JsonValue;
4use crate::shared::constants::DEFAULT_DELIMITER;
5
6pub type EncodeReplacer =
7 Arc<dyn Fn(&str, &JsonValue, &[PathSegment]) -> Option<JsonValue> + Send + Sync>;
8
9#[derive(Clone)]
10pub struct EncodeOptions {
11 pub indent: Option<usize>,
12 pub delimiter: Option<char>,
13 pub key_folding: Option<KeyFoldingMode>,
14 pub flatten_depth: Option<usize>,
15 pub replacer: Option<EncodeReplacer>,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum KeyFoldingMode {
20 Off,
21 Safe,
22}
23
24#[derive(Debug, Clone)]
25pub struct DecodeOptions {
26 pub indent: Option<usize>,
27 pub strict: Option<bool>,
28 pub expand_paths: Option<ExpandPathsMode>,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum ExpandPathsMode {
33 Off,
34 Safe,
35}
36
37#[derive(Debug, Clone, Default)]
38pub struct DecodeStreamOptions {
39 pub indent: Option<usize>,
40 pub strict: Option<bool>,
41}
42
43#[derive(Clone)]
44pub struct ResolvedEncodeOptions {
45 pub indent: usize,
46 pub delimiter: char,
47 pub key_folding: KeyFoldingMode,
48 pub flatten_depth: usize,
49 pub replacer: Option<EncodeReplacer>,
50}
51
52#[derive(Debug, Clone)]
53pub struct ResolvedDecodeOptions {
54 pub indent: usize,
55 pub strict: bool,
56 pub expand_paths: ExpandPathsMode,
57}
58
59#[derive(Debug, Clone, PartialEq, Eq)]
60pub enum PathSegment {
61 Key(String),
62 Index(usize),
63}
64
65#[must_use]
66pub fn resolve_encode_options(options: Option<EncodeOptions>) -> ResolvedEncodeOptions {
67 let options = options.unwrap_or(EncodeOptions {
68 indent: None,
69 delimiter: None,
70 key_folding: None,
71 flatten_depth: None,
72 replacer: None,
73 });
74
75 ResolvedEncodeOptions {
76 indent: options.indent.unwrap_or(2),
77 delimiter: options.delimiter.unwrap_or(DEFAULT_DELIMITER),
78 key_folding: options.key_folding.unwrap_or(KeyFoldingMode::Off),
79 flatten_depth: options.flatten_depth.unwrap_or(usize::MAX),
80 replacer: options.replacer,
81 }
82}
83
84#[must_use]
85pub fn resolve_decode_options(options: Option<DecodeOptions>) -> ResolvedDecodeOptions {
86 let options = options.unwrap_or(DecodeOptions {
87 indent: None,
88 strict: None,
89 expand_paths: None,
90 });
91
92 ResolvedDecodeOptions {
93 indent: options.indent.unwrap_or(2),
94 strict: options.strict.unwrap_or(true),
95 expand_paths: options.expand_paths.unwrap_or(ExpandPathsMode::Off),
96 }
97}