Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a global_module example #571

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}")
}
}
20 changes: 20 additions & 0 deletions examples/global_module/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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, verbosity setting is greater than 0");
}

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


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

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


// This function defines the static settings storage.
fn settings() -> &'static RwLock<Settings> {
static SETTINGS: OnceLock<RwLock<Settings>> = OnceLock::new();
SETTINGS.get_or_init(|| RwLock::new(Settings::default()))
}

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 {
// This associated function replaces previous settings values with a newly
// loaded ones.
//
// It is mainly intended for loading the values from config file to replace
// the plain default used for static allocation. Thus running it once at
// the beginning of the program execution when the config files are known.
//
// But a later call to this function may be used to update the settings,
// for example, when the config file changes during the execution and you want
// to sync with it (signal/notify/whatever based reload).
pub fn init(cfgfile: Option<&str>) {
let file = cfgfile.unwrap_or("config.toml");

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

// Following associated functions are just getters, when you want to keep
// the Settings structure members private.
pub fn user() -> Result<String, String> {
match &settings().read().unwrap().cred {
Some(c) => Ok(c.user.clone()),
None => Err("Credential config is missing".to_string()),
}
}

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

pub fn verbosity() -> u8 {
settings().read().unwrap().verbose.unwrap_or(0)
}

// It is not a problem to make all the Settings structure members public and
// then only create here one function, that will return a read reference
// to the Settings. This may be useful if you want to omit the getters and
// the settings contain just plain values that don't need any error
// handling.
//
// Example:
// pub fn new() -> Self {
// settings().read().unwrap()
// }
}