Skip to content

Commit 115825c

Browse files
committed
Add a global_module example for a singleton configuration module deserialized to a predefined structures
1 parent e3c1d0b commit 115825c

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
verbose=1
2+
3+
[cred]
4+
user="robert"
5+
key="123456789"

examples/global_module/cred.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use super::settings::Settings;
2+
3+
pub fn list_cred() {
4+
match Settings::user() {
5+
Ok(u) => println!("My name is: {u}"),
6+
Err(e) => println!("{e}")
7+
}
8+
9+
match Settings::key() {
10+
Ok(k) => println!("My key is: {k}"),
11+
Err(e) => println!("{e}")
12+
}
13+
}

examples/global_module/main.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
mod settings;
2+
mod cred;
3+
4+
use crate::settings::Settings;
5+
use crate::cred::list_cred;
6+
7+
fn main() {
8+
// init the config module
9+
Settings::init(Some("examples/global_module/config/default.toml"));
10+
11+
// now your config may be used anywhere in the code where you are able to
12+
// use "crate::settings" or "super::settings".
13+
let verbosity = Settings::verbosity();
14+
15+
if verbosity > 0 {
16+
println!("Hello world");
17+
}
18+
19+
list_cred();
20+
21+
return;
22+
}

examples/global_module/settings.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use config::{Config, File};
2+
use lazy_static::lazy_static;
3+
use serde_derive::Deserialize;
4+
use std::sync::RwLock;
5+
6+
lazy_static! {
7+
static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::new());
8+
}
9+
10+
11+
#[derive(Default, Clone, Deserialize)]
12+
struct Cred {
13+
user: String,
14+
key: String,
15+
}
16+
17+
#[derive(Default, Clone, Deserialize)]
18+
pub struct Settings {
19+
verbose: Option<u8>,
20+
cred: Option<Cred>,
21+
}
22+
23+
24+
fn build_config(file: &str) -> Settings {
25+
let s = Config::builder()
26+
// Configuration file
27+
.add_source(File::with_name(file).required(false))
28+
.build()
29+
.expect("Config build failed");
30+
31+
// Deserialize (and thus freeze) the entire configuration
32+
s.try_deserialize().unwrap()
33+
}
34+
35+
impl Settings {
36+
fn new() -> Self {
37+
let settings: Settings = Default::default();
38+
settings
39+
}
40+
41+
pub fn init(cfgfile: Option<&str>) {
42+
let file = match cfgfile {
43+
Some(x) => x,
44+
None => "config.toml"
45+
};
46+
47+
let mut new_settings = SETTINGS.write().unwrap();
48+
*new_settings = build_config(file);
49+
}
50+
51+
pub fn user() -> Result<String, String> {
52+
match &SETTINGS.read().unwrap().cred {
53+
Some(c) => Ok(c.user.clone()),
54+
None => Err(format!("Credential config is missing"))
55+
}
56+
}
57+
58+
pub fn key() -> Result<String, String> {
59+
match &SETTINGS.read().unwrap().cred {
60+
Some(c) => Ok(c.key.clone()),
61+
None => Err(format!("Credential config is missing"))
62+
}
63+
}
64+
65+
pub fn verbosity() -> u8 {
66+
match SETTINGS.read().unwrap().verbose {
67+
Some(v) => v,
68+
None => 0
69+
}
70+
}
71+
}
72+

0 commit comments

Comments
 (0)