start reworking config
This commit is contained in:
68
src/main.rs
68
src/main.rs
@@ -1,32 +1,53 @@
|
||||
mod generate;
|
||||
use std::{fs, path::PathBuf};
|
||||
use std::{fs, path::PathBuf, str::FromStr};
|
||||
|
||||
use clap::Parser;
|
||||
use color_eyre::Result;
|
||||
use color_eyre::{Report, Result};
|
||||
use figment::{
|
||||
providers::{Format, Serialized, Toml},
|
||||
Figment,
|
||||
};
|
||||
use sea_orm_codegen::{
|
||||
DateTimeCrate as CodegenDateTimeCrate, EntityTransformer, EntityWriterContext, WithPrelude,
|
||||
WithSerde,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
struct Config {
|
||||
sea_orm: SeaOrmConfig,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
enum DateTimeCrate {
|
||||
Time,
|
||||
Chrono,
|
||||
}
|
||||
|
||||
impl From<DateTimeCrate> for CodegenDateTimeCrate {
|
||||
fn from(date_time_crate: DateTimeCrate) -> CodegenDateTimeCrate {
|
||||
match date_time_crate {
|
||||
DateTimeCrate::Chrono => CodegenDateTimeCrate::Chrono,
|
||||
DateTimeCrate::Time => CodegenDateTimeCrate::Time,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
struct SeaOrmConfig {
|
||||
expanded_format: bool,
|
||||
table: SeaOrmTableConfig,
|
||||
include_hidden_tables: bool,
|
||||
with_serde: bool,
|
||||
with_serde: String,
|
||||
with_prelude: String,
|
||||
with_copy_enums: bool,
|
||||
serde_skip_deserializing_primary_key: bool,
|
||||
serde_skip_hidden_column: bool,
|
||||
max_connections: u32,
|
||||
acquire_timeout: u64,
|
||||
database_schema: Option<String>,
|
||||
date_format: DateTimeCrate,
|
||||
}
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
struct SeaOrmTableConfig {
|
||||
include_hidden: bool,
|
||||
only: Vec<String>,
|
||||
@@ -46,15 +67,40 @@ impl Default for Config {
|
||||
max_connections: 10,
|
||||
acquire_timeout: 30,
|
||||
expanded_format: false,
|
||||
include_hidden_tables: false,
|
||||
with_serde: false,
|
||||
with_copy_enums: false,
|
||||
with_serde: String::from("none"),
|
||||
with_prelude: String::from("none"),
|
||||
serde_skip_hidden_column: false,
|
||||
serde_skip_deserializing_primary_key: false,
|
||||
date_format: DateTimeCrate::Time,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryInto<EntityWriterContext> for Config {
|
||||
type Error = Report;
|
||||
fn try_into(self) -> Result<EntityWriterContext> {
|
||||
Ok(EntityWriterContext::new(
|
||||
self.sea_orm.expanded_format,
|
||||
WithPrelude::from_str(&self.sea_orm.with_prelude)?,
|
||||
WithSerde::from_str(&self.sea_orm.with_serde)?,
|
||||
self.sea_orm.with_copy_enums,
|
||||
self.sea_orm.date_format.into(),
|
||||
self.sea_orm.database_schema,
|
||||
false,
|
||||
self.sea_orm.serde_skip_deserializing_primary_key,
|
||||
self.sea_orm.serde_skip_hidden_column,
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
false,
|
||||
false,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct Args {
|
||||
#[clap(short, long, default_value = "generator.toml")]
|
||||
@@ -73,6 +119,8 @@ async fn main() -> Result<()> {
|
||||
.extract()?;
|
||||
tracing::info!(?config);
|
||||
tracing::info!(?args);
|
||||
generate::get_tables(args.database_url, config).await?;
|
||||
let (_, table_stmts) = generate::get_tables(args.database_url, &config).await?;
|
||||
let writer_context = config.clone().try_into()?;
|
||||
let output = EntityTransformer::transform(table_stmts)?.generate(&writer_context);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user