caretta-sync/lazy-supplements-core/src/global/mod.rs

104 lines
2.8 KiB
Rust
Raw Normal View History

2025-06-20 08:42:02 +09:00
use std::{any::type_name, collections::HashMap, net::{IpAddr, Ipv4Addr}, path::{Path, PathBuf}, sync::LazyLock};
2025-05-30 09:26:47 +09:00
2025-06-22 11:29:44 +09:00
use crate::{config::{P2pConfig, PartialP2pConfig, StorageConfig}, error::Error };
#[cfg(any(test, feature="test"))]
use crate::tests::{GlobalTestDefault, TestDefault};
2025-06-05 09:23:24 +09:00
use futures::StreamExt;
use libp2p::{swarm::SwarmEvent, Multiaddr, PeerId};
2025-06-10 07:47:37 +09:00
use sea_orm::{prelude::*, Database};
use sea_orm_migration::MigratorTrait;
2025-06-22 11:29:44 +09:00
use tokio::sync::{OnceCell, RwLock, RwLockReadGuard, RwLockWriteGuard};
2025-05-25 17:19:37 +09:00
2025-06-19 07:24:44 +09:00
mod peers;
2025-06-22 11:29:44 +09:00
pub use peers::PEERS;
2025-06-20 07:28:51 +09:00
mod config;
2025-06-20 08:42:02 +09:00
pub use config::STORAGE_CONFIG;
2025-06-19 07:24:44 +09:00
mod database_connection;
2025-06-20 07:28:51 +09:00
pub use database_connection::*;
2025-06-17 07:20:24 +09:00
use uuid::{ContextV7, Timestamp, Uuid};
pub fn generate_uuid() -> Uuid {
Uuid::new_v7(Timestamp::now(ContextV7::new()))
}
2025-05-25 17:19:37 +09:00
2025-05-30 09:26:47 +09:00
pub static PRODUCT_NAME: LazyLock<String> = LazyLock::new(|| {
env!("CARGO_PKG_NAME").to_string()
});
2025-06-01 15:18:17 +09:00
pub static DEFAULT_LISTEN_IPS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))];
2025-05-30 09:26:47 +09:00
pub static DEFAULT_CONFIG_FILE_NAME: LazyLock<PathBuf> = LazyLock::new(|| {
PathBuf::from(String::new() + env!("CARGO_PKG_NAME") + ".toml")
});
pub static DEFAULT_DATABASE_FILE_NAME: LazyLock<PathBuf> = LazyLock::new(|| {
PathBuf::from(String::new() + env!("CARGO_PKG_NAME") + ".sqlite")
});
2025-06-20 08:42:02 +09:00
fn uninitialized_message<T>(var: T) -> String {
format!("{} is uninitialized!", &stringify!(var))
}
2025-06-22 11:29:44 +09:00
pub struct GlobalConstant<T> {
pub name: &'static str,
2025-06-20 08:42:02 +09:00
inner: OnceCell<T>
}
2025-06-22 11:29:44 +09:00
impl<T> GlobalConstant<T> {
pub const fn const_new(name: &'static str ) -> Self {
Self{
name: name,
inner: OnceCell::const_new()
}
2025-06-20 08:42:02 +09:00
}
pub async fn get_or_init(&'static self, source: T) -> &'static T {
self.inner.get_or_init(|| async {
source
}).await
}
pub fn get(&'static self) -> Option<&'static T> {
self.inner.get()
}
pub fn get_and_unwrap(&'static self) -> &'static T {
self.get().expect(&format!("{} is uninitialized!", &stringify!(self)))
}
}
2025-06-22 11:29:44 +09:00
#[cfg(any(test, feature="test"))]
impl<T> GlobalTestDefault<T> for GlobalConstant<T>
where
T: TestDefault + 'static
{
async fn get_or_init_test_default(&'static self) -> &'static T {
self.get_or_init(T::test_default()).await
}
}
2025-06-20 08:42:02 +09:00
struct GlobalRwLock<T> {
2025-06-22 11:29:44 +09:00
pub name: &'static str,
2025-06-20 08:42:02 +09:00
inner: OnceCell<RwLock<T>>
}
impl<T> GlobalRwLock<T> {
2025-06-22 11:29:44 +09:00
pub const fn const_new(name: &'static str) -> Self {
Self{
name: name,
inner: OnceCell::const_new()
}
2025-06-20 08:42:02 +09:00
}
2025-06-22 11:29:44 +09:00
pub fn get(&'static self) -> &'static RwLock<T> {
self.inner.get().expect(&format!("{} is uninitialized", self.name))
2025-06-20 08:42:02 +09:00
}
2025-06-22 11:29:44 +09:00
pub async fn write(&'static self) -> RwLockWriteGuard<'_ ,T> {
self.get().write().await
}
pub async fn read(&'static self) -> RwLockReadGuard<'_, T> {
self.get().read().await
2025-06-20 08:42:02 +09:00
}
2025-06-20 07:28:51 +09:00
}
#[cfg(test)]
mod tests {
2025-06-10 07:47:37 +09:00
}