#[derive(Debug)]
pub struct Policy {
pub(crate) inner: PolicyInner,
}
#[derive(Debug)]
pub(crate) enum PolicyInner {
Limited(u32),
None,
}
impl Policy {
pub fn limited(max: usize) -> Self {
let clamped = u32::try_from(max).unwrap_or(u32::MAX);
Self {
inner: PolicyInner::Limited(clamped),
}
}
pub fn none() -> Self {
Self {
inner: PolicyInner::None,
}
}
}
impl Default for Policy {
fn default() -> Self {
Self::limited(10)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn policy_construction() {
type TestCase<'a> = (&'a str, Policy, fn(&PolicyInner) -> bool);
let cases: Vec<TestCase<'_>> = vec![
("limited(5)", Policy::limited(5), |p| matches!(p, PolicyInner::Limited(5))),
("none", Policy::none(), |p| matches!(p, PolicyInner::None)),
("default", Policy::default(), |p| matches!(p, PolicyInner::Limited(10))),
("limited(0)", Policy::limited(0), |p| matches!(p, PolicyInner::Limited(0))),
("limited(usize::MAX) saturates", Policy::limited(usize::MAX), |p| {
matches!(p, PolicyInner::Limited(u32::MAX))
}),
];
for (label, policy, check) in &cases {
assert!(check(&policy.inner), "Policy::{label}: unexpected inner");
}
}
#[test]
fn policy_debug() {
let p = Policy::none();
let s = format!("{p:?}");
assert!(s.contains("None"));
}
}