Pre-release v0.2.0
This commit is contained in:
52
src/settings.rs
Normal file
52
src/settings.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use config::{Config, File, Value};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::thread::available_parallelism;
|
||||
|
||||
use crate::blockchains::Blockchain;
|
||||
use crate::key_generators::KeyAlgorithm;
|
||||
use crate::utils::DynError;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Settings {
|
||||
pub key_generators: HashMap<KeyAlgorithm, KeyGeneratorSettings>,
|
||||
pub blockchains: HashMap<Blockchain, BlockchainSettings>,
|
||||
pub notifications: NotificationSettings,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct KeyGeneratorSettings {
|
||||
#[serde(default = "default_workers")]
|
||||
pub workers: usize,
|
||||
#[serde(flatten)]
|
||||
pub data: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct BlockchainSettings {
|
||||
#[serde(default = "default_workers")]
|
||||
pub workers: usize,
|
||||
#[serde(flatten)]
|
||||
pub data: HashMap<String, Value>,
|
||||
|
||||
}
|
||||
|
||||
fn default_workers() -> usize {
|
||||
available_parallelism().unwrap().get()
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct NotificationSettings {
|
||||
pub telegram_bot_token: String,
|
||||
pub telegram_user_id: String,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn load(path: &str) -> Result<Self, DynError> {
|
||||
Config::builder()
|
||||
.add_source(File::with_name(path).required(false))
|
||||
.build()?
|
||||
.try_deserialize()
|
||||
.map_err(|e| e.into())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user