use std::{fmt::Display, sync::Arc};
use flume::Sender;
use futures::Future;
use once_cell::sync::OnceCell;
use crate::Log;
static GLOBAL_CONFIG: OnceCell<Arc<Configuration>> = OnceCell::new();
tokio::task_local! {
static TASK_CONFIG: Option<Arc<Configuration>>;
}
#[derive(Debug)]
pub struct Configuration {
pub destination: Sender<Arc<Log>>,
pub process: String,
}
impl Configuration {
pub fn named<S: Display>(process: S, sender: Sender<Arc<Log>>) -> Self {
Self {
destination: sender,
process: process.to_string(),
}
}
pub fn set_global(config: Self) {
GLOBAL_CONFIG.set(Arc::new(config)).unwrap();
}
pub async fn run<F: Future<Output = R> + Send, R: Send>(self, future: F) -> R {
TASK_CONFIG.scope(Some(Arc::new(self)), future).await
}
pub(crate) fn current() -> Option<Arc<Self>> {
TASK_CONFIG
.try_with(Clone::clone)
.unwrap_or_else(|_| GLOBAL_CONFIG.get().cloned())
}
}