1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
//! To me, OnceCell is a good tool for config.
//! It said:
//! A cell which can be written to only once.
//! Ok!
//! Call config from config add institute.
//!
use crate::fns::fn_io;
use once_cell::sync::OnceCell;
use std::error::Error;
use toml::Value;
type Result = ::std::result::Result<(), Box<dyn Error>>;
/// Often, it can be a database url or file.
pub static DB_LOCATION: OnceCell<String> = OnceCell::new();
/// and it can be a source from what?
pub static DATA_FROM: OnceCell<String> = OnceCell::new();
/// Ordering temporary place.
pub static DATA_TIDY: OnceCell<String> = OnceCell::new();
///Open file
///get fields which is used.
///written to only once
///used it for main or lib.
///
/// It is just a example for me.
///Can you be look source code and changed it for yourself.
///
/// Example:
///
///```rust
/// use jay_lib::config::{set_conf,DB_LOCATION,DATA_FROM,DATA_TIDY};
/// let db_location;
/// let dic_from;
/// let tidy_data;
/// match set_conf() {
/// Ok(_) => {
/// db_location = DB_LOCATION.get();
/// dic_from = DATA_FROM.get();
/// tidy_data = DATA_TIDY.get();
/// println!("{:?},{:?},{:?}", db_location, dic_from, tidy_data);
/// }
/// Err(e) => { eprintln!("{e:?}");return; }
/// };
/// ```
/// ***It will be changed.***
/// If no exist config file ... ***To Do***
///
pub fn set_conf() -> Result {
let mut s = String::from("");
if let Ok(st) = fn_io::f_string("./.temporary/config.toml") {
s = st;
}
let toml_info: Value = toml::from_str(&s)?;
if let Some(from) = toml_info["data"]["from"].as_str() {
DATA_FROM.get_or_init(|| from.to_string());
}
if let Some(tidy) = toml_info["data"]["tidy"].as_str() {
DATA_TIDY.get_or_init(|| tidy.to_string());
//data_tidy=tidy;
}
if let Some(location) = toml_info["db"]["location"].as_str() {
DB_LOCATION.get_or_init(|| location.to_string());
}
Ok(())
}