| 
									
										
										
										
											2025-08-20 06:50:35 +09:00
										 |  |  | #[cfg(any(test,feature="test"))]
 | 
					
						
							| 
									
										
										
										
											2025-08-18 08:22:20 +09:00
										 |  |  | use tempfile::TempDir;
 | 
					
						
							|  |  |  | use tokio::sync::OnceCell;
 | 
					
						
							| 
									
										
										
										
											2025-06-20 07:28:51 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-20 06:50:35 +09:00
										 |  |  | use crate::{config::{Config, ConfigError, PartialP2pConfig, PartialRpcConfig, PartialStorageConfig, StorageConfig}, error::Error};
 | 
					
						
							| 
									
										
										
										
											2025-06-20 07:28:51 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-18 08:22:20 +09:00
										 |  |  | pub static CONFIG: GlobalConfig = GlobalConfig::const_new();
 | 
					
						
							|  |  |  | pub struct GlobalConfig {
 | 
					
						
							|  |  |  |     inner: OnceCell<Config>
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2025-06-22 11:29:44 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-18 08:22:20 +09:00
										 |  |  | impl GlobalConfig { 
 | 
					
						
							|  |  |  |     pub const fn const_new() -> Self {
 | 
					
						
							|  |  |  |         Self{
 | 
					
						
							|  |  |  |             inner: OnceCell::const_new()
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2025-08-20 06:50:35 +09:00
										 |  |  |     pub async fn get_or_init<T>(&'static self, config: T) -> &'static Config where 
 | 
					
						
							|  |  |  |     T: AsRef<Config>{
 | 
					
						
							| 
									
										
										
										
											2025-08-18 08:22:20 +09:00
										 |  |  |         self.inner.get_or_init(|| async {
 | 
					
						
							| 
									
										
										
										
											2025-08-20 06:50:35 +09:00
										 |  |  |             config.as_ref().clone()
 | 
					
						
							| 
									
										
										
										
											2025-08-18 08:22:20 +09:00
										 |  |  |         }).await
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2025-08-20 06:50:35 +09:00
										 |  |  |     pub async fn get_or_try_init<T, E>(&'static self, config: T) -> Result<&'static Config, <T as TryInto<Config>>::Error> where 
 | 
					
						
							|  |  |  |         T: TryInto<Config>,
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         self.inner.get_or_try_init(|| async {
 | 
					
						
							|  |  |  |             config.try_into()
 | 
					
						
							|  |  |  |         }).await
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2025-08-18 08:22:20 +09:00
										 |  |  |     pub fn get(&'static self) -> Option<&'static Config> {
 | 
					
						
							|  |  |  |         self.inner.get()
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2025-06-22 11:29:44 +09:00
										 |  |  | }
 |