fakecloud_stepfunctions/
io_processing.rs1use serde_json::Value;
2
3pub fn apply_input_path(input: &Value, path: Option<&str>) -> Value {
8 match path {
9 None | Some("$") => input.clone(),
10 Some(p) => resolve_path(input, p),
11 }
12}
13
14pub fn apply_output_path(output: &Value, path: Option<&str>) -> Value {
17 match path {
18 None | Some("$") => output.clone(),
19 Some(p) => resolve_path(output, p),
20 }
21}
22
23pub fn apply_result_path(input: &Value, result: &Value, path: Option<&str>) -> Value {
28 match path {
29 None | Some("$") => result.clone(),
30 Some("null") => input.clone(),
31 Some(p) => set_at_path(input, p, result),
32 }
33}
34
35pub fn resolve_path(root: &Value, path: &str) -> Value {
38 if path == "$" {
39 return root.clone();
40 }
41
42 let path = path.strip_prefix("$.").unwrap_or(path);
43 let mut current = root;
44
45 for segment in split_path_segments(path) {
46 match segment {
47 PathSegment::Field(name) => {
48 current = match current.get(name) {
49 Some(v) => v,
50 None => return Value::Null,
51 };
52 }
53 PathSegment::Index(name, idx) => {
54 current = match current.get(name) {
55 Some(v) => match v.get(idx) {
56 Some(v) => v,
57 None => return Value::Null,
58 },
59 None => return Value::Null,
60 };
61 }
62 }
63 }
64
65 current.clone()
66}
67
68fn set_at_path(root: &Value, path: &str, value: &Value) -> Value {
70 let mut result = root.clone();
71 let path = path.strip_prefix("$.").unwrap_or(path);
72 let segments: Vec<&str> = path.split('.').collect();
73
74 let mut current = &mut result;
75 for (i, segment) in segments.iter().enumerate() {
76 if i == segments.len() - 1 {
77 if let Some(obj) = current.as_object_mut() {
79 obj.insert(segment.to_string(), value.clone());
80 }
81 } else {
82 if current.get(*segment).is_none() {
84 if let Some(obj) = current.as_object_mut() {
85 obj.insert(segment.to_string(), serde_json::json!({}));
86 }
87 }
88 match current.get_mut(*segment) {
89 Some(v) => current = v,
90 None => return result, }
92 }
93 }
94
95 result
96}
97
98enum PathSegment<'a> {
99 Field(&'a str),
100 Index(&'a str, usize),
101}
102
103fn split_path_segments(path: &str) -> Vec<PathSegment<'_>> {
104 let mut segments = Vec::new();
105 for part in path.split('.') {
106 if let Some(bracket_pos) = part.find('[') {
107 let name = &part[..bracket_pos];
108 let idx_str = &part[bracket_pos + 1..part.len() - 1];
109 if let Ok(idx) = idx_str.parse::<usize>() {
110 segments.push(PathSegment::Index(name, idx));
111 } else {
112 segments.push(PathSegment::Field(part));
113 }
114 } else {
115 segments.push(PathSegment::Field(part));
116 }
117 }
118 segments
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124 use serde_json::json;
125
126 #[test]
127 fn test_resolve_path_root() {
128 let input = json!({"a": 1});
129 assert_eq!(resolve_path(&input, "$"), input);
130 }
131
132 #[test]
133 fn test_resolve_path_simple_field() {
134 let input = json!({"name": "hello", "value": 42});
135 assert_eq!(resolve_path(&input, "$.name"), json!("hello"));
136 assert_eq!(resolve_path(&input, "$.value"), json!(42));
137 }
138
139 #[test]
140 fn test_resolve_path_nested() {
141 let input = json!({"a": {"b": {"c": 99}}});
142 assert_eq!(resolve_path(&input, "$.a.b.c"), json!(99));
143 }
144
145 #[test]
146 fn test_resolve_path_missing() {
147 let input = json!({"a": 1});
148 assert_eq!(resolve_path(&input, "$.missing"), Value::Null);
149 }
150
151 #[test]
152 fn test_resolve_path_array_index() {
153 let input = json!({"items": [10, 20, 30]});
154 assert_eq!(resolve_path(&input, "$.items[0]"), json!(10));
155 assert_eq!(resolve_path(&input, "$.items[2]"), json!(30));
156 }
157
158 #[test]
159 fn test_apply_input_path_default() {
160 let input = json!({"x": 1});
161 assert_eq!(apply_input_path(&input, None), input);
162 assert_eq!(apply_input_path(&input, Some("$")), input);
163 }
164
165 #[test]
166 fn test_apply_result_path_default() {
167 let input = json!({"x": 1});
168 let result = json!({"y": 2});
169 assert_eq!(apply_result_path(&input, &result, None), result);
171 assert_eq!(apply_result_path(&input, &result, Some("$")), result);
172 }
173
174 #[test]
175 fn test_apply_result_path_null() {
176 let input = json!({"x": 1});
177 let result = json!({"y": 2});
178 assert_eq!(apply_result_path(&input, &result, Some("null")), input);
180 }
181
182 #[test]
183 fn test_apply_result_path_nested() {
184 let input = json!({"x": 1});
185 let result = json!("hello");
186 let output = apply_result_path(&input, &result, Some("$.result"));
187 assert_eq!(output, json!({"x": 1, "result": "hello"}));
188 }
189
190 #[test]
191 fn test_set_at_path_non_object_intermediate() {
192 let input = json!({"x": 42});
195 let result = json!("hello");
196 let output = apply_result_path(&input, &result, Some("$.x.nested"));
197 assert_eq!(output, json!({"x": 42}));
199 }
200
201 #[test]
202 fn test_apply_output_path() {
203 let output = json!({"a": 1, "b": 2});
204 assert_eq!(apply_output_path(&output, Some("$.a")), json!(1));
205 assert_eq!(apply_output_path(&output, None), output);
206 }
207}