pjson_rs_domain/value_objects/
priority.rs1use crate::{DomainError, DomainResult};
7use std::fmt;
8use std::num::NonZeroU8;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
25pub struct Priority(NonZeroU8);
26
27impl Priority {
28 pub const CRITICAL: Self = Self::new_unchecked(100);
30
31 pub const HIGH: Self = Self::new_unchecked(80);
33
34 pub const MEDIUM: Self = Self::new_unchecked(50);
36
37 pub const LOW: Self = Self::new_unchecked(25);
39
40 pub const BACKGROUND: Self = Self::new_unchecked(10);
42
43 pub fn new(value: u8) -> DomainResult<Self> {
45 NonZeroU8::new(value)
46 .map(Self)
47 .ok_or_else(|| DomainError::InvalidPriority("Priority cannot be zero".to_string()))
48 }
49
50 const fn new_unchecked(value: u8) -> Self {
52 unsafe { Self(NonZeroU8::new_unchecked(value)) }
54 }
55
56 pub fn value(self) -> u8 {
58 self.0.get()
59 }
60
61 pub fn increase_by(self, delta: u8) -> Self {
63 let new_value = self.0.get().saturating_add(delta);
64 Self(NonZeroU8::new(new_value).unwrap_or(NonZeroU8::MAX))
65 }
66
67 pub fn decrease_by(self, delta: u8) -> Self {
69 let new_value = self.0.get().saturating_sub(delta);
70 Self(NonZeroU8::new(new_value).unwrap_or(NonZeroU8::MIN))
71 }
72
73 pub fn is_critical(self) -> bool {
75 self.0.get() >= Self::CRITICAL.0.get()
76 }
77
78 pub fn is_high_or_above(self) -> bool {
80 self.0.get() >= Self::HIGH.0.get()
81 }
82
83 pub fn from_percentage(percent: f32) -> DomainResult<Self> {
85 if !(0.0..=100.0).contains(&percent) {
86 return Err(DomainError::InvalidPriority(format!(
87 "Percentage must be 0-100, got {percent}"
88 )));
89 }
90
91 let value = (percent * 2.55).round() as u8;
92 Self::new(value.max(1)) }
94
95 pub fn to_percentage(self) -> f32 {
97 (self.0.get() as f32 / 255.0) * 100.0
98 }
99
100 pub fn unwrap_or(self, _default: u8) -> u8 {
102 self.0.get()
103 }
104}
105
106impl fmt::Display for Priority {
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108 match *self {
109 Self::CRITICAL => {
110 let val = self.0.get();
111 write!(f, "Critical({val})")
112 }
113 Self::HIGH => {
114 let val = self.0.get();
115 write!(f, "High({val})")
116 }
117 Self::MEDIUM => {
118 let val = self.0.get();
119 write!(f, "Medium({val})")
120 }
121 Self::LOW => {
122 let val = self.0.get();
123 write!(f, "Low({val})")
124 }
125 Self::BACKGROUND => {
126 let val = self.0.get();
127 write!(f, "Background({val})")
128 }
129 _ => {
130 let val = self.0.get();
131 write!(f, "Priority({val})")
132 }
133 }
134 }
135}
136
137impl From<Priority> for u8 {
138 fn from(priority: Priority) -> Self {
139 priority.0.get()
140 }
141}
142
143impl TryFrom<u8> for Priority {
144 type Error = DomainError;
145
146 fn try_from(value: u8) -> Result<Self, Self::Error> {
147 Self::new(value)
148 }
149}
150
151#[cfg(test)]
152mod tests {
153 use super::*;
154
155 trait PriorityRule {
156 fn validate(&self, priority: Priority) -> bool;
157 fn name(&self) -> &'static str;
158 }
159
160 struct MinimumPriority(Priority);
161
162 impl PriorityRule for MinimumPriority {
163 fn validate(&self, priority: Priority) -> bool {
164 priority >= self.0
165 }
166
167 fn name(&self) -> &'static str {
168 "minimum_priority"
169 }
170 }
171
172 struct PriorityRange {
173 min: Priority,
174 max: Priority,
175 }
176
177 impl PriorityRule for PriorityRange {
178 fn validate(&self, priority: Priority) -> bool {
179 priority >= self.min && priority <= self.max
180 }
181
182 fn name(&self) -> &'static str {
183 "priority_range"
184 }
185 }
186
187 struct PriorityRules {
188 rules: Vec<Box<dyn PriorityRule + Send + Sync>>,
189 }
190
191 impl PriorityRules {
192 fn new() -> Self {
193 Self { rules: Vec::new() }
194 }
195
196 fn add_rule(mut self, rule: impl PriorityRule + Send + Sync + 'static) -> Self {
197 self.rules.push(Box::new(rule));
198 self
199 }
200
201 fn validate(&self, priority: Priority) -> DomainResult<()> {
202 for rule in &self.rules {
203 if !rule.validate(priority) {
204 return Err(DomainError::InvalidPriority(format!(
205 "Priority {priority} violates rule: {}",
206 rule.name()
207 )));
208 }
209 }
210 Ok(())
211 }
212 }
213
214 #[test]
215 fn test_priority_constants() {
216 assert_eq!(Priority::CRITICAL.value(), 100);
217 assert_eq!(Priority::HIGH.value(), 80);
218 assert_eq!(Priority::MEDIUM.value(), 50);
219 assert_eq!(Priority::LOW.value(), 25);
220 assert_eq!(Priority::BACKGROUND.value(), 10);
221 }
222
223 #[test]
224 fn test_priority_validation() {
225 assert!(Priority::new(1).is_ok());
226 assert!(Priority::new(255).is_ok());
227 assert!(Priority::new(0).is_err());
228 }
229
230 #[test]
231 fn test_priority_ordering() {
232 assert!(Priority::CRITICAL > Priority::HIGH);
233 assert!(Priority::HIGH > Priority::MEDIUM);
234 assert!(Priority::MEDIUM > Priority::LOW);
235 assert!(Priority::LOW > Priority::BACKGROUND);
236 }
237
238 #[test]
239 fn test_priority_arithmetic() {
240 let p = Priority::MEDIUM;
241 assert_eq!(p.increase_by(10).value(), 60);
242 assert_eq!(p.decrease_by(10).value(), 40);
243
244 let max_p = Priority::new(255).unwrap();
246 assert_eq!(max_p.increase_by(10).value(), 255);
247
248 let min_p = Priority::new(1).unwrap();
249 assert_eq!(min_p.decrease_by(10).value(), 1);
250 }
251
252 #[test]
253 fn test_priority_percentage() {
254 let p = Priority::from_percentage(50.0).unwrap();
255 assert!(p.to_percentage() >= 49.0 && p.to_percentage() <= 51.0);
256
257 assert!(Priority::from_percentage(101.0).is_err());
258 assert!(Priority::from_percentage(-1.0).is_err());
259 }
260
261 #[test]
262 fn test_priority_rules() {
263 let rules = PriorityRules::new()
264 .add_rule(MinimumPriority(Priority::LOW))
265 .add_rule(PriorityRange {
266 min: Priority::LOW,
267 max: Priority::CRITICAL,
268 });
269
270 assert!(rules.validate(Priority::MEDIUM).is_ok());
271 assert!(rules.validate(Priority::new(5).unwrap()).is_err());
272 assert!(rules.validate(Priority::new(200).unwrap()).is_err());
273 }
274
275 #[test]
276 fn test_priority_display() {
277 assert_eq!(Priority::CRITICAL.to_string(), "Critical(100)");
278 assert_eq!(Priority::HIGH.to_string(), "High(80)");
279 assert_eq!(Priority::new(42).unwrap().to_string(), "Priority(42)");
280 }
281}