oxedyne/fe2o3/fe2o3_shield/src/app/cfg.rs
2.1 KiB, 21 runs
created by r1870400018:4064, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::srv::{ |
| 2 | cfg::ServerConfig, |
| 3 | }; |
| 4 | |
| 5 | use oxedyne_fe2o3_core::{ |
| 6 | prelude::*, |
| 7 | }; |
| 8 | use oxedyne_fe2o3_jdat::{ |
| 9 | prelude::*, |
| 10 | cfg::Config, |
| 11 | }; |
| 12 | |
| 13 | use std::{ |
| 14 | collections::BTreeMap, |
| 15 | path::Path, |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | #[derive(Clone, Debug, Eq, PartialEq, FromDatMap, ToDatMap)] |
| 20 | pub struct AppConfig { |
| 21 | pub app_root: String, |
| 22 | pub app_name: String, |
| 23 | pub app_human_name: String, |
| 24 | pub app_description: String, |
| 25 | pub app_log_level: String, |
| 26 | pub kdf_name: String, |
| 27 | pub enc_name: String, |
| 28 | pub server_cfg: DaticleMap, |
| 29 | } |
| 30 | |
| 31 | impl Config for AppConfig { |
| 32 | fn check_and_fix(&mut self) -> Outcome<()> { |
| 33 | let app_root_path = Path::new(&self.app_root); |
| 34 | res!(std::fs::create_dir_all(app_root_path)); |
| 35 | Ok(()) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl Default for AppConfig { |
| 40 | fn default() -> Self { |
| 41 | Self { |
| 42 | app_root: String::new(), |
| 43 | app_name: fmt!("shield"), |
| 44 | app_human_name: fmt!("Shield Server"), |
| 45 | app_description: fmt!("A Hematite Shield Server app."), |
| 46 | app_log_level: fmt!("debug"), |
| 47 | kdf_name: fmt!("Argon2id_v0x13"), |
| 48 | enc_name: fmt!("AES-256-GCM"), |
| 49 | server_cfg: DaticleMap::new(), |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl AppConfig { |
| 55 | |
| 56 | pub fn new() -> Outcome<Self> { |
| 57 | let mut cfg = Self::default(); |
| 58 | cfg.app_root = fmt!("{}", res!(std::env::current_dir()).display()); |
| 59 | cfg.server_cfg = try_extract_dat!( |
| 60 | ServerConfig::to_datmap(ServerConfig::default()), |
| 61 | Map, |
| 62 | ); |
| 63 | Ok(cfg) |
| 64 | } |
| 65 | |
| 66 | pub fn server_log_level(&self) -> Outcome<(LogLevel, String)> { |
| 67 | let level_str = if let Some(dat) = self.server_cfg.get(&dat!("log_level")) { |
| 68 | try_extract_dat!(dat, Str) |
| 69 | } else { |
| 70 | return Err(err!( |
| 71 | "Log level key not found in server configuration: {:?}.", |
| 72 | self.server_cfg; |
| 73 | Configuration, Missing, Key)); |
| 74 | }; |
| 75 | let level = res!(LogLevel::from_str(&level_str)); |
| 76 | Ok((level, level_str.clone())) |
| 77 | } |
| 78 | } |