#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum RouteSource {
Default,
Environment,
Fallback,
}
impl RouteSource {
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Default => "default",
Self::Environment => "env",
Self::Fallback => "fallback",
}
}
}
pub(crate) fn is_kill_spelling(value: &str) -> bool {
matches!(value, "0" | "off" | "false" | "stock")
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RouteAudit {
pub name: &'static str,
pub value: String,
pub source: &'static str,
}
impl RouteAudit {
pub(crate) fn new(name: &'static str, value: impl Into<String>, source: &'static str) -> Self {
Self {
name,
value: value.into(),
source,
}
}
}
pub(crate) fn audit_text(routes: &[RouteAudit]) -> String {
routes
.iter()
.map(|route| format!("{}={} ({})", route.name, route.value, route.source))
.collect::<Vec<_>>()
.join(" ")
}
pub(crate) const AUDIT_TARGET: &str = "hf2q::audit";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct Resolved<T> {
pub(crate) value: T,
pub(crate) source: RouteSource,
}
impl<T> Resolved<T> {
pub(crate) fn default_value(value: T) -> Self {
Self {
value,
source: RouteSource::Default,
}
}
pub(crate) fn environment(value: T) -> Self {
Self {
value,
source: RouteSource::Environment,
}
}
pub(crate) fn fallback(value: T) -> Self {
Self {
value,
source: RouteSource::Fallback,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn audit_text_names_value_and_source_of_every_route() {
let routes = [
RouteAudit::new("a", "On", RouteSource::Default.as_str()),
RouteAudit::new("b", "Stock", RouteSource::Environment.as_str()),
RouteAudit::new("c", "VecHybrid", RouteSource::Fallback.as_str()),
];
assert_eq!(
audit_text(&routes),
"a=On (default) b=Stock (env) c=VecHybrid (fallback)"
);
}
#[test]
fn kill_spellings_are_the_four_off_values() {
for value in ["0", "off", "false", "stock"] {
assert!(is_kill_spelling(value), "{value}");
}
for value in ["", "1", "no", "none", "disable", "Off"] {
assert!(!is_kill_spelling(value), "{value}");
}
}
}