lib_defs/app_env/src/appenv.rs

84 lines
1.8 KiB
Rust
Raw Normal View History

2021-09-01 16:15:20 +00:00
//
// Copyright 2021, Jesús Pérez Lorenzo
//
use std::collections::HashMap;
use serde::Deserialize;
use crate::{
config::Config,
module::Module,
appinfo::AppInfo,
};
#[derive(Clone, Debug, Default, Deserialize)]
pub struct DataCollUsers {
pub fields: String,
}
#[derive(Clone, Debug)]
pub struct AppEnv {
pub info: AppInfo,
pub config: Config,
pub debug_level: String,
pub appkey: String,
pub checked: bool,
pub collections: HashMap<String, toml::Value>,
pub modules: HashMap<String, Module>,
// pub modules: HashMap<String, toml::Value>,
}
impl Default for AppEnv {
fn default() -> Self {
Self {
info: AppInfo::default(),
config: Config::default(),
debug_level: String::from(""),
appkey: String::from(""),
checked: true,
collections: HashMap::new(),
modules: HashMap::new(),
}
}
}
impl AppEnv {
#[allow(clippy::too_many_arguments)]
pub async fn new(
config: Config,
debug_level: &str,
info: AppInfo,
appkey: &str,
checked: bool,
collections: HashMap<String, toml::Value>,
modules: HashMap<String, Module>,
// jwt_sign: Option<JwtSignature>,
) -> Self {
let akey = appkey.to_string();
Self {
info,
config,
debug_level: debug_level.to_string(),
appkey: akey,
checked,
collections,
modules,
// jwt_sign,
}
}
pub async fn set_appkey(&mut self, app_key: &str, key: &str) {
if key.is_empty() {
self.appkey = app_key.to_string();
}
}
#[must_use]
pub fn has_appkey(&self) -> bool {
self.appkey.as_str() == ""
}
pub fn get_module(&self, key_module: &str) -> Module {
if let Some(module) = self.modules.get(key_module) {
module.to_owned()
} else {
Module::default()
}
}
}