Skip to main content

kimun_notes/cli/
json_output.rs

1use crate::cli::metadata_extractor::{extract_headers, extract_links, extract_tags};
2use chrono::Utc;
3use kimun_core::NoteVault;
4use kimun_core::nfs::NoteEntryData;
5use kimun_core::nfs::VaultPath;
6use kimun_core::note::NoteContentData;
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10#[derive(Debug, Serialize, Deserialize, Clone)]
11pub struct JsonHeader {
12    pub level: u32,
13    pub text: String,
14}
15
16#[derive(Debug, Serialize, Deserialize)]
17pub struct JsonOutputMetadata {
18    pub workspace: String,
19    pub workspace_path: String,
20    pub total_results: usize,
21    pub query: Option<String>,
22    pub is_listing: bool,
23    pub generated_at: String,
24}
25
26/// Nested note-level metadata extracted from content
27#[derive(Debug, Serialize, Deserialize)]
28pub struct JsonNoteMetadata {
29    pub tags: Vec<String>,
30    pub links: Vec<String>,
31    pub headers: Vec<JsonHeader>,
32}
33
34#[derive(Debug, Serialize, Deserialize)]
35pub struct JsonNoteEntry {
36    pub path: String,
37    pub title: String,
38    pub content: String,
39    pub size: u64,
40    pub modified: u64,
41    pub created: u64,
42    pub hash: String,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub journal_date: Option<String>,
45    pub metadata: JsonNoteMetadata,
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub backlinks: Option<Vec<String>>,
48}
49
50#[derive(Debug, Serialize, Deserialize)]
51pub struct JsonOutput {
52    pub metadata: JsonOutputMetadata,
53    pub notes: Vec<JsonNoteEntry>,
54}
55
56/// Format note entries with their content as JSON output.
57pub fn format_notes_with_content_as_json(
58    vault: &NoteVault,
59    entries: &[(NoteEntryData, NoteContentData)],
60    content_map: &[(VaultPath, String)],
61    workspace_name: &str,
62    workspace_path: &str,
63    query: Option<&str>,
64    is_listing: bool,
65) -> Result<String, Box<dyn std::error::Error>> {
66    let output_metadata = JsonOutputMetadata {
67        workspace: workspace_name.to_string(),
68        workspace_path: workspace_path.to_string(),
69        total_results: entries.len(),
70        query: query.map(|q| q.to_string()),
71        is_listing,
72        generated_at: Utc::now().to_rfc3339(),
73    };
74
75    let content_lookup: HashMap<String, &str> = content_map
76        .iter()
77        .map(|(p, c)| (p.to_string(), c.as_str()))
78        .collect();
79
80    let notes = entries
81        .iter()
82        .map(|(entry_data, content_data)| {
83            let path_str = entry_data.path.to_string();
84            let path_with_ext = entry_data.path.to_string_with_ext();
85
86            let content: &str = content_lookup.get(&path_str).copied().unwrap_or("");
87
88            let tags = extract_tags(content);
89            let links = extract_links(content);
90            let headers = extract_headers(content);
91
92            // Detect journal date using vault
93            let journal_date = vault
94                .journal_date(&entry_data.path)
95                .map(|d| d.format("%Y-%m-%d").to_string());
96
97            // Use modified as created (fallback until created timestamp is tracked separately)
98            let created = entry_data.modified_secs;
99
100            JsonNoteEntry {
101                path: path_with_ext,
102                title: content_data.title.clone(),
103                content: content.to_owned(),
104                size: entry_data.size,
105                modified: entry_data.modified_secs,
106                created,
107                hash: format!("{:x}", content_data.hash),
108                journal_date,
109                metadata: JsonNoteMetadata {
110                    tags,
111                    links,
112                    headers,
113                },
114                backlinks: None,
115            }
116        })
117        .collect();
118
119    let output = JsonOutput {
120        metadata: output_metadata,
121        notes,
122    };
123    Ok(serde_json::to_string(&output)?)
124}
125
126/// Format note entries as JSON output, fetching note content from vault
127pub async fn format_notes_as_json(
128    vault: &NoteVault,
129    entries: &[(NoteEntryData, NoteContentData)],
130    workspace_name: &str,
131    query: Option<&str>,
132    is_listing: bool,
133) -> Result<String, Box<dyn std::error::Error>> {
134    let workspace_path = vault.workspace_path().to_string_lossy().to_string();
135
136    // Fetch actual content for each note concurrently — bounded so a vault
137    // with thousands of entries doesn't open thousands of file descriptors.
138    const JSON_FETCH_CONCURRENCY: usize = 32;
139    use futures::stream::StreamExt;
140    let content_results: Vec<_> = futures::stream::iter(entries.iter().map(|(entry_data, _)| {
141        let path = entry_data.path.clone();
142        async move {
143            match vault.get_note_text(&path).await {
144                Ok(content) => Some((path, content)),
145                Err(_) => None,
146            }
147        }
148    }))
149    .buffered(JSON_FETCH_CONCURRENCY)
150    .collect()
151    .await;
152    let content_map: Vec<_> = content_results.into_iter().flatten().collect();
153
154    format_notes_with_content_as_json(
155        vault,
156        entries,
157        &content_map,
158        workspace_name,
159        &workspace_path,
160        query,
161        is_listing,
162    )
163}