sqlant/
lib.rs

1use strum_macros::{Display, EnumString};
2
3pub mod error;
4pub mod mermaid_generator;
5pub mod plantuml_generator;
6pub mod psql_erd_loader;
7pub mod sql_entities;
8
9pub use error::SqlantError;
10use mermaid_generator::MermaidGenerator;
11use plantuml_generator::PlantUmlDefaultGenerator;
12use psql_erd_loader::PostgreSqlERDLoader;
13use sql_entities::{SqlERData, SqlERDataLoader};
14
15pub struct GeneratorConfigOptions {
16    pub not_null: bool,
17    pub draw_enums: bool,
18    pub draw_legend: bool,
19    pub inline_puml_lib: bool,
20    pub conceptual_diagram: bool,
21}
22
23pub trait ViewGenerator {
24    fn generate(
25        &self,
26        sql_erd: SqlERData,
27        opts: &GeneratorConfigOptions,
28    ) -> Result<String, SqlantError>;
29}
30
31pub async fn lookup_parser(
32    connection_string: &str,
33    schema_name: String,
34) -> Result<Box<dyn SqlERDataLoader>, SqlantError> {
35    Ok(Box::new(
36        PostgreSqlERDLoader::new(connection_string, schema_name).await?,
37    ))
38}
39
40#[derive(Clone, Debug, Display, EnumString, Eq, PartialEq, PartialOrd, Ord)]
41#[strum(serialize_all = "lowercase")]
42pub enum GeneratorType {
43    PlantUML,
44    Mermaid,
45}
46
47// If you want to add generator, you need to add input parameter
48// for this function.
49// To distinguish what exactly generator you need.
50pub fn get_generator(generator_type: GeneratorType) -> Result<Box<dyn ViewGenerator>, SqlantError> {
51    match generator_type {
52        GeneratorType::PlantUML => Ok(Box::new(PlantUmlDefaultGenerator::new()?)),
53        GeneratorType::Mermaid => Ok(Box::new(MermaidGenerator::new()?)),
54    }
55}