EnvironmentData

Struct EnvironmentData 

Source
pub struct EnvironmentData { /* private fields */ }
Expand description

Baseline implementation for data. A separate thread listens for data changes through the channel, in case of data changes, we receive an event and change the data snapshot.

Implementations§

Source§

impl EnvironmentData

Source

pub fn data(&self) -> HashMap<String, String>

Getter for snapshot data

Examples found in repository?
examples/derive_snapshot.rs (line 17)
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}
More examples
Hide additional examples
examples/snapshot.rs (line 51)
30pub fn main() -> Result<(), Error> {
31    let sub_envs_key = vec!["test.bey".to_string(), "test.wow".to_string()];
32    fill_envs(sub_envs_key.clone(), TEST_VALUE);
33
34    let sub_envs = Subscribe::Envs(sub_envs_key.clone());
35
36    let sub_pattern_envs_key = vec![
37        "my.server.port".to_string(),
38        "my.server.host".to_string(),
39        "my.server.locale".to_string(),
40        "my.server.tls".to_string(),
41    ];
42    fill_envs(sub_pattern_envs_key.clone(), TEST_VALUE);
43
44    let sub_pattern_envs = Subscribe::PatternEnvs(vec!["^my.server.*".to_string()]);
45
46    let env_core = EnvironmentWatcher::new(Duration::from_secs(3));
47    sleep(Duration::from_secs(1));
48
49    let only_envs = env_core.subscribe_snapshot(sub_envs)?;
50
51    let envs_data = only_envs.data();
52    print_after();
53    println!("{:?}", &envs_data);
54    println!();
55
56    envs_data.iter().for_each(|kv| {
57        match &**kv.0 {
58            "test.bey" | "test.wow" => {
59                // All good.
60                assert_eq!(TEST_VALUE, &**kv.1);
61            }
62            _ => {
63                println!("Find unknown key with value. {}, {}", &**kv.0, &**kv.1);
64                panic!("unknown k/v in pattern sub.");
65            }
66        }
67    });
68
69    let pattern_envs = env_core.subscribe_snapshot(sub_pattern_envs)?;
70
71    let pattern_data = pattern_envs.data();
72    println!("{:?}", &pattern_data);
73
74    pattern_data.iter().for_each(|kv| {
75        match &**kv.0 {
76            "my.server.port" | "my.server.host" | "my.server.locale" | "my.server.tls" => {
77                // All good.
78                assert_eq!(TEST_VALUE, &**kv.1);
79            }
80            _ => {
81                println!("Find unknown key with value. {}, {}", &**kv.0, &**kv.1);
82                panic!("unknown k/v in pattern sub.");
83            }
84        }
85    });
86
87    fill_envs(sub_envs_key.clone(), TEST_BY_CHANGE_VALUE);
88    fill_envs(sub_pattern_envs_key.clone(), TEST_BY_CHANGE_VALUE);
89
90    sleep(Duration::from_secs(3));
91
92    print_before();
93    let envs_data = only_envs.data();
94    println!("{:?}", &envs_data);
95    println!();
96
97    envs_data.iter().for_each(|kv| {
98        match &**kv.0 {
99            "test.bey" | "test.wow" => {
100                // All good.
101                assert_eq!(TEST_BY_CHANGE_VALUE, &**kv.1);
102            }
103            _ => {
104                println!("Find unknown key with value. {}, {}", &**kv.0, &**kv.1);
105                panic!("unknown k/v in pattern sub.");
106            }
107        }
108    });
109
110    let pattern_data = pattern_envs.data();
111    println!("{:?}", &pattern_data);
112
113    pattern_data.iter().for_each(|kv| {
114        match &**kv.0 {
115            "my.server.port" | "my.server.host" | "my.server.locale" | "my.server.tls" => {
116                // All good.
117                assert_eq!(TEST_BY_CHANGE_VALUE, &**kv.1);
118            }
119            _ => {
120                println!("Find unknown key with value. {}, {}", &**kv.0, &**kv.1);
121                panic!("unknown k/v in pattern sub.");
122            }
123        }
124    });
125
126    Ok(())
127}
Source

pub fn ref_data(&self) -> Arc<Mutex<HashMap<String, String>>>

Reference for current snapshot

Source

pub fn receive(&self)

In a separate thread, we listen to the change of variables

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.