Skip to main content

f00_format/
json.rs

1use f00_core::Entry;
2use serde::Serialize;
3
4#[derive(Serialize)]
5struct JsonEntry<'a> {
6    name: &'a str,
7    path: String,
8    kind: &'static str,
9    size: u64,
10    modified: Option<String>,
11    mode: String,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    symlink_target: Option<String>,
14    git_status: &'static str,
15    depth: usize,
16}
17
18/// Serialize entries (skipping directory headers) as pretty JSON.
19pub fn format_json(entries: &[Entry]) -> Result<String, serde_json::Error> {
20    let items: Vec<JsonEntry<'_>> = entries
21        .iter()
22        .filter(|e| !e.is_dir_header)
23        .map(|e| JsonEntry {
24            name: &e.name,
25            path: e.path.display().to_string(),
26            kind: e.kind.as_str(),
27            size: e.size,
28            modified: e.modified_datetime().map(|dt| dt.to_rfc3339()),
29            mode: format!("{:o}", e.mode),
30            symlink_target: e.symlink_target.as_ref().map(|p| p.display().to_string()),
31            git_status: e.git_status.as_str(),
32            depth: e.depth,
33        })
34        .collect();
35
36    serde_json::to_string_pretty(&items)
37}