use std::future::Future;
tokio::task_local! {
static CONTEXT: WriteContext;
}
#[derive(Clone, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct WriteContext {
pub principal: Option<ciborium::Value>,
pub group: Option<String>,
}
impl WriteContext {
pub fn new() -> Self {
Self::default()
}
pub fn with_principal(mut self, principal: ciborium::Value) -> Self {
self.principal = Some(principal);
self
}
pub fn with_group(mut self, group: impl Into<String>) -> Self {
self.group = Some(group.into());
self
}
pub async fn scope<F: Future>(self, fut: F) -> F::Output {
CONTEXT.scope(self, fut).await
}
pub fn current() -> Option<WriteContext> {
CONTEXT.try_with(|c| c.clone()).ok()
}
}