From d6c1f181d65b7e07e0873c4390e6a53242090df5 Mon Sep 17 00:00:00 2001 From: Lukas Wagner Date: Thu, 20 Jul 2023 16:31:44 +0200 Subject: [PATCH] notify: add context Since `proxmox-notify` is intended to be used by multiple products, there needs to be a way to inject product-specific behavior. Signed-off-by: Lukas Wagner --- proxmox-notify/Cargo.toml | 1 + proxmox-notify/src/context.rs | 14 ++++++++++++++ proxmox-notify/src/lib.rs | 1 + 3 files changed, 16 insertions(+) create mode 100644 proxmox-notify/src/context.rs diff --git a/proxmox-notify/Cargo.toml b/proxmox-notify/Cargo.toml index 6bf4d076..5cceb0b7 100644 --- a/proxmox-notify/Cargo.toml +++ b/proxmox-notify/Cargo.toml @@ -11,6 +11,7 @@ exclude.workspace = true handlebars = { workspace = true } lazy_static.workspace = true log.workspace = true +once_cell.workspace = true openssl.workspace = true proxmox-http = { workspace = true, features = ["client-sync"], optional = true } proxmox-human-byte.workspace = true diff --git a/proxmox-notify/src/context.rs b/proxmox-notify/src/context.rs new file mode 100644 index 00000000..660b27fb --- /dev/null +++ b/proxmox-notify/src/context.rs @@ -0,0 +1,14 @@ +use once_cell::sync::OnceCell; +use std::fmt::Debug; + +pub trait Context: Send + Sync + Debug {} + +static CONTEXT: OnceCell<&'static dyn Context> = OnceCell::new(); + +pub fn set_context(context: &'static dyn Context) { + CONTEXT.set(context).expect("context has already been set"); +} + +pub(crate) fn context() -> &'static dyn Context { + *CONTEXT.get().expect("context has not been yet") +} diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs index e254604b..0059e44b 100644 --- a/proxmox-notify/src/lib.rs +++ b/proxmox-notify/src/lib.rs @@ -13,6 +13,7 @@ use std::error::Error as StdError; pub mod api; mod config; +pub mod context; pub mod endpoints; pub mod filter; pub mod group;