1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-License-Identifier: MIT

const NUD_INCOMPLETE: u16 = 0x01;
const NUD_REACHABLE: u16 = 0x02;
const NUD_STALE: u16 = 0x04;
const NUD_DELAY: u16 = 0x08;
const NUD_PROBE: u16 = 0x10;
const NUD_FAILED: u16 = 0x20;
const NUD_NOARP: u16 = 0x40;
const NUD_PERMANENT: u16 = 0x80;
const NUD_NONE: u16 = 0x00;

#[derive(Clone, Eq, PartialEq, Debug, Copy, Default)]
#[non_exhaustive]
pub enum NeighbourState {
    Incomplete,
    Reachable,
    Stale,
    Delay,
    Probe,
    Failed,
    Noarp,
    Permanent,
    #[default]
    None,
    Other(u16),
}

impl From<NeighbourState> for u16 {
    fn from(v: NeighbourState) -> u16 {
        match v {
            NeighbourState::Incomplete => NUD_INCOMPLETE,
            NeighbourState::Reachable => NUD_REACHABLE,
            NeighbourState::Stale => NUD_STALE,
            NeighbourState::Delay => NUD_DELAY,
            NeighbourState::Probe => NUD_PROBE,
            NeighbourState::Failed => NUD_FAILED,
            NeighbourState::Noarp => NUD_NOARP,
            NeighbourState::Permanent => NUD_PERMANENT,
            NeighbourState::None => NUD_NONE,
            NeighbourState::Other(d) => d,
        }
    }
}

impl From<u16> for NeighbourState {
    fn from(d: u16) -> Self {
        match d {
            NUD_INCOMPLETE => Self::Incomplete,
            NUD_REACHABLE => Self::Reachable,
            NUD_STALE => Self::Stale,
            NUD_DELAY => Self::Delay,
            NUD_PROBE => Self::Probe,
            NUD_FAILED => Self::Failed,
            NUD_NOARP => Self::Noarp,
            NUD_PERMANENT => Self::Permanent,
            NUD_NONE => Self::None,
            _ => Self::Other(d),
        }
    }
}

impl std::fmt::Display for NeighbourState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Incomplete => write!(f, "incomplete"),
            Self::Reachable => write!(f, "reachable"),
            Self::Stale => write!(f, "stale"),
            Self::Delay => write!(f, "delay"),
            Self::Probe => write!(f, "probe"),
            Self::Failed => write!(f, "failed"),
            Self::Noarp => write!(f, "noarp"),
            Self::Permanent => write!(f, "permanent"),
            Self::None => write!(f, "none"),
            Self::Other(d) => write!(f, "other({d}"),
        }
    }
}