numaperf_core/
enforcement.rs1#[derive(Debug, Clone, PartialEq, Eq, Default)]
9pub enum EnforcementLevel {
10 #[default]
15 Strict,
16
17 BestEffort {
22 reason: String,
24 },
25
26 None {
31 reason: String,
33 },
34}
35
36impl EnforcementLevel {
37 #[inline]
39 pub fn strict() -> Self {
40 Self::Strict
41 }
42
43 pub fn best_effort(reason: impl Into<String>) -> Self {
45 Self::BestEffort {
46 reason: reason.into(),
47 }
48 }
49
50 pub fn none(reason: impl Into<String>) -> Self {
52 Self::None {
53 reason: reason.into(),
54 }
55 }
56
57 #[inline]
59 pub fn is_strict(&self) -> bool {
60 matches!(self, Self::Strict)
61 }
62
63 #[inline]
65 pub fn is_best_effort(&self) -> bool {
66 matches!(self, Self::BestEffort { .. })
67 }
68
69 #[inline]
71 pub fn is_none(&self) -> bool {
72 matches!(self, Self::None { .. })
73 }
74
75 pub fn reason(&self) -> Option<&str> {
77 match self {
78 Self::Strict => None,
79 Self::BestEffort { reason } | Self::None { reason } => Some(reason),
80 }
81 }
82}
83
84impl std::fmt::Display for EnforcementLevel {
85 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86 match self {
87 Self::Strict => write!(f, "strict"),
88 Self::BestEffort { reason } => write!(f, "best-effort ({})", reason),
89 Self::None { reason } => write!(f, "none ({})", reason),
90 }
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_enforcement_level() {
100 let strict = EnforcementLevel::strict();
101 assert!(strict.is_strict());
102 assert!(!strict.is_best_effort());
103 assert!(strict.reason().is_none());
104
105 let best = EnforcementLevel::best_effort("missing CAP_SYS_ADMIN");
106 assert!(best.is_best_effort());
107 assert_eq!(best.reason(), Some("missing CAP_SYS_ADMIN"));
108
109 let none = EnforcementLevel::none("NUMA not supported");
110 assert!(none.is_none());
111 assert_eq!(none.reason(), Some("NUMA not supported"));
112 }
113
114 #[test]
115 fn test_enforcement_display() {
116 assert_eq!(format!("{}", EnforcementLevel::strict()), "strict");
117 assert_eq!(
118 format!("{}", EnforcementLevel::best_effort("reason")),
119 "best-effort (reason)"
120 );
121 }
122}