Skip to main content

context_harness/
export.rs

1//! Export the search index as JSON for static site search.
2//!
3//! Produces a `data.json` file containing all documents and chunks,
4//! suitable for use with `ctx-search.js` on static sites. Replaces
5//! the Python one-liner previously used in `build-docs.sh`.
6
7use anyhow::Result;
8use std::path::Path;
9
10use crate::app_store::{AppStore, SqliteAppStore};
11use crate::config::Config;
12
13/// Export documents and chunks as JSON.
14///
15/// If `output` is `Some`, writes to that file path. Otherwise writes
16/// to stdout for piping.
17pub async fn run_export(config: &Config, output: Option<&Path>) -> Result<()> {
18    let store = SqliteAppStore::connect(config).await?;
19    let data = store.export_index().await?;
20    let doc_count = data.documents.len();
21    let chunk_count = data.chunks.len();
22    let json = serde_json::to_string_pretty(&data)?;
23
24    match output {
25        Some(path) => {
26            if let Some(parent) = path.parent() {
27                std::fs::create_dir_all(parent)?;
28            }
29            std::fs::write(path, &json)?;
30            eprintln!(
31                "Exported {} documents, {} chunks to {}",
32                doc_count,
33                chunk_count,
34                path.display()
35            );
36        }
37        None => {
38            println!("{}", json);
39        }
40    }
41
42    store.close().await;
43    Ok(())
44}