derive_snapshot/
derive_snapshot.rs

1use std::env::set_var;
2use std::thread::sleep;
3use std::time::Duration;
4use env_watcher::{Error, Subscribe, sub_env_snapshot, init_env_watch};
5
6/// In this case, we get a reference to the values, when the environment variable changes, there is a change in the reference.
7/// Analogous to [`examples/snapshot.rs`]
8fn main() -> Result<(), Error> {
9    set_var("key.key", "vvv");
10
11    init_env_watch!(Duration::from_millis(25 * 10))?;
12
13    let sub = Subscribe::Envs(vec!["key.key".to_string()]);
14
15    let env = sub_env_snapshot!(sub)?;
16
17    assert_eq!(Some(&String::from("vvv")), env.data().get("key.key"));
18
19    set_var("key.key", "hello");
20
21    sleep(Duration::from_millis(500));
22
23    assert_eq!(Some(&String::from("hello")), env.data().get("key.key"));
24
25    Ok(())
26}