-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a global_module example for a singleton configuration module dese…
…rialized to a predefined structures
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
verbose=1 | ||
|
||
[cred] | ||
user="robert" | ||
key="123456789" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
|