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