Skip to main content

schema_installer/
config.rs

1use std::path::PathBuf;
2use schema_model::model::types::{BooleanMode, ForeignKeyMode};
3use schema_sql_generator::common::generator_type::GeneratorType;
4use crate::error::SchemaInstallerError;
5
6pub struct SchemaInstallerConfig {
7    pub database_type: GeneratorType,
8    pub connection_string: String,
9    pub schema_file: Option<PathBuf>,
10    pub boolean_mode: BooleanMode,
11    pub foreign_key_mode: ForeignKeyMode,
12}
13
14pub struct SchemaInstallerConfigBuilder {
15    database_type: Option<GeneratorType>,
16    connection_string: Option<String>,
17    schema_file: Option<PathBuf>,
18    boolean_mode: BooleanMode,
19    foreign_key_mode: ForeignKeyMode,
20}
21
22impl SchemaInstallerConfigBuilder {
23    pub fn new() -> Self {
24        Self {
25            database_type: None,
26            connection_string: None,
27            schema_file: None,
28            boolean_mode: BooleanMode::Native,
29            foreign_key_mode: ForeignKeyMode::Relations,
30        }
31    }
32
33    pub fn database_type(mut self, db_type: GeneratorType) -> Self {
34        self.database_type = Some(db_type);
35        self
36    }
37
38    pub fn connection_string(mut self, conn_str: String) -> Self {
39        self.connection_string = Some(conn_str);
40        self
41    }
42
43    pub fn schema_file(mut self, path: PathBuf) -> Self {
44        self.schema_file = Some(path);
45        self
46    }
47
48    pub fn boolean_mode(mut self, mode: BooleanMode) -> Self {
49        self.boolean_mode = mode;
50        self
51    }
52
53    pub fn foreign_key_mode(mut self, mode: ForeignKeyMode) -> Self {
54        self.foreign_key_mode = mode;
55        self
56    }
57
58    pub fn build(self) -> Result<SchemaInstallerConfig, SchemaInstallerError> {
59        let database_type = self.database_type
60            .ok_or_else(|| SchemaInstallerError::InvalidConfiguration("database_type required".to_string()))?;
61        let connection_string = self.connection_string
62            .ok_or_else(|| SchemaInstallerError::InvalidConfiguration("connection_string required".to_string()))?;
63
64        if let Some(ref schema_file) = self.schema_file {
65            if !schema_file.exists() {
66                return Err(SchemaInstallerError::SchemaFileNotFound(schema_file.display().to_string()));
67            }
68        }
69
70        Ok(SchemaInstallerConfig {
71            database_type,
72            connection_string,
73            schema_file: self.schema_file,
74            boolean_mode: self.boolean_mode,
75            foreign_key_mode: self.foreign_key_mode,
76        })
77    }
78}
79
80impl Default for SchemaInstallerConfigBuilder {
81    fn default() -> Self {
82        Self::new()
83    }
84}