kit_rs/config/
repository.rs1use std::any::{Any, TypeId};
2use std::collections::HashMap;
3use std::sync::{OnceLock, RwLock};
4
5static CONFIG_REPOSITORY: OnceLock<RwLock<ConfigRepository>> = OnceLock::new();
7
8pub struct ConfigRepository {
10 configs: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
11}
12
13impl ConfigRepository {
14 pub fn new() -> Self {
16 Self {
17 configs: HashMap::new(),
18 }
19 }
20
21 pub fn register<T: Any + Send + Sync + 'static>(&mut self, config: T) {
23 self.configs.insert(TypeId::of::<T>(), Box::new(config));
24 }
25
26 pub fn get<T: Any + Send + Sync + Clone + 'static>(&self) -> Option<T> {
28 self.configs
29 .get(&TypeId::of::<T>())
30 .and_then(|boxed| boxed.downcast_ref::<T>())
31 .cloned()
32 }
33
34 pub fn has<T: Any + 'static>(&self) -> bool {
36 self.configs.contains_key(&TypeId::of::<T>())
37 }
38}
39
40impl Default for ConfigRepository {
41 fn default() -> Self {
42 Self::new()
43 }
44}
45
46pub fn init_repository() -> &'static RwLock<ConfigRepository> {
48 CONFIG_REPOSITORY.get_or_init(|| RwLock::new(ConfigRepository::new()))
49}
50
51pub fn register<T: Any + Send + Sync + 'static>(config: T) {
53 let repo = init_repository();
54 if let Ok(mut repo) = repo.write() {
55 repo.register(config);
56 }
57}
58
59pub fn get<T: Any + Send + Sync + Clone + 'static>() -> Option<T> {
61 let repo = CONFIG_REPOSITORY.get()?;
62 repo.read().ok()?.get::<T>()
63}
64
65pub fn has<T: Any + 'static>() -> bool {
67 CONFIG_REPOSITORY
68 .get()
69 .and_then(|repo| repo.read().ok())
70 .map(|repo| repo.has::<T>())
71 .unwrap_or(false)
72}