use std::{error::Error, ffi::OsString};
use inflector::Inflector;
use super::transpile;
#[derive(Debug, PartialEq, Clone)]
pub enum Target {
Proto3,
}
#[derive(Debug, PartialEq, Clone)]
pub struct Config {
pub in_path: OsString,
pub out_path: OsString,
pub target: Target,
pub package_name: String,
pub table_name_transform_fn: fn(Option<&str>, &str, Option<&str>) -> String,
pub enum_name_transform_fn: fn(Option<&str>, &str) -> String,
pub is_with_update_schema: bool,
pub is_with_create_schema: bool,
pub is_create_schema_primary_key_included: Option<bool>,
pub is_update_schema_primary_key_included: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
in_path: OsString::from(""),
out_path: OsString::from(""),
target: Target::Proto3,
package_name: String::new(),
table_name_transform_fn: |_: Option<&str>, name: &str, _: Option<&str>| {
format!("{}Schema", name.to_pascal_case())
},
enum_name_transform_fn: |_: Option<&str>, name: &str| {
format!("{}Enum", name.to_pascal_case())
},
is_with_update_schema: false,
is_with_create_schema: false,
is_create_schema_primary_key_included: None,
is_update_schema_primary_key_included: false,
}
}
}
impl Config {
pub fn validate(&self) -> Option<&str> {
if self.in_path.is_empty() {
Some("in_path is not set")
} else if self.out_path.is_empty() {
Some("out_path is not set")
} else {
None
}
}
pub fn transpile(&self) -> Result<String, Box<dyn Error>> {
let sem_ast = dbml_rs::parse_file(&self.in_path)?;
let result = transpile(sem_ast, &self)?;
Ok(result)
}
}