generated from t/malachite
add: full initial
This commit is contained in:
parent
f5c663495d
commit
d06bc5e726
29 changed files with 592 additions and 1928 deletions
59
crates/buckets-core/src/config.rs
Normal file
59
crates/buckets-core/src/config.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use oiseau::config::{Configuration, DatabaseConfig};
|
||||
use pathbufd::PathBufD;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
/// The directory files are stored in (relative to cwd).
|
||||
#[serde(default = "default_directory")]
|
||||
pub directory: String,
|
||||
/// Database configuration.
|
||||
#[serde(default = "default_database")]
|
||||
pub database: DatabaseConfig,
|
||||
}
|
||||
|
||||
fn default_directory() -> String {
|
||||
"buckets".to_string()
|
||||
}
|
||||
|
||||
fn default_database() -> DatabaseConfig {
|
||||
DatabaseConfig::default()
|
||||
}
|
||||
|
||||
impl Configuration for Config {
|
||||
fn db_config(&self) -> DatabaseConfig {
|
||||
self.database.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
directory: default_directory(),
|
||||
database: default_database(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Read the configuration file.
|
||||
pub fn read() -> Self {
|
||||
toml::from_str(
|
||||
&match std::fs::read_to_string(PathBufD::current().join("app.toml")) {
|
||||
Ok(x) => x,
|
||||
Err(_) => {
|
||||
let x = Config::default();
|
||||
|
||||
std::fs::write(
|
||||
PathBufD::current().join("app.toml"),
|
||||
&toml::to_string_pretty(&x).expect("failed to serialize config"),
|
||||
)
|
||||
.expect("failed to write config");
|
||||
|
||||
return x;
|
||||
}
|
||||
},
|
||||
)
|
||||
.expect("failed to deserialize config")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue