lib_defs/app_env/src/lib.rs

192 lines
4.3 KiB
Rust

//
// Copyright 2021, Jesús Pérez Lorenzo
//
use core::fmt;
// use futures::lock::Mutex;
// use std::sync::Arc;
// use lazy_static::lazy_static;
// use once_cell::sync::Lazy;
use std::sync::Arc;
// use tokio::sync::Mutex;
use parking_lot::RwLock;
use serde::Deserialize;
pub mod appdata;
pub mod appenv;
pub mod appinfo;
pub mod config;
pub mod module;
pub mod profile;
pub mod applogs;
use crate::appdata::AppData;
use AppRunMode::*;
pub type BxDynResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[derive(Clone, Debug)]
pub struct AppStore {
pub app_data: Arc<RwLock<AppData>>,
}
impl AppStore {
pub fn new(app: AppData) -> Self {
AppStore {
app_data: Arc::new(RwLock::new(app)),
}
}
}
/*
/// So we don't have to tackle how different database work, we'll just use
/// a simple in-memory DB, a vector synchronized by a mutex.
pub type Db = Arc<Mutex<Vec<AppData>>>;
#[must_use]
pub fn blank_db() -> Db {
Arc::new(Mutex::new(Vec::new())) // Vec::new()))
//Arc::new(Mutex::new(vec!(AppData::new(AppEnv::default())))) // Vec::new()))
}
#[must_use]
pub fn appdata_db(env: AppEnv) -> Db {
Arc::new(Mutex::new(vec!(AppData::new(env))))
}
pub static DB: Lazy<Db> = Lazy::new(|| blank_db());
pub async fn get_db_appdata() -> AppData {
let db_appdata = DB.lock().await;
db_appdata[0].to_owned()
}
*/
#[derive(Clone, Copy, Debug, PartialEq, Deserialize)]
/// `DataStore` options
pub enum DataStore {
File,
Mysql, // (sqlx::MySqlPool),
Postgres, // (sqlx::PgPool),
Sqlite, // (sqlx::SqlitePool),
Redis, // (redis::Client),
Tikv, // (tikv::RawClient),
Slab, // (Storage),
Unknown,
}
pub type OptionDataStore = Option<DataStore>;
impl Default for DataStore {
fn default() -> Self {
DataStore::Unknown
}
}
impl DataStore {
/// Get `DataStore`from String to enum
#[must_use]
pub fn get_from(str: String) -> DataStore {
match str.as_str() {
"file" | "File" => DataStore::File,
"mysql" | "Mysql" | "MySql" => DataStore::Mysql,
"postgres" | "Postgres" | "pg" => DataStore::Postgres,
"sqlite" | "Sqlite" => DataStore::Sqlite,
"redis" | "Redis" => DataStore::Redis,
"tikv" | "Tikv" => DataStore::Tikv,
"slab" | "Slab" => DataStore::Slab,
_ => DataStore::Unknown,
}
}
}
#[allow(clippy::pattern_type_mismatch)]
impl fmt::Display for DataStore {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DataStore::File => write!(f, "file"),
DataStore::Mysql => write!(f, "mysql"),
DataStore::Postgres => write!(f, "postgres"),
DataStore::Sqlite => write!(f, "sqlite"),
DataStore::Redis => write!(f, "redis"),
DataStore::Tikv => write!(f, "tikv"),
DataStore::Slab => write!(f, "slab"),
DataStore::Unknown => write!(f, "Unknown"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
/// `DataStore` options
pub enum AppRunMode {
Basic, // Free basic mode
Pro, // Pro licensed mode
Premium, // Premium licensed mode
Unknown,
}
pub type OptionAppRunMode = Option<AppRunMode>;
impl Default for AppRunMode {
fn default() -> Self {
Unknown
}
}
#[allow(clippy::pattern_type_mismatch)]
impl fmt::Display for AppRunMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Basic => write!(f, "basic"),
Pro => write!(f, "pro"),
Premium => write!(f, "premium"),
Unknown => write!(f, "Unknown"),
}
}
}
impl AppRunMode {
/// Get `DataStore`from String to enum
#[must_use]
pub fn get_from(str: &str) -> AppRunMode {
match str {
"basic" => Basic,
"pro" => Pro,
"premium" => Premium,
_ => Unknown,
}
}
}
#[derive(Clone, Debug, Default, Deserialize)]
pub struct DataCollUsers {
pub fields: String,
}
#[derive(Clone, Debug)]
pub struct Zterton {
pub protocol: String,
pub host: String,
pub port: u16,
}
impl Default for Zterton {
fn default() -> Self {
Self {
protocol: String::from("http"),
host: String::from("127.0.0.1"),
port: 8000,
}
}
}
impl Zterton {
#[must_use]
/// New Zterton definition
pub fn new(protocol: String, host: String, port: u16) -> Self {
Zterton {
protocol,
host,
port,
}
}
}