use std::collections::BTreeMap;
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Config {
pub schema: Vec<PathBuf>,
pub queries: Vec<PathBuf>,
pub output: PathBuf,
pub resource: Option<String>,
pub dry_run: bool,
pub check: bool,
pub force: bool,
pub lenient: bool,
pub conventions: Conventions,
pub derive: Vec<Derive>,
pub extractors: Vec<Extractor>,
pub relationships: BTreeMap<String, BTreeMap<String, RelationshipChange>>,
pub modules: BTreeMap<String, String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RelationshipChange {
Rename(String),
OptOut,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Derive {
Serde,
Validate,
}
impl Derive {
pub fn name(self) -> &'static str {
match self {
Derive::Serde => "serde",
Derive::Validate => "validate",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Extractor {
Axum,
ActixWeb,
}
impl Extractor {
pub fn name(self) -> &'static str {
match self {
Extractor::Axum => "axum",
Extractor::ActixWeb => "actix_web",
}
}
}
impl Default for Config {
fn default() -> Self {
Self {
schema: Vec::new(),
queries: Vec::new(),
output: PathBuf::from("src/db"),
resource: None,
dry_run: false,
check: false,
force: false,
lenient: false,
conventions: Conventions::default(),
derive: Vec::new(),
extractors: Vec::new(),
relationships: BTreeMap::new(),
modules: BTreeMap::new(),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Conventions {
pub tenant_column: Option<String>,
pub tenant_setting: Option<String>,
pub user_setting: Option<String>,
pub soft_delete_column: Option<String>,
pub managed_columns: Vec<String>,
pub version_column: Option<String>,
pub primary_key_ids: Option<IdGeneration>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IdGeneration {
UuidV4,
UuidV7,
Ulid,
}
impl IdGeneration {
pub fn name(self) -> &'static str {
match self {
IdGeneration::UuidV4 => "uuid_v4",
IdGeneration::UuidV7 => "uuid_v7",
IdGeneration::Ulid => "ulid",
}
}
}
impl Conventions {
pub const PRESETS: &[&str] = &["rok"];
pub fn rok() -> Self {
Self {
tenant_column: Some("organization_id".to_owned()),
tenant_setting: Some("app.organization_id".to_owned()),
user_setting: Some("app.user_id".to_owned()),
soft_delete_column: Some("deleted_at".to_owned()),
managed_columns: vec!["updated_at".to_owned(), "row_version".to_owned()],
version_column: Some("row_version".to_owned()),
primary_key_ids: None,
}
}
pub fn preset(name: &str) -> Option<Self> {
match name {
"rok" => Some(Self::rok()),
_ => None,
}
}
}