mod define_fn_wrapper;
use std::sync::Arc;
pub(crate) use define_fn_wrapper::define_fn_wrapper;
#[cfg(any(feature = "metrics", test))]
mod attributes;
#[cfg(any(feature = "metrics", test))]
pub(crate) use attributes::*;
mod telemetry_helper;
pub(crate) use telemetry_helper::TelemetryHelper;
#[derive(Default)]
pub(crate) enum EnableIf<In> {
#[default]
Enabled,
Disabled,
Custom(Arc<dyn Fn(&In) -> bool + Send + Sync>),
}
impl<In> EnableIf<In> {
pub fn new(enabled: bool) -> Self {
if enabled { Self::Enabled } else { Self::Disabled }
}
pub fn custom(predicate: impl Fn(&In) -> bool + Send + Sync + 'static) -> Self {
Self::Custom(Arc::new(predicate))
}
#[cfg_attr(test, mutants::skip)] pub fn call(&self, input: &In) -> bool {
match self {
Self::Enabled => true,
Self::Disabled => false,
Self::Custom(predicate) => predicate(input),
}
}
}
impl<In> Clone for EnableIf<In> {
fn clone(&self) -> Self {
match self {
Self::Enabled => Self::Enabled,
Self::Disabled => Self::Disabled,
Self::Custom(predicate) => Self::Custom(Arc::clone(predicate)),
}
}
}
impl<In> std::fmt::Debug for EnableIf<In> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Enabled => write!(f, "Enabled"),
Self::Disabled => write!(f, "Disabled"),
Self::Custom(_) => write!(f, "Custom"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn enable_if_enabled_debug() {
let enable_if: EnableIf<String> = EnableIf::default();
assert_eq!(format!("{enable_if:?}"), "Enabled");
}
#[test]
fn enable_if_disabled_debug() {
let enable_if: EnableIf<String> = EnableIf::new(false);
assert_eq!(format!("{enable_if:?}"), "Disabled");
}
#[test]
fn enable_if_custom_debug() {
let enable_if: EnableIf<String> = EnableIf::custom(|_| true);
assert_eq!(format!("{enable_if:?}"), "Custom");
}
}