use crate::Config;
use derive_more::{Display, Error};
use std::sync::OnceLock;
#[derive(Debug, Clone, Copy, Default)]
struct GlobalDefaults {
config: Config,
instance: (u64, u64),
}
static GLOBAL_DEFAULTS: OnceLock<GlobalDefaults> = OnceLock::new();
#[derive(Display, Error, Debug, Clone, PartialEq, Eq)]
pub enum DefaultConfigError {
#[display("Global defaults have already been set")]
DefaultsAlreadySet,
}
pub fn set_defaults(
config: Config,
worker_id: u64,
process_id: u64,
) -> Result<(), DefaultConfigError> {
let defaults = GlobalDefaults {
config,
instance: (worker_id, process_id),
};
GLOBAL_DEFAULTS
.set(defaults)
.map_err(|_| DefaultConfigError::DefaultsAlreadySet)
}
pub fn set_default_config(config: Config) -> Result<(), DefaultConfigError> {
let defaults = GlobalDefaults {
config,
instance: (0, 0),
};
GLOBAL_DEFAULTS
.set(defaults)
.map_err(|_| DefaultConfigError::DefaultsAlreadySet)
}
pub fn set_default_instance(worker_id: u64, process_id: u64) -> Result<(), DefaultConfigError> {
let defaults = GlobalDefaults {
config: Config::default(),
instance: (worker_id, process_id),
};
GLOBAL_DEFAULTS
.set(defaults)
.map_err(|_| DefaultConfigError::DefaultsAlreadySet)
}
pub fn get_default_config() -> Config {
GLOBAL_DEFAULTS.get_or_init(GlobalDefaults::default).config
}
pub fn get_default_instance() -> (u64, u64) {
GLOBAL_DEFAULTS
.get_or_init(GlobalDefaults::default)
.instance
}
pub fn is_defaults_set() -> bool {
GLOBAL_DEFAULTS.get().is_some()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_config_fallback() {
let config = get_default_config();
let default_config = Config::default();
assert_eq!(
config.layout().timestamp(),
default_config.layout().timestamp()
);
assert_eq!(config.layout().worker(), default_config.layout().worker());
assert_eq!(config.layout().process(), default_config.layout().process());
assert_eq!(
config.layout().sequence(),
default_config.layout().sequence()
);
assert_eq!(config.epoch(), default_config.epoch());
}
#[test]
fn default_instance_zero_zero() {
let (worker_id, process_id) = get_default_instance();
assert_eq!(worker_id, 0);
assert_eq!(process_id, 0);
}
}