1use serde::{Deserialize, Serialize};
9
10use crate::result::ExecResult;
11
12#[non_exhaustive]
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
22#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
23#[serde(rename_all = "lowercase")]
24pub enum EntryType {
25 #[default]
27 Text,
28 File,
30 Directory,
32 Executable,
34 Symlink,
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
49#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
50#[serde(default)]
51pub struct OutputNode {
52 pub name: String,
54 pub entry_type: EntryType,
56 pub text: Option<String>,
65 pub cells: Vec<String>,
67 pub children: Vec<OutputNode>,
69}
70
71impl OutputNode {
72 pub fn new(name: impl Into<String>) -> Self {
74 Self {
75 name: name.into(),
76 ..Default::default()
77 }
78 }
79
80 pub fn text(content: impl Into<String>) -> Self {
82 Self {
83 text: Some(content.into()),
84 ..Default::default()
85 }
86 }
87
88 pub fn with_entry_type(mut self, entry_type: EntryType) -> Self {
90 self.entry_type = entry_type;
91 self
92 }
93
94 pub fn with_cells(mut self, cells: Vec<String>) -> Self {
96 self.cells = cells;
97 self
98 }
99
100 pub fn with_children(mut self, children: Vec<OutputNode>) -> Self {
102 self.children = children;
103 self
104 }
105
106 pub fn with_text(mut self, text: impl Into<String>) -> Self {
108 self.text = Some(text.into());
109 self
110 }
111
112 pub fn is_text_only(&self) -> bool {
114 self.text.is_some() && self.name.is_empty() && self.cells.is_empty() && self.children.is_empty()
115 }
116
117 pub fn has_children(&self) -> bool {
119 !self.children.is_empty()
120 }
121
122 pub fn estimated_byte_size(&self) -> usize {
124 if self.children.is_empty() {
125 self.name.len() + self.text.as_ref().map_or(0, |t| t.len())
126 } else {
127 let mut size = self.name.len() + 2; for (i, child) in self.children.iter().enumerate() {
130 if i > 0 {
131 size += 1; }
133 size += child.estimated_byte_size();
134 }
135 size + 1 }
137 }
138
139 pub fn write_canonical(&self, w: &mut dyn std::io::Write, budget: usize) -> std::io::Result<usize> {
141 if self.children.is_empty() {
142 w.write_all(self.name.as_bytes())?;
143 return Ok(self.name.len());
144 }
145 let mut written = 0;
146 w.write_all(self.name.as_bytes())?;
147 written += self.name.len();
148 if written >= budget {
149 return Ok(written);
150 }
151 w.write_all(b"/{")?;
152 written += 2;
153 for (i, child) in self.children.iter().enumerate() {
154 if written >= budget {
155 break;
156 }
157 if i > 0 {
158 w.write_all(b",")?;
159 written += 1;
160 }
161 written += child.write_canonical(w, budget.saturating_sub(written))?;
162 }
163 w.write_all(b"}")?;
164 written += 1;
165 Ok(written)
166 }
167
168 pub fn display_name(&self) -> &str {
170 if self.name.is_empty() {
171 self.text.as_deref().unwrap_or("")
172 } else {
173 &self.name
174 }
175 }
176}
177
178#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
196#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
197#[serde(default)]
198#[non_exhaustive]
199pub struct OutputData {
200 pub headers: Option<Vec<String>>,
202 pub root: Vec<OutputNode>,
204 #[serde(skip)]
213 #[cfg_attr(feature = "schema", schemars(skip))]
214 pub rich_json: Option<serde_json::Value>,
215}
216
217impl OutputData {
218 pub fn new() -> Self {
220 Self::default()
221 }
222
223 pub fn text(content: impl Into<String>) -> Self {
227 Self {
228 headers: None,
229 root: vec![OutputNode::text(content)],
230 rich_json: None,
231 }
232 }
233
234 pub fn nodes(nodes: Vec<OutputNode>) -> Self {
236 Self {
237 headers: None,
238 root: nodes,
239 rich_json: None,
240 }
241 }
242
243 pub fn table(headers: Vec<String>, nodes: Vec<OutputNode>) -> Self {
245 Self {
246 headers: Some(headers),
247 root: nodes,
248 rich_json: None,
249 }
250 }
251
252 pub fn with_headers(mut self, headers: Vec<String>) -> Self {
254 self.headers = Some(headers);
255 self
256 }
257
258 pub fn with_rich_json(mut self, value: serde_json::Value) -> Self {
260 self.rich_json = Some(value);
261 self
262 }
263
264 pub fn is_simple_text(&self) -> bool {
266 self.root.len() == 1 && self.root[0].is_text_only()
267 }
268
269 pub fn is_flat(&self) -> bool {
271 self.root.iter().all(|n| !n.has_children())
272 }
273
274 pub fn is_tabular(&self) -> bool {
276 self.root.iter().any(|n| !n.cells.is_empty())
277 }
278
279 pub fn as_text(&self) -> Option<&str> {
281 if self.is_simple_text() {
282 self.root[0].text.as_deref()
283 } else {
284 None
285 }
286 }
287
288 pub fn into_text(mut self) -> Result<String, Self> {
292 if self.root.len() == 1 && self.root[0].is_text_only() {
293 Ok(self.root.pop().and_then(|n| n.text).unwrap_or_default())
294 } else {
295 Err(self)
296 }
297 }
298
299 pub fn estimated_byte_size(&self) -> usize {
304 if self.root.len() == 1 && self.root[0].is_text_only() {
305 return self.root[0].text.as_ref().map_or(0, |t| t.len());
306 }
307
308 if self.is_flat() {
309 let mut size = 0;
310 for (i, n) in self.root.iter().enumerate() {
311 if i > 0 {
312 size += 1; }
314 size += n.display_name().len();
315 for cell in &n.cells {
316 size += 1 + cell.len(); }
318 }
319 return size;
320 }
321
322 let mut size = 0;
324 for (i, n) in self.root.iter().enumerate() {
325 if i > 0 {
326 size += 1; }
328 size += n.estimated_byte_size();
329 }
330 size
331 }
332
333 pub fn write_canonical(&self, w: &mut dyn std::io::Write, budget: Option<usize>) -> std::io::Result<usize> {
339 let mut written = 0usize;
340 let budget = budget.unwrap_or(usize::MAX);
341
342 if self.root.len() == 1 && self.root[0].is_text_only() {
343 if let Some(ref text) = self.root[0].text {
344 w.write_all(text.as_bytes())?;
345 return Ok(text.len());
346 }
347 return Ok(0);
348 }
349
350 if self.is_flat() {
351 for (i, n) in self.root.iter().enumerate() {
352 if i > 0 {
353 w.write_all(b"\n")?;
354 written += 1;
355 }
356 let name = n.display_name();
357 w.write_all(name.as_bytes())?;
358 written += name.len();
359 for cell in &n.cells {
360 w.write_all(b"\t")?;
361 w.write_all(cell.as_bytes())?;
362 written += 1 + cell.len();
363 }
364 if written > budget {
365 return Ok(written);
366 }
367 }
368 return Ok(written);
369 }
370
371 for (i, n) in self.root.iter().enumerate() {
373 if i > 0 {
374 w.write_all(b"\n")?;
375 written += 1;
376 }
377 written += n.write_canonical(w, budget.saturating_sub(written))?;
378 if written > budget {
379 return Ok(written);
380 }
381 }
382 Ok(written)
383 }
384
385 pub fn to_canonical_string(&self) -> String {
394 if let Some(text) = self.as_text() {
395 return text.to_string();
396 }
397
398 if self.is_flat() {
400 return self.root.iter()
401 .map(|n| {
402 if n.cells.is_empty() {
403 n.display_name().to_string()
404 } else {
405 let mut parts = vec![n.display_name().to_string()];
407 parts.extend(n.cells.iter().cloned());
408 parts.join("\t")
409 }
410 })
411 .collect::<Vec<_>>()
412 .join("\n");
413 }
414
415 fn format_node(node: &OutputNode) -> String {
417 if node.children.is_empty() {
418 node.name.clone()
419 } else {
420 let children: Vec<String> = node.children.iter()
421 .map(format_node)
422 .collect();
423 format!("{}/{{{}}}", node.name, children.join(","))
424 }
425 }
426
427 self.root.iter()
428 .map(format_node)
429 .collect::<Vec<_>>()
430 .join("\n")
431 }
432
433 pub fn to_json(&self) -> serde_json::Value {
444 if let Some(rich) = &self.rich_json {
447 return rich.clone();
448 }
449 if let Some(text) = self.as_text() {
451 return serde_json::Value::String(text.to_string());
452 }
453
454 if let Some(ref headers) = self.headers {
459 fn row_to_json(node: &OutputNode, headers: &[String]) -> serde_json::Value {
460 let mut map = serde_json::Map::new();
461 if let Some(first) = headers.first() {
463 map.insert(first.clone(), serde_json::Value::String(node.name.clone()));
464 }
465 for (header, cell) in headers.iter().skip(1).zip(node.cells.iter()) {
467 map.insert(header.clone(), serde_json::Value::String(cell.clone()));
468 }
469 if !node.children.is_empty() {
470 let children: Vec<serde_json::Value> = node
471 .children
472 .iter()
473 .map(|child| row_to_json(child, headers))
474 .collect();
475 map.insert("children".to_string(), serde_json::Value::Array(children));
476 }
477 serde_json::Value::Object(map)
478 }
479 let rows: Vec<serde_json::Value> = self
480 .root
481 .iter()
482 .map(|node| row_to_json(node, headers))
483 .collect();
484 return serde_json::Value::Array(rows);
485 }
486
487 if !self.is_flat() {
489 fn node_to_json(node: &OutputNode) -> serde_json::Value {
490 if node.children.is_empty() {
491 serde_json::Value::Null
492 } else {
493 let mut map = serde_json::Map::new();
494 for child in &node.children {
495 map.insert(child.name.clone(), node_to_json(child));
496 }
497 serde_json::Value::Object(map)
498 }
499 }
500
501 if self.root.len() == 1 {
503 return node_to_json(&self.root[0]);
504 }
505 let mut map = serde_json::Map::new();
507 for node in &self.root {
508 map.insert(node.name.clone(), node_to_json(node));
509 }
510 return serde_json::Value::Object(map);
511 }
512
513 let items: Vec<serde_json::Value> = self.root.iter()
515 .map(|n| serde_json::Value::String(n.display_name().to_string()))
516 .collect();
517 serde_json::Value::Array(items)
518 }
519}
520
521#[non_exhaustive]
527#[derive(Debug, Clone, Copy, PartialEq, Eq)]
528pub enum OutputFormat {
529 Json,
531}
532
533pub fn apply_output_format(mut result: ExecResult, format: OutputFormat) -> ExecResult {
539 if result.is_bytes() {
542 let envelope = crate::bytes::bytes_to_envelope(result.out_bytes().unwrap_or(&[]));
543 match format {
544 OutputFormat::Json => {
545 result.set_out(
546 serde_json::to_string(&envelope).unwrap_or_else(|_| "null".to_string()),
547 );
548 result.data = Some(crate::result::json_to_value(envelope));
549 result.set_output(None);
550 }
551 }
552 return result;
553 }
554 if !result.has_output() && result.text_out().is_empty() {
555 if !result.ok() && !result.err.is_empty() {
561 match format {
562 OutputFormat::Json => {
563 let mut obj = serde_json::json!({
564 "error": result.err,
565 "code": result.code,
566 });
567 if let Some(data) = &result.data {
572 obj["data"] = crate::result::value_to_json(data);
573 }
574 if let Some(latch) = &result.latch {
579 if let Ok(v) = serde_json::to_value(latch) {
580 obj["latch"] = v;
581 }
582 }
583 let out =
584 serde_json::to_string(&obj).unwrap_or_else(|_| "null".to_string());
585 result.set_out(out);
586 result.data = Some(crate::result::json_to_value(obj));
587 }
588 }
589 }
590 return result;
591 }
592 match format {
593 OutputFormat::Json => {
594 if let Some(output) = result.output() {
595 let json_value = output.to_json();
596 result.set_out(serde_json::to_string(&json_value)
598 .unwrap_or_else(|_| "null".to_string()));
599 result.data = Some(crate::result::json_to_value(json_value));
600 } else if let Some(data) = &result.data {
601 let json_out = serde_json::to_string(&crate::result::value_to_json(data))
606 .unwrap_or_else(|_| "null".to_string());
607 result.set_out(json_out);
608 } else {
609 let text = result.text_out().into_owned();
611 let json_out = serde_json::to_string(&text)
612 .unwrap_or_else(|_| "null".to_string());
613 result.data = Some(crate::value::Value::String(text));
614 result.set_out(json_out);
615 }
616 result.set_output(None);
618 result
619 }
620 }
621}
622
623#[cfg(test)]
624mod tests {
625 use super::*;
626
627 #[test]
628 fn entry_type_variants() {
629 assert_ne!(EntryType::File, EntryType::Directory);
630 assert_ne!(EntryType::Directory, EntryType::Executable);
631 assert_ne!(EntryType::Executable, EntryType::Symlink);
632 }
633
634 #[test]
635 fn to_json_simple_text() {
636 let output = OutputData::text("hello world");
637 assert_eq!(output.to_json(), serde_json::json!("hello world"));
638 }
639
640 #[test]
641 fn to_json_flat_list() {
642 let output = OutputData::nodes(vec![
643 OutputNode::new("file1"),
644 OutputNode::new("file2"),
645 OutputNode::new("file3"),
646 ]);
647 assert_eq!(output.to_json(), serde_json::json!(["file1", "file2", "file3"]));
648 }
649
650 #[test]
651 fn to_json_table() {
652 let output = OutputData::table(
653 vec!["NAME".into(), "SIZE".into(), "TYPE".into()],
654 vec![
655 OutputNode::new("foo.rs").with_cells(vec!["1024".into(), "file".into()]),
656 OutputNode::new("bar/").with_cells(vec!["4096".into(), "dir".into()]),
657 ],
658 );
659 assert_eq!(output.to_json(), serde_json::json!([
660 {"NAME": "foo.rs", "SIZE": "1024", "TYPE": "file"},
661 {"NAME": "bar/", "SIZE": "4096", "TYPE": "dir"},
662 ]));
663 }
664
665 #[test]
666 fn to_json_table_with_children() {
667 let output = OutputData::table(
672 vec!["NAME".into(), "TYPE".into(), "SIZE".into()],
673 vec![
674 OutputNode::new(".")
675 .with_entry_type(EntryType::Directory)
676 .with_children(vec![
677 OutputNode::new("top.txt").with_cells(vec!["-".into(), "6".into()]),
678 OutputNode::new("a").with_cells(vec!["d".into(), "60".into()]),
679 ]),
680 OutputNode::new("a")
681 .with_entry_type(EntryType::Directory)
682 .with_children(vec![
683 OutputNode::new("mid.txt").with_cells(vec!["-".into(), "3".into()]),
684 ]),
685 ],
686 );
687 assert_eq!(output.to_json(), serde_json::json!([
688 {"NAME": ".", "children": [
689 {"NAME": "top.txt", "TYPE": "-", "SIZE": "6"},
690 {"NAME": "a", "TYPE": "d", "SIZE": "60"},
691 ]},
692 {"NAME": "a", "children": [
693 {"NAME": "mid.txt", "TYPE": "-", "SIZE": "3"},
694 ]},
695 ]));
696 }
697
698 #[test]
699 fn to_json_tree() {
700 let child1 = OutputNode::new("main.rs").with_entry_type(EntryType::File);
701 let child2 = OutputNode::new("utils.rs").with_entry_type(EntryType::File);
702 let subdir = OutputNode::new("lib")
703 .with_entry_type(EntryType::Directory)
704 .with_children(vec![child2]);
705 let root = OutputNode::new("src")
706 .with_entry_type(EntryType::Directory)
707 .with_children(vec![child1, subdir]);
708
709 let output = OutputData::nodes(vec![root]);
710 assert_eq!(output.to_json(), serde_json::json!({
711 "main.rs": null,
712 "lib": {"utils.rs": null},
713 }));
714 }
715
716 #[test]
717 fn to_json_tree_multiple_roots() {
718 let root1 = OutputNode::new("src")
719 .with_entry_type(EntryType::Directory)
720 .with_children(vec![OutputNode::new("main.rs")]);
721 let root2 = OutputNode::new("docs")
722 .with_entry_type(EntryType::Directory)
723 .with_children(vec![OutputNode::new("README.md")]);
724
725 let output = OutputData::nodes(vec![root1, root2]);
726 assert_eq!(output.to_json(), serde_json::json!({
727 "src": {"main.rs": null},
728 "docs": {"README.md": null},
729 }));
730 }
731
732 #[test]
733 fn to_json_empty() {
734 let output = OutputData::new();
735 assert_eq!(output.to_json(), serde_json::json!([]));
736 }
737
738 #[test]
739 fn apply_output_format_clears_sentinel() {
740 let output = OutputData::table(
741 vec!["NAME".into()],
742 vec![OutputNode::new("test")],
743 );
744 let result = ExecResult::with_output(output);
745 assert!(result.has_output(), "before: sentinel present");
746
747 let formatted = apply_output_format(result, OutputFormat::Json);
748 assert!(!formatted.has_output(), "after Json: sentinel cleared");
749 }
750
751 #[test]
752 fn apply_output_format_no_double_encoding() {
753 let output = OutputData::nodes(vec![
754 OutputNode::new("file1"),
755 OutputNode::new("file2"),
756 ]);
757 let result = ExecResult::with_output(output);
758
759 let after_json = apply_output_format(result, OutputFormat::Json);
760 let json_out = after_json.text_out().into_owned();
761 assert!(!after_json.has_output(), "sentinel cleared by Json");
762
763 let parsed: serde_json::Value = serde_json::from_str(&json_out).expect("valid JSON");
764 assert_eq!(parsed, serde_json::json!(["file1", "file2"]));
765 }
766
767 #[test]
768 fn apply_output_format_populates_data() {
769 let output = OutputData::nodes(vec![
770 OutputNode::new("file1"),
771 OutputNode::new("file2"),
772 ]);
773 let result = ExecResult::with_output(output);
774 assert!(result.data.is_none(), "before: no data on non-text output");
775
776 let formatted = apply_output_format(result, OutputFormat::Json);
777 assert!(formatted.data.is_some(), "after Json: data populated");
778
779 let data = formatted.data.unwrap();
781 assert!(matches!(data, crate::value::Value::Json(_)), "data should be Json variant");
782 if let crate::value::Value::Json(json) = data {
783 assert_eq!(json, serde_json::json!(["file1", "file2"]));
784 }
785 }
786
787 #[test]
788 fn apply_output_format_prefers_structured_data_over_text() {
789 use crate::value::Value;
793 let result = ExecResult::success_with_data("1", Value::Int(1));
794 assert!(!result.has_output(), "no OutputData sentinel on this path");
795
796 let formatted = apply_output_format(result, OutputFormat::Json);
797 let json_out = formatted.text_out().into_owned();
798 let parsed: serde_json::Value = serde_json::from_str(&json_out).expect("valid JSON");
799 assert_eq!(parsed, serde_json::json!(1), "number, not the string \"1\"");
800 assert_eq!(formatted.data, Some(Value::Int(1)));
802 }
803
804 #[test]
805 fn apply_output_format_compact_json() {
806 let output = OutputData::nodes(vec![
807 OutputNode::new("file1"),
808 OutputNode::new("file2"),
809 ]);
810 let result = ExecResult::with_output(output);
811
812 let formatted = apply_output_format(result, OutputFormat::Json);
813 let out = formatted.text_out();
815 assert!(!out.contains('\n'), "should be compact JSON, got: {}", out);
816 assert_eq!(&*out, r#"["file1","file2"]"#);
817 }
818
819 #[test]
820 fn apply_output_format_emits_json_error_object_on_failure() {
821 let result = ExecResult::failure(2, "grep: unknown flag --bogus-flag");
825 assert!(!result.has_output());
826 assert!(result.text_out().is_empty());
827
828 let formatted = apply_output_format(result, OutputFormat::Json);
829 let out = formatted.text_out().into_owned();
830 let parsed: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
831 assert_eq!(
832 parsed,
833 serde_json::json!({"error": "grep: unknown flag --bogus-flag", "code": 2})
834 );
835 assert!(matches!(formatted.data, Some(crate::value::Value::Json(_))));
837 }
838
839 #[test]
840 fn apply_output_format_preserves_structured_data_on_error() {
841 let mut result = ExecResult::failure(2, "rm: confirmation required (latch enabled)");
846 result.data = Some(crate::value::Value::Json(serde_json::json!({
847 "nonce": "a3f7b2c1",
848 "command": "rm",
849 "paths": ["important.dat"],
850 "hint": "rm --confirm=\"a3f7b2c1\" important.dat",
851 "ttl": 60,
852 })));
853
854 let formatted = apply_output_format(result, OutputFormat::Json);
855 let out = formatted.text_out().into_owned();
856 let parsed: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
857 assert_eq!(parsed["error"], "rm: confirmation required (latch enabled)");
858 assert_eq!(parsed["code"], 2);
859 assert_eq!(parsed["data"]["nonce"], "a3f7b2c1");
860 assert_eq!(parsed["data"]["ttl"], 60);
861 match &formatted.data {
863 Some(crate::value::Value::Json(v)) => assert_eq!(v["data"]["nonce"], "a3f7b2c1"),
864 other => panic!("expected nested JSON data, got {other:?}"),
865 }
866 }
867
868 #[test]
869 fn apply_output_format_leaves_clean_no_match_empty() {
870 let result = ExecResult::failure(1, "");
873 let formatted = apply_output_format(result, OutputFormat::Json);
874 assert!(formatted.text_out().is_empty());
875 assert!(formatted.data.is_none());
876 }
877
878 #[test]
879 fn apply_output_format_empty_success_stays_empty() {
880 let result = ExecResult::success("");
881 let formatted = apply_output_format(result, OutputFormat::Json);
882 assert!(formatted.text_out().is_empty());
883 assert!(formatted.data.is_none());
884 }
885
886 #[test]
887 fn estimated_byte_size_text_only_node() {
888 let node = OutputNode::text("hello world");
889 assert_eq!(node.estimated_byte_size(), 11);
891 }
892
893 #[test]
894 fn estimated_byte_size_named_node() {
895 let node = OutputNode::new("file.txt");
896 assert_eq!(node.estimated_byte_size(), 8);
897 }
898
899 #[test]
900 fn write_canonical_respects_budget() {
901 let parent = OutputNode::new("root")
902 .with_children(vec![
903 OutputNode::new("aaaa"),
904 OutputNode::new("bbbb"),
905 OutputNode::new("cccc"),
906 ]);
907 let mut buf = Vec::new();
909 let written = parent.write_canonical(&mut buf, 8).unwrap();
910 let output = String::from_utf8(buf).unwrap();
911 assert!(written <= 16, "should respect budget, wrote {} bytes: {}", written, output);
913 assert!(output.starts_with("root"), "should start with root: {}", output);
915 }
916
917 #[test]
918 fn into_text_simple() {
919 let data = OutputData::text("hello");
920 assert_eq!(data.into_text(), Ok("hello".to_string()));
921 }
922
923 #[test]
924 fn into_text_non_simple() {
925 let data = OutputData::nodes(vec![OutputNode::new("a"), OutputNode::new("b")]);
926 assert!(data.into_text().is_err());
927 }
928
929 #[test]
930 fn into_text_empty() {
931 let data = OutputData::text("");
932 assert_eq!(data.into_text(), Ok("".to_string()));
933 }
934}