Skip to main content

flowscope_export/
lib.rs

1//! Database export for FlowScope analysis results.
2//!
3//! Exports `AnalyzeResult` to queryable database formats (DuckDB, SQLite).
4//!
5//! Two export modes are available:
6//! - **Binary export** (`export_duckdb`): Creates a DuckDB database file (native only)
7//! - **SQL export** (`export_sql`): Generates DDL + INSERT statements (WASM-compatible)
8
9mod csv;
10mod error;
11mod extract;
12mod html;
13mod json;
14mod mermaid;
15mod naming;
16mod schema;
17mod sql_backend;
18mod xlsx;
19
20#[cfg(feature = "duckdb")]
21mod duckdb_backend;
22
23pub use error::ExportError;
24pub use extract::{ColumnMapping, ScriptInfo, TableDependency, TableInfo, TableType};
25pub use mermaid::MermaidView;
26pub use naming::ExportNaming;
27
28use flowscope_core::AnalyzeResult;
29
30/// Supported export formats for filenames and UI integrations.
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum ExportFormat {
33    DuckDb,
34    Sql { schema: bool },
35    Json { compact: bool },
36    Mermaid { view: MermaidView },
37    Html,
38    CsvBundle,
39    Xlsx,
40    Png,
41}
42
43pub type Format = ExportFormat;
44
45/// Export analysis result to a database file.
46///
47/// Returns raw bytes of the database file.
48/// Only works with `ExportFormat::DuckDb` when the `duckdb` feature is enabled.
49pub fn export(result: &AnalyzeResult, format: ExportFormat) -> Result<Vec<u8>, ExportError> {
50    match format {
51        #[cfg(feature = "duckdb")]
52        ExportFormat::DuckDb => duckdb_backend::export(result),
53        #[cfg(not(feature = "duckdb"))]
54        ExportFormat::DuckDb => Err(ExportError::UnsupportedFormat("DuckDB feature not enabled")),
55        ExportFormat::Sql { .. } => Ok(sql_backend::export_sql(result, None)?.into_bytes()),
56        ExportFormat::Json { compact } => Ok(json::export_json(result, compact)?.into_bytes()),
57        ExportFormat::Mermaid { view } => Ok(mermaid::export_mermaid(result, view).into_bytes()),
58        ExportFormat::Html => {
59            Ok(html::export_html(result, "FlowScope", chrono::Utc::now()).into_bytes())
60        }
61        ExportFormat::CsvBundle => csv::export_csv_bundle(result),
62        ExportFormat::Xlsx => xlsx::export_xlsx(result),
63        ExportFormat::Png => Err(ExportError::UnsupportedFormat("PNG export is UI-only")),
64    }
65}
66
67/// Export analysis result to DuckDB format.
68///
69/// Requires the `duckdb` feature (native only, not WASM-compatible).
70#[cfg(feature = "duckdb")]
71pub fn export_duckdb(result: &AnalyzeResult) -> Result<Vec<u8>, ExportError> {
72    duckdb_backend::export(result)
73}
74
75/// Export analysis result as SQL statements.
76///
77/// Returns DDL (CREATE TABLE/VIEW) + INSERT statements that can be
78/// executed by duckdb-wasm in the browser.
79///
80/// This is the WASM-compatible export path.
81///
82/// If `schema` is provided, all tables and views will be prefixed with that schema
83/// (e.g., "myschema.tablename") and a `CREATE SCHEMA IF NOT EXISTS` statement will be added.
84pub fn export_sql(result: &AnalyzeResult, schema: Option<&str>) -> Result<String, ExportError> {
85    sql_backend::export_sql(result, schema)
86}
87
88pub fn export_json(result: &AnalyzeResult, compact: bool) -> Result<String, ExportError> {
89    json::export_json(result, compact)
90}
91
92pub fn export_mermaid(result: &AnalyzeResult, view: MermaidView) -> Result<String, ExportError> {
93    Ok(mermaid::export_mermaid(result, view))
94}
95
96pub fn export_csv_bundle(result: &AnalyzeResult) -> Result<Vec<u8>, ExportError> {
97    csv::export_csv_bundle(result)
98}
99
100pub fn export_xlsx(result: &AnalyzeResult) -> Result<Vec<u8>, ExportError> {
101    xlsx::export_xlsx(result)
102}
103
104pub fn export_html(
105    result: &AnalyzeResult,
106    project_name: &str,
107    exported_at: chrono::DateTime<chrono::Utc>,
108) -> Result<String, ExportError> {
109    Ok(html::export_html(result, project_name, exported_at))
110}