Skip to content

Commit

Permalink
Add a global_module example for a singleton configuration module dese…
Browse files Browse the repository at this point in the history
…rialized to a predefined structures
  • Loading branch information
robertek committed Jul 11, 2024
1 parent e3c1d0b commit 115825c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/global_module/config/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
verbose=1

[cred]
user="robert"
key="123456789"
13 changes: 13 additions & 0 deletions examples/global_module/cred.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::settings::Settings;

pub fn list_cred() {
match Settings::user() {
Ok(u) => println!("My name is: {u}"),
Err(e) => println!("{e}")
}

match Settings::key() {
Ok(k) => println!("My key is: {k}"),
Err(e) => println!("{e}")
}
}
22 changes: 22 additions & 0 deletions examples/global_module/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
mod settings;
mod cred;

use crate::settings::Settings;
use crate::cred::list_cred;

fn main() {
// init the config module
Settings::init(Some("examples/global_module/config/default.toml"));

// now your config may be used anywhere in the code where you are able to
// use "crate::settings" or "super::settings".
let verbosity = Settings::verbosity();

if verbosity > 0 {
println!("Hello world");
}

list_cred();

return;
}
72 changes: 72 additions & 0 deletions examples/global_module/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use config::{Config, File};
use lazy_static::lazy_static;
use serde_derive::Deserialize;
use std::sync::RwLock;

lazy_static! {
static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::new());
}


#[derive(Default, Clone, Deserialize)]
struct Cred {
user: String,
key: String,
}

#[derive(Default, Clone, Deserialize)]
pub struct Settings {
verbose: Option<u8>,
cred: Option<Cred>,
}


fn build_config(file: &str) -> Settings {
let s = Config::builder()
// Configuration file
.add_source(File::with_name(file).required(false))
.build()
.expect("Config build failed");

// Deserialize (and thus freeze) the entire configuration
s.try_deserialize().unwrap()
}

impl Settings {
fn new() -> Self {
let settings: Settings = Default::default();
settings
}

pub fn init(cfgfile: Option<&str>) {
let file = match cfgfile {
Some(x) => x,
None => "config.toml"
};

let mut new_settings = SETTINGS.write().unwrap();
*new_settings = build_config(file);
}

pub fn user() -> Result<String, String> {
match &SETTINGS.read().unwrap().cred {
Some(c) => Ok(c.user.clone()),
None => Err(format!("Credential config is missing"))
}
}

pub fn key() -> Result<String, String> {
match &SETTINGS.read().unwrap().cred {
Some(c) => Ok(c.key.clone()),
None => Err(format!("Credential config is missing"))
}
}

pub fn verbosity() -> u8 {
match SETTINGS.read().unwrap().verbose {
Some(v) => v,
None => 0
}
}
}

0 comments on commit 115825c

Please sign in to comment.