[pve-devel] [PATCH v4 proxmox 17/69] notify: add context

Lukas Wagner l.wagner at proxmox.com
Thu Jul 20 16:31:44 CEST 2023


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 <l.wagner at proxmox.com>
---

Notes:
    Changes since v3:
      - Use OnceCell instead of Mutex

 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;
-- 
2.39.2






More information about the pve-devel mailing list