52 lines
1.3 KiB
Rust
52 lines
1.3 KiB
Rust
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())
|
|
}
|
|
} |