use std::collections::HashMap;
use std::path::Path;
use std::sync::OnceLock;
use std::sync::RwLock;
use std::sync::mpsc::channel;
use std::time::Duration;
use config::{Config, File};
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
fn main() {
watch();
}
fn watch() -> ! {
let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(
tx,
notify::Config::default().with_poll_interval(Duration::from_secs(2)),
)
.unwrap();
watcher
.watch(Path::new(SETTINGS_PATH), RecursiveMode::NonRecursive)
.unwrap();
show();
loop {
match rx.recv() {
Ok(Ok(Event {
kind: notify::event::EventKind::Modify(_),
..
})) => {
println!(" * settings.toml written; refreshing configuration ...");
refresh();
show();
}
Err(e) => println!("watch error: {e:?}"),
_ => {
}
}
}
}
fn show() {
println!(
" * Settings :: \n\x1b[31m{:?}\x1b[0m",
settings()
.read()
.unwrap()
.clone()
.try_deserialize::<HashMap<String, String>>()
.unwrap()
);
}
pub fn settings() -> &'static RwLock<Config> {
static CONFIG: OnceLock<RwLock<Config>> = OnceLock::new();
CONFIG.get_or_init(|| {
let settings = load();
RwLock::new(settings)
})
}
pub fn refresh() {
*settings().write().unwrap() = load();
}
fn load() -> Config {
Config::builder()
.add_source(File::with_name(SETTINGS_PATH))
.build()
.unwrap()
}
static SETTINGS_PATH: &str = "examples/settings.toml";