jtool_grep/output/
jsonl.rs1use super::OutputFormatter;
4use crate::types::GrepResult;
5use anyhow::Result;
6use serde_json::json;
7
8pub struct JsonlFormatter;
10
11impl JsonlFormatter {
12 pub fn new() -> Self {
13 Self
14 }
15}
16
17impl Default for JsonlFormatter {
18 fn default() -> Self {
19 Self::new()
20 }
21}
22
23impl OutputFormatter for JsonlFormatter {
24 fn format_result(&self, result: &GrepResult) -> Result<String> {
25 let mut output = String::new();
26
27 for m in &result.matches {
28 let obj = json!({
29 "notebook": result.notebook,
30 "cell_index": m.cell_index,
31 "cell_number": m.cell_number,
32 "execution_count": m.execution_count,
33 "match_type": m.match_type,
34 "line_index": m.line_index,
35 "line_number": m.line_number,
36 "matched_text": m.matched_text,
37 "line_content": m.line_content,
38 });
39
40 output.push_str(&serde_json::to_string(&obj)?);
41 output.push('\n');
42 }
43
44 Ok(output)
45 }
46
47 fn format_results(&self, results: &[GrepResult]) -> Result<String> {
48 let mut output = String::new();
49
50 for result in results {
51 for m in &result.matches {
52 let obj = json!({
53 "notebook": result.notebook,
54 "cell_index": m.cell_index,
55 "cell_number": m.cell_number,
56 "execution_count": m.execution_count,
57 "match_type": m.match_type,
58 "line_index": m.line_index,
59 "line_number": m.line_number,
60 "matched_text": m.matched_text,
61 "line_content": m.line_content,
62 });
63
64 output.push_str(&serde_json::to_string(&obj)?);
65 output.push('\n');
66 }
67 }
68
69 Ok(output)
70 }
71}