|
| 1 | +use std::panic::Location; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use derive_builder::Builder; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +use super::driver::Driver::{self, Sqlite3}; |
| 8 | +use super::driver::{self}; |
| 9 | +use super::error::Error; |
| 10 | +use super::{mysql, sqlite}; |
| 11 | + |
| 12 | +#[derive(Builder, Default, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Debug, Clone, Hash)] |
| 13 | +#[builder(default, pattern = "immutable")] |
| 14 | +pub struct Settings { |
| 15 | + #[builder(default = "driver::Driver::default()")] |
| 16 | + pub driver: driver::Driver, |
| 17 | + #[builder(default = "self.sql_lite_path_default()")] |
| 18 | + sql_lite_3_db_file_path: Option<Box<Path>>, |
| 19 | + my_sql_connection_url: Option<String>, |
| 20 | +} |
| 21 | + |
| 22 | +impl SettingsBuilder { |
| 23 | + // Private helper method that will set the default database path if the database is Sqlite. |
| 24 | + #[allow(clippy::unused_self)] |
| 25 | + fn sql_lite_path_default(&self) -> Option<Box<Path>> { |
| 26 | + if let Sqlite3 = driver::Driver::default() { |
| 27 | + Some(Path::new("data.db").into()) |
| 28 | + } else { |
| 29 | + None |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl Settings { |
| 35 | + /// Returns the check of this [`Settings`]. |
| 36 | + /// |
| 37 | + /// # Errors |
| 38 | + /// |
| 39 | + /// This function will return an error if unable to transform into a definite database setting. |
| 40 | + pub fn check(&self) -> Result<(), Error> { |
| 41 | + match self.driver { |
| 42 | + Driver::Sqlite3 => { |
| 43 | + sqlite::Settings::try_from(self)?; |
| 44 | + } |
| 45 | + Driver::MySQL => { |
| 46 | + mysql::Settings::try_from(self)?; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + Ok(()) |
| 51 | + } |
| 52 | + |
| 53 | + pub fn get_sqlite_settings(&self) -> Result<sqlite::Settings, Error> { |
| 54 | + sqlite::Settings::try_from(self) |
| 55 | + } |
| 56 | + |
| 57 | + pub fn get_mysql_settings(&self) -> Result<mysql::Settings, Error> { |
| 58 | + mysql::Settings::try_from(self) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(PartialEq, PartialOrd, Ord, Eq, Debug, Clone, Hash)] |
| 63 | +pub struct OldConfig { |
| 64 | + pub db_driver: driver::Driver, |
| 65 | + pub db_path: String, |
| 66 | +} |
| 67 | + |
| 68 | +impl TryFrom<&OldConfig> for Settings { |
| 69 | + type Error = Error; |
| 70 | + |
| 71 | + fn try_from(value: &OldConfig) -> Result<Self, Self::Error> { |
| 72 | + Ok(match value.db_driver { |
| 73 | + Driver::Sqlite3 => SettingsBuilder::default() |
| 74 | + .driver(Driver::Sqlite3) |
| 75 | + .sql_lite_3_db_file_path(Some(Path::new(&value.db_path).into())) |
| 76 | + .build() |
| 77 | + .unwrap(), |
| 78 | + Driver::MySQL => SettingsBuilder::default() |
| 79 | + .driver(Driver::MySQL) |
| 80 | + .my_sql_connection_url(Some(value.db_path.clone())) |
| 81 | + .build() |
| 82 | + .unwrap(), |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl TryFrom<&Settings> for sqlite::Settings { |
| 88 | + type Error = Error; |
| 89 | + |
| 90 | + fn try_from(value: &Settings) -> Result<Self, Self::Error> { |
| 91 | + Ok(Self { |
| 92 | + database_file_path: match value.driver { |
| 93 | + Driver::Sqlite3 => match &value.sql_lite_3_db_file_path { |
| 94 | + Some(path) => path.clone(), |
| 95 | + None => { |
| 96 | + return Err(Error::MissingFelid { |
| 97 | + location: Location::caller(), |
| 98 | + felid: "sql_lite_3_db_file_path".to_string(), |
| 99 | + }) |
| 100 | + } |
| 101 | + }, |
| 102 | + driver => { |
| 103 | + return Err(Error::WrongDriver { |
| 104 | + location: Location::caller(), |
| 105 | + expected: Driver::Sqlite3, |
| 106 | + actual: driver, |
| 107 | + settings: value.clone(), |
| 108 | + }) |
| 109 | + } |
| 110 | + }, |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl TryFrom<&Settings> for mysql::Settings { |
| 116 | + type Error = Error; |
| 117 | + |
| 118 | + fn try_from(value: &Settings) -> Result<Self, Self::Error> { |
| 119 | + Ok(Self { |
| 120 | + connection_url: match value.driver { |
| 121 | + Driver::MySQL => match &value.my_sql_connection_url { |
| 122 | + Some(url) => url.clone(), |
| 123 | + None => { |
| 124 | + return Err(Error::MissingFelid { |
| 125 | + location: Location::caller(), |
| 126 | + felid: "my_sql_connection_url".to_string(), |
| 127 | + }) |
| 128 | + } |
| 129 | + }, |
| 130 | + driver => { |
| 131 | + return Err(Error::WrongDriver { |
| 132 | + location: Location::caller(), |
| 133 | + expected: Driver::MySQL, |
| 134 | + actual: driver, |
| 135 | + settings: value.clone(), |
| 136 | + }) |
| 137 | + } |
| 138 | + }, |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments