scf_core/manager/mod.rs
1use std::collections::HashMap;
2
3use lang_extension::any::*;
4use lang_extension::ops::function::*;
5
6use crate::property::*;
7use crate::source::*;
8
9pub mod default;
10
11pub trait ConfigurationManagerConfig: Value + Send + Sync {
12 /// for description use
13 fn get_name(&self) -> &str;
14
15 /// key for the source priority, value for the source
16 ///
17 /// the greater the key is, the higher the priority is
18 ///
19 /// non-null, non-empty
20 fn get_sources(&self) -> &Vec<Box<dyn ConfigurationSource>>;
21
22 /// thread pool for property value update and property change listeners
23 ///
24 /// by default, property value update and property change listeners
25 /// will be done in the source change raising thread
26 ///
27 /// if property count is too large, or the property change listeners are too slow,
28 /// it's better to use an async thread pool
29 fn get_task_executor(&self) -> &dyn Fn(&Box<dyn Fn()>);
30
31 as_boxed!(ConfigurationManagerConfig);
32}
33
34boxed_value_trait!(ConfigurationManagerConfig);
35
36pub trait ConfigurationManagerConfigBuilder {
37 /// required
38 fn set_name(&mut self, name: &str) -> &mut dyn ConfigurationManagerConfigBuilder;
39
40 /// required
41 fn add_source(
42 &mut self,
43 priority: i32,
44 source: Box<dyn ConfigurationSource>,
45 ) -> &mut dyn ConfigurationManagerConfigBuilder;
46
47 /// required
48 fn add_sources(
49 &mut self,
50 sources: HashMap<i32, Box<dyn ConfigurationSource>>,
51 ) -> &mut dyn ConfigurationManagerConfigBuilder;
52
53 /// optional
54 fn set_task_executor(
55 &mut self,
56 task_executor: ConsumerRef<Box<dyn Fn()>>,
57 ) -> &mut dyn ConfigurationManagerConfigBuilder;
58
59 fn build(&self) -> Box<dyn ConfigurationManagerConfig>;
60}
61
62pub trait ConfigurationManager: Value + Send + Sync {
63 fn get_config(&self) -> &dyn ConfigurationManagerConfig;
64
65 /// the properties created by get_property
66 fn get_properties(&self) -> Vec<Box<dyn RawProperty>>;
67
68 /// get property value by get_property_value
69 /// and return a property with the propertyConfig and value
70 ///
71 /// once a property is created, it is kept by the manager and will be auto-update after some configuration source changed
72 ///
73 /// same property config in, same property out, 1 key 1 property
74 fn get_property(&self, config: &dyn RawPropertyConfig) -> Box<dyn RawProperty>;
75
76 /// get property value in each configuration source by source priority
77 ///
78 /// if non-None value is got by a source ConfigurationSource::get_property_value
79 /// if PropertyConfig::get_value_filter() is None, return the non-None value,
80 /// if PropertyConfig::get_value_filter() is non-None, apply the filter to the non-None value,
81 /// if the new value returned by the filter is non-None, return the new value.
82 /// otherwise, go to the next lower priority source
83 ///
84 /// after handling all sources, no non-None value got, return None
85 fn get_property_value(&self, config: &dyn RawPropertyConfig) -> Option<Box<dyn Value>>;
86
87 /// listeners to the property change, notified once property changed
88 fn add_raw_change_listener(&self, listener: RawPropertyChangeListener);
89
90 as_boxed!(ConfigurationManager);
91}
92
93boxed_value_trait!(ConfigurationManager);