redup_abstractions/
phase.rs1use core::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub enum Phase {
9 Unknown,
11 Serving,
13 Starting,
15 NotServing,
17 Failed,
19}
20
21impl Phase {
22 pub fn is_serving(self) -> bool {
24 self == Self::Serving
25 }
26
27 fn counted(self) -> Self {
29 match self {
30 Self::Unknown => Self::Starting,
31 other => other,
32 }
33 }
34
35 pub fn worst(phases: impl IntoIterator<Item = Self>) -> Self {
37 phases
38 .into_iter()
39 .fold(Self::Serving, |worst, phase| worst.max(phase.counted()))
40 }
41}
42
43impl fmt::Display for Phase {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 let name = match self {
46 Self::Unknown => "Unknown",
47 Self::Serving => "Serving",
48 Self::Starting => "Starting",
49 Self::NotServing => "NotServing",
50 Self::Failed => "Failed",
51 };
52 f.write_str(name)
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::Phase;
59
60 #[test]
61 fn later_phase_wins() {
62 assert_eq!(Phase::Starting.max(Phase::Serving), Phase::Starting);
63 assert_eq!(Phase::Failed.max(Phase::NotServing), Phase::Failed);
64 assert!(Phase::Serving < Phase::Starting);
65 }
66
67 #[test]
68 fn unknown_counts_as_starting_in_aggregate() {
69 assert_eq!(
70 Phase::worst([Phase::Serving, Phase::Unknown]),
71 Phase::Starting
72 );
73 assert_eq!(Phase::worst([Phase::Unknown]), Phase::Starting);
74 assert_eq!(
75 Phase::worst([Phase::Serving, Phase::Serving]),
76 Phase::Serving
77 );
78 }
79
80 #[test]
81 fn empty_aggregate_is_serving() {
82 assert_eq!(Phase::worst([]), Phase::Serving);
83 }
84}