#![allow(deprecated)]
use config::{Config, File};
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::collections::HashMap;
use std::path::Path;
use std::sync::mpsc::channel;
use std::sync::RwLock;
use std::time::Duration;
lazy_static::lazy_static! {
static ref SETTINGS: RwLock<Config> = RwLock::new({
let mut settings = Config::default();
settings.merge(File::with_name("examples/watch/Settings.toml")).unwrap();
settings
});
}
fn show() {
println!(
" * Settings :: \n\x1b[31m{:?}\x1b[0m",
SETTINGS
.read()
.unwrap()
.clone()
.try_deserialize::<HashMap<String, String>>()
.unwrap()
);
}
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("examples/watch/Settings.toml"),
RecursiveMode::NonRecursive,
)
.unwrap();
loop {
match rx.recv() {
Ok(Ok(Event {
kind: notify::event::EventKind::Modify(_),
..
})) => {
println!(" * Settings.toml written; refreshing configuration ...");
SETTINGS.write().unwrap().refresh().unwrap();
show();
}
Err(e) => println!("watch error: {:?}", e),
_ => {
}
}
}
}
fn main() {
show();
watch();
}