mdns_proto/service/state.rs
1//! Service lifecycle states (RFC 6762 §8).
2
3use derive_more::{Display, IsVariant, TryUnwrap, Unwrap};
4
5/// Lifecycle state of a registered service.
6///
7/// Transitions:
8/// `Init` → `Probing(0..3)` → `Announcing(0..2)` → `Established`
9/// `Probing(_)` -\[conflict\]→ `Conflicting` (caller decides next step)
10#[derive(Debug, Display, Copy, Clone, Eq, PartialEq, Hash, IsVariant, Unwrap, TryUnwrap)]
11#[display("{}", self.as_str())]
12#[non_exhaustive]
13pub enum ServiceState {
14 /// Just registered; waiting on initial 0–250 ms randomized delay.
15 Init,
16 /// Probing: probes sent so far (0, 1, or 2 — third probe ends probing).
17 Probing(u8),
18 /// Announcing: announcements sent so far (0 or 1).
19 Announcing(u8),
20 /// Established and serving questions; periodically re-announces.
21 Established,
22 /// Detected a conflict while probing; caller must rename and restart.
23 Conflicting,
24}
25
26impl ServiceState {
27 /// Canonical lowercase slug for this state.
28 pub fn as_str(&self) -> &str {
29 match self {
30 Self::Init => "init",
31 Self::Probing(_) => "probing",
32 Self::Announcing(_) => "announcing",
33 Self::Established => "established",
34 Self::Conflicting => "conflicting",
35 }
36 }
37}
38
39#[cfg(test)]
40mod tests;