1use crate::document::{Document, Node, SourceType};
4use anyhow::{Context, Result};
5use serde_json::Value;
6use std::path::Path;
7
8pub struct JsonParser;
14
15impl super::Parser for JsonParser {
16 fn extensions(&self) -> &[&str] {
17 &["json"]
18 }
19
20 fn source_type(&self) -> SourceType {
21 SourceType::Json
22 }
23
24 fn parse(&self, path: &Path, content: &str) -> Result<Document> {
25 let (doc_id, file_name) = path_parts(path);
26 let mut doc = Document::new(&doc_id, &file_name, SourceType::Json);
27
28 if content.trim().is_empty() {
29 doc.assign_node_ids();
30 return Ok(doc);
31 }
32
33 let value: Value = serde_json::from_str(content).context("failed to parse JSON")?;
34 let root = value_to_node(&file_name, &value);
35 doc.structure.push(root);
36 doc.assign_node_ids();
37 Ok(doc)
38 }
39}
40
41pub struct YamlParser;
47
48impl super::Parser for YamlParser {
49 fn extensions(&self) -> &[&str] {
50 &["yaml", "yml"]
51 }
52
53 fn source_type(&self) -> SourceType {
54 SourceType::Yaml
55 }
56
57 fn parse(&self, path: &Path, content: &str) -> Result<Document> {
58 let (doc_id, file_name) = path_parts(path);
59 let mut doc = Document::new(&doc_id, &file_name, SourceType::Yaml);
60
61 if content.trim().is_empty() {
62 doc.assign_node_ids();
63 return Ok(doc);
64 }
65
66 let yaml_value: serde_yaml::Value =
67 serde_yaml::from_str(content).context("failed to parse YAML")?;
68 let json_value = yaml_to_json(yaml_value);
69 let root = value_to_node(&file_name, &json_value);
70 doc.structure.push(root);
71 doc.assign_node_ids();
72 Ok(doc)
73 }
74}
75
76pub struct TomlParser;
82
83impl super::Parser for TomlParser {
84 fn extensions(&self) -> &[&str] {
85 &["toml"]
86 }
87
88 fn source_type(&self) -> SourceType {
89 SourceType::Toml
90 }
91
92 fn parse(&self, path: &Path, content: &str) -> Result<Document> {
93 let (doc_id, file_name) = path_parts(path);
94 let mut doc = Document::new(&doc_id, &file_name, SourceType::Toml);
95
96 if content.trim().is_empty() {
97 doc.assign_node_ids();
98 return Ok(doc);
99 }
100
101 let toml_value: toml::Value = toml::from_str(content).context("failed to parse TOML")?;
102 let json_value = toml_to_json(&toml_value);
103 let root = value_to_node(&file_name, &json_value);
104 doc.structure.push(root);
105 doc.assign_node_ids();
106 Ok(doc)
107 }
108}
109
110fn value_to_node(title: &str, value: &Value) -> Node {
121 let mut node = Node::new("", title);
122
123 match value {
124 Value::Object(map) => {
125 for (key, val) in map {
126 match val {
127 Value::Object(_) => {
128 let child = value_to_node(key, val);
129 node.children.push(child);
130 }
131 Value::Array(arr) => {
132 let child = array_to_node(key, arr);
133 node.children.push(child);
134 }
135 _ => {
136 let line = format!("{}: {}", key, format_scalar(val));
137 if node.text.is_empty() {
138 node.text = line;
139 } else {
140 node.text.push('\n');
141 node.text.push_str(&line);
142 }
143 }
144 }
145 }
146 }
147 Value::Array(arr) => {
148 let mut inner = array_to_node(title, arr);
149 node.text = std::mem::take(&mut inner.text);
150 node.children = std::mem::take(&mut inner.children);
151 }
152 _ => {
153 node.text = format_scalar(value);
154 }
155 }
156
157 node
158}
159
160fn array_to_node(title: &str, arr: &[Value]) -> Node {
163 let mut node = Node::new("", title);
164 let mut scalar_lines = Vec::new();
165
166 for (i, item) in arr.iter().enumerate() {
167 match item {
168 Value::Object(_) => {
169 let child_title = format!("{}[{}]", title, i);
170 let child = value_to_node(&child_title, item);
171 node.children.push(child);
172 }
173 Value::Array(inner) => {
174 let child_title = format!("{}[{}]", title, i);
175 let child = array_to_node(&child_title, inner);
176 node.children.push(child);
177 }
178 _ => {
179 scalar_lines.push(format!("- {}", format_scalar(item)));
180 }
181 }
182 }
183
184 if !scalar_lines.is_empty() {
185 node.text = scalar_lines.join("\n");
186 }
187
188 node
189}
190
191fn format_scalar(value: &Value) -> String {
192 match value {
193 Value::String(s) => s.clone(),
194 Value::Number(n) => n.to_string(),
195 Value::Bool(b) => b.to_string(),
196 Value::Null => "null".to_string(),
197 _ => value.to_string(),
198 }
199}
200
201fn yaml_to_json(yaml: serde_yaml::Value) -> Value {
206 match yaml {
207 serde_yaml::Value::Null => Value::Null,
208 serde_yaml::Value::Bool(b) => Value::Bool(b),
209 serde_yaml::Value::Number(n) => {
210 if let Some(i) = n.as_i64() {
211 Value::Number(i.into())
212 } else if let Some(f) = n.as_f64() {
213 serde_json::Number::from_f64(f)
214 .map(Value::Number)
215 .unwrap_or(Value::Null)
216 } else {
217 Value::Null
218 }
219 }
220 serde_yaml::Value::String(s) => Value::String(s),
221 serde_yaml::Value::Sequence(seq) => {
222 Value::Array(seq.into_iter().map(yaml_to_json).collect())
223 }
224 serde_yaml::Value::Mapping(map) => {
225 let obj = map
226 .into_iter()
227 .map(|(k, v)| {
228 let key = match k {
229 serde_yaml::Value::String(s) => s,
230 other => format!("{:?}", other),
231 };
232 (key, yaml_to_json(v))
233 })
234 .collect();
235 Value::Object(obj)
236 }
237 serde_yaml::Value::Tagged(tagged) => yaml_to_json(tagged.value),
238 }
239}
240
241fn toml_to_json(toml_val: &toml::Value) -> Value {
242 match toml_val {
243 toml::Value::String(s) => Value::String(s.clone()),
244 toml::Value::Integer(i) => Value::Number((*i).into()),
245 toml::Value::Float(f) => serde_json::Number::from_f64(*f)
246 .map(Value::Number)
247 .unwrap_or(Value::Null),
248 toml::Value::Boolean(b) => Value::Bool(*b),
249 toml::Value::Datetime(dt) => Value::String(dt.to_string()),
250 toml::Value::Array(arr) => Value::Array(arr.iter().map(toml_to_json).collect()),
251 toml::Value::Table(table) => {
252 let obj = table
253 .iter()
254 .map(|(k, v)| (k.clone(), toml_to_json(v)))
255 .collect();
256 Value::Object(obj)
257 }
258 }
259}
260
261fn path_parts(path: &Path) -> (String, String) {
266 let doc_id = path.to_string_lossy().to_string();
267 let file_name = path
268 .file_name()
269 .map(|n| n.to_string_lossy().to_string())
270 .unwrap_or_default();
271 (doc_id, file_name)
272}
273
274#[cfg(test)]
279mod tests {
280 use super::*;
281 use crate::parser::Parser;
282
283 fn parse_json(content: &str) -> Document {
286 let parser = JsonParser;
287 parser
288 .parse(Path::new("config.json"), content)
289 .expect("parse failed")
290 }
291
292 #[test]
293 fn test_json_empty() {
294 let doc = parse_json("");
295 assert!(doc.structure.is_empty());
296 }
297
298 #[test]
299 fn test_json_simple_object() {
300 let doc = parse_json(r#"{"name": "Alice", "age": 30}"#);
301 assert_eq!(doc.structure.len(), 1);
302 let root = &doc.structure[0];
303 assert!(root.text.contains("name: Alice"));
304 assert!(root.text.contains("age: 30"));
305 }
306
307 #[test]
308 fn test_json_nested_object() {
309 let doc = parse_json(r#"{"db": {"host": "localhost", "port": 5432}}"#);
310 assert_eq!(doc.structure.len(), 1);
311 let root = &doc.structure[0];
312 assert_eq!(root.children.len(), 1);
313 assert_eq!(root.children[0].title, "db");
314 assert!(root.children[0].text.contains("host: localhost"));
315 assert!(root.children[0].text.contains("port: 5432"));
316 }
317
318 #[test]
319 fn test_json_array_of_scalars() {
320 let doc = parse_json(r#"{"tags": ["a", "b", "c"]}"#);
321 let root = &doc.structure[0];
322 assert_eq!(root.children.len(), 1);
323 assert_eq!(root.children[0].title, "tags");
324 assert!(root.children[0].text.contains("- a"));
325 assert!(root.children[0].text.contains("- b"));
326 assert!(root.children[0].text.contains("- c"));
327 }
328
329 #[test]
330 fn test_json_array_of_objects() {
331 let doc = parse_json(r#"{"items": [{"id": 1}, {"id": 2}]}"#);
332 let root = &doc.structure[0];
333 let items = &root.children[0];
334 assert_eq!(items.title, "items");
335 assert_eq!(items.children.len(), 2);
336 assert_eq!(items.children[0].title, "items[0]");
337 assert_eq!(items.children[1].title, "items[1]");
338 }
339
340 #[test]
341 fn test_json_source_type() {
342 let doc = parse_json(r#"{"x": 1}"#);
343 assert_eq!(doc.source_type, SourceType::Json);
344 assert_eq!(doc.doc_id, "config.json");
345 }
346
347 #[test]
348 fn test_json_node_ids() {
349 let doc = parse_json(r#"{"a": {"b": 1}}"#);
350 assert_eq!(doc.structure[0].node_id, "0");
351 assert_eq!(doc.structure[0].children[0].node_id, "1");
352 }
353
354 #[test]
355 fn test_json_top_level_array() {
356 let doc = parse_json(r#"[1, 2, 3]"#);
357 assert_eq!(doc.structure.len(), 1);
358 assert!(doc.structure[0].text.contains("- 1"));
359 }
360
361 #[test]
362 fn test_json_scalar_value() {
363 let doc = parse_json(r#""hello world""#);
364 assert_eq!(doc.structure.len(), 1);
365 assert_eq!(doc.structure[0].text, "hello world");
366 }
367
368 fn parse_yaml(content: &str) -> Document {
371 let parser = YamlParser;
372 parser
373 .parse(Path::new("config.yaml"), content)
374 .expect("parse failed")
375 }
376
377 #[test]
378 fn test_yaml_empty() {
379 let doc = parse_yaml("");
380 assert!(doc.structure.is_empty());
381 }
382
383 #[test]
384 fn test_yaml_simple() {
385 let doc = parse_yaml("name: Bob\nage: 25");
386 assert_eq!(doc.structure.len(), 1);
387 assert!(doc.structure[0].text.contains("name: Bob"));
388 assert!(doc.structure[0].text.contains("age: 25"));
389 }
390
391 #[test]
392 fn test_yaml_nested() {
393 let content = "database:\n host: localhost\n port: 3306";
394 let doc = parse_yaml(content);
395 let root = &doc.structure[0];
396 assert_eq!(root.children.len(), 1);
397 assert_eq!(root.children[0].title, "database");
398 assert!(root.children[0].text.contains("host: localhost"));
399 }
400
401 #[test]
402 fn test_yaml_list() {
403 let content = "items:\n - one\n - two\n - three";
404 let doc = parse_yaml(content);
405 let root = &doc.structure[0];
406 let items = &root.children[0];
407 assert_eq!(items.title, "items");
408 assert!(items.text.contains("- one"));
409 }
410
411 #[test]
412 fn test_yaml_source_type() {
413 let doc = parse_yaml("x: 1");
414 assert_eq!(doc.source_type, SourceType::Yaml);
415 assert_eq!(doc.doc_id, "config.yaml");
416 }
417
418 fn parse_toml(content: &str) -> Document {
421 let parser = TomlParser;
422 parser
423 .parse(Path::new("config.toml"), content)
424 .expect("parse failed")
425 }
426
427 #[test]
428 fn test_toml_empty() {
429 let doc = parse_toml("");
430 assert!(doc.structure.is_empty());
431 }
432
433 #[test]
434 fn test_toml_simple() {
435 let doc = parse_toml("name = \"test\"\nversion = \"1.0\"");
436 assert_eq!(doc.structure.len(), 1);
437 assert!(doc.structure[0].text.contains("name: test"));
438 assert!(doc.structure[0].text.contains("version: 1.0"));
439 }
440
441 #[test]
442 fn test_toml_nested_table() {
443 let content = "[package]\nname = \"foo\"\nversion = \"0.1\"";
444 let doc = parse_toml(content);
445 let root = &doc.structure[0];
446 assert_eq!(root.children.len(), 1);
447 assert_eq!(root.children[0].title, "package");
448 assert!(root.children[0].text.contains("name: foo"));
449 }
450
451 #[test]
452 fn test_toml_source_type() {
453 let doc = parse_toml("x = 1");
454 assert_eq!(doc.source_type, SourceType::Toml);
455 assert_eq!(doc.doc_id, "config.toml");
456 }
457
458 #[test]
459 fn test_toml_array_of_tables() {
460 let content = "[[servers]]\nname = \"alpha\"\n\n[[servers]]\nname = \"beta\"";
461 let doc = parse_toml(content);
462 let root = &doc.structure[0];
463 let servers = root
464 .children
465 .iter()
466 .find(|c| c.title == "servers")
467 .expect("servers node");
468 assert_eq!(servers.children.len(), 2);
469 }
470
471 #[test]
474 fn test_value_to_node_scalar() {
475 let node = value_to_node("root", &Value::String("hello".into()));
476 assert_eq!(node.title, "root");
477 assert_eq!(node.text, "hello");
478 assert!(node.children.is_empty());
479 }
480
481 #[test]
482 fn test_value_to_node_deeply_nested() {
483 let json: Value = serde_json::from_str(r#"{"a": {"b": {"c": "deep"}}}"#).unwrap();
484 let node = value_to_node("root", &json);
485 assert_eq!(node.children[0].title, "a");
486 assert_eq!(node.children[0].children[0].title, "b");
487 assert!(node.children[0].children[0].text.contains("c: deep"));
488 }
489
490 #[test]
491 fn test_format_scalar_types() {
492 assert_eq!(format_scalar(&Value::String("hi".into())), "hi");
493 assert_eq!(format_scalar(&Value::Bool(true)), "true");
494 assert_eq!(format_scalar(&Value::Null), "null");
495 assert_eq!(
496 format_scalar(&Value::Number(serde_json::Number::from(42))),
497 "42"
498 );
499 }
500
501 #[test]
502 fn test_yaml_to_json_roundtrip() {
503 let yaml: serde_yaml::Value = serde_yaml::from_str("key: value\nnum: 42").unwrap();
504 let json = yaml_to_json(yaml);
505 assert_eq!(json["key"], Value::String("value".into()));
506 assert_eq!(json["num"], Value::Number(42.into()));
507 }
508
509 #[test]
510 fn test_toml_to_json_roundtrip() {
511 let toml_val: toml::Value = toml::from_str("key = \"value\"\nnum = 42").unwrap();
512 let json = toml_to_json(&toml_val);
513 assert_eq!(json["key"], Value::String("value".into()));
514 assert_eq!(json["num"], Value::Number(42.into()));
515 }
516}