From 7035d57312031bdfbb4ff339e7d3cf6e2a8ef5d6 Mon Sep 17 00:00:00 2001 From: Lukas Wagner Date: Tue, 23 Apr 2024 13:51:48 +0200 Subject: [PATCH] notify: use std::sync::OnceCell instead of lazy_static! Signed-off-by: Lukas Wagner Tested-by: Maximiliano Sandoval --- proxmox-notify/Cargo.toml | 1 - proxmox-notify/src/config.rs | 23 +++++++++++++++-------- proxmox-notify/src/lib.rs | 5 ++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/proxmox-notify/Cargo.toml b/proxmox-notify/Cargo.toml index 797b1acf..3a97847d 100644 --- a/proxmox-notify/Cargo.toml +++ b/proxmox-notify/Cargo.toml @@ -11,7 +11,6 @@ exclude.workspace = true anyhow.workspace = true const_format.workspace = true handlebars = { workspace = true } -lazy_static.workspace = true lettre = { workspace = true, optional = true } log.workspace = true mail-parser = { workspace = true, optional = true } diff --git a/proxmox-notify/src/config.rs b/proxmox-notify/src/config.rs index fe25ea74..44453713 100644 --- a/proxmox-notify/src/config.rs +++ b/proxmox-notify/src/config.rs @@ -1,4 +1,4 @@ -use lazy_static::lazy_static; +use std::sync::OnceLock; use proxmox_schema::{ApiType, ObjectSchema}; use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin}; @@ -9,9 +9,16 @@ use crate::matcher::{MatcherConfig, MATCHER_TYPENAME}; use crate::schema::BACKEND_NAME_SCHEMA; use crate::Error; -lazy_static! { - pub static ref CONFIG: SectionConfig = config_init(); - pub static ref PRIVATE_CONFIG: SectionConfig = private_config_init(); +/// Section config schema for the public config file. +pub fn config_parser() -> &'static SectionConfig { + static CONFIG: OnceLock = OnceLock::new(); + CONFIG.get_or_init(|| config_init()) +} + +/// Section config schema for the private config file. +pub fn private_config_parser() -> &'static SectionConfig { + static CONFIG: OnceLock = OnceLock::new(); + CONFIG.get_or_init(|| private_config_init()) } fn config_init() -> SectionConfig { @@ -108,7 +115,7 @@ fn private_config_init() -> SectionConfig { pub fn config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error> { let digest = openssl::sha::sha256(raw_config.as_bytes()); - let mut data = CONFIG + let mut data = config_parser() .parse("notifications.cfg", raw_config) .map_err(|err| Error::ConfigDeserialization(err.into()))?; @@ -139,20 +146,20 @@ pub fn config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error> pub fn private_config(raw_config: &str) -> Result<(SectionConfigData, [u8; 32]), Error> { let digest = openssl::sha::sha256(raw_config.as_bytes()); - let data = PRIVATE_CONFIG + let data = private_config_parser() .parse("priv/notifications.cfg", raw_config) .map_err(|err| Error::ConfigDeserialization(err.into()))?; Ok((data, digest)) } pub fn write(config: &SectionConfigData) -> Result { - CONFIG + config_parser() .write("notifications.cfg", config) .map_err(|err| Error::ConfigSerialization(err.into())) } pub fn write_private(config: &SectionConfigData) -> Result { - PRIVATE_CONFIG + private_config_parser() .write("priv/notifications.cfg", config) .map_err(|err| Error::ConfigSerialization(err.into())) } diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs index f9917d9f..c69ac26d 100644 --- a/proxmox-notify/src/lib.rs +++ b/proxmox-notify/src/lib.rs @@ -13,12 +13,11 @@ use proxmox_section_config::SectionConfigData; use proxmox_uuid::Uuid; pub mod matcher; -use crate::config::CONFIG; use matcher::{MatcherConfig, MATCHER_TYPENAME}; pub mod api; -pub mod context; pub mod config; +pub mod context; pub mod endpoints; pub mod filter; pub mod group; @@ -280,7 +279,7 @@ impl Config { let default_config = context().default_config(); - let builtin_config = CONFIG + let builtin_config = config::config_parser() .parse("", default_config) .map_err(|err| Error::ConfigDeserialization(err.into()))?;