use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(tag = "policy", rename_all = "snake_case")]
pub enum DerivePolicy {
Legacy,
Reset {
threshold_tokens: usize,
},
}
impl DerivePolicy {
pub fn kind(&self) -> &'static str {
match self {
DerivePolicy::Legacy => "legacy",
DerivePolicy::Reset { .. } => "reset",
}
}
}
impl Default for DerivePolicy {
fn default() -> Self {
DerivePolicy::Legacy
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_legacy() {
assert!(matches!(DerivePolicy::default(), DerivePolicy::Legacy));
}
#[test]
fn reset_carries_threshold() {
let p = DerivePolicy::Reset {
threshold_tokens: 8192,
};
if let DerivePolicy::Reset { threshold_tokens } = p {
assert_eq!(threshold_tokens, 8192);
} else {
panic!("expected Reset");
}
}
#[test]
fn kind_is_snake_case_and_distinct() {
assert_eq!(DerivePolicy::Legacy.kind(), "legacy");
assert_eq!(
DerivePolicy::Reset {
threshold_tokens: 0
}
.kind(),
"reset"
);
}
#[test]
fn policy_round_trips_through_serde() {
let p = DerivePolicy::Reset {
threshold_tokens: 12_000,
};
let json = serde_json::to_string(&p).unwrap();
assert!(json.contains("\"policy\":\"reset\""));
let back: DerivePolicy = serde_json::from_str(&json).unwrap();
assert!(matches!(
back,
DerivePolicy::Reset {
threshold_tokens: 12_000
}
));
}
}