erdify-rs 1.2.0

CLI tool to generate Mermaid ER diagrams from PostgreSQL databases
Documentation
//! Generates Mermaid ER diagrams from a PostgreSQL database.

pub mod config;
pub mod db;
pub mod errors;
pub mod mermaid;
pub mod schema;

use crate::config::Args;
use crate::errors::ErdifyError;
use std::io::Write as _;

/// Application entry point.
///
/// # Errors
///
/// Returns an [ErdifyError] if the url is invalid, if the connection or a
/// query fails, if no table matches the filters, or if writing the output
/// file fails.
pub async fn run(args: Args) -> Result<(), ErdifyError> {
    let url_info = args.parse_url()?;

    let schema_filters = args.parse_csv(args.schema.as_deref());
    let table_filters = args.parse_csv(args.table.as_deref());
    let ignore_tables = args.parse_csv(args.ignore_tables.as_deref());
    let mode = args.output_mode();

    let client = db::connect(&url_info).await?;
    let tables = db::fetch_tables(&client, &schema_filters, &table_filters, &ignore_tables).await?;

    if tables.is_empty() {
        return Err(ErdifyError::NoTablesFound);
    }

    let output = mermaid::render_all(&tables, mode, &args, &url_info.database);

    if let Some(path) = &args.output {
        tokio::fs::write(path, output).await?;
    } else {
        // `output` already ends with a newline: `print!` avoids the extra
        // blank line that `println!` would add.
        let mut stdout = std::io::stdout().lock();
        stdout.write_all(output.as_bytes())?;
        stdout.flush()?;
    }

    Ok(())
}