use net_lattice_core::Id;
use crate::IpAddress;
use crate::mac::MacAddress;
pub type NeighborId = Id<NeighborEntry>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum NeighborState {
Incomplete,
Reachable,
Stale,
Delay,
Probe,
Failed,
Permanent,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct NeighborEntry {
pub id: NeighborId,
pub interface_index: u32,
pub address: IpAddress,
pub mac: Option<MacAddress>,
pub state: NeighborState,
}
impl NeighborEntry {
pub fn new(id: NeighborId, interface_index: u32, address: IpAddress) -> Self {
Self {
id,
interface_index,
address,
mac: None,
state: NeighborState::Unknown,
}
}
pub fn with_mac(mut self, mac: MacAddress) -> Self {
self.mac = Some(mac);
self
}
pub fn with_state(mut self, state: NeighborState) -> Self {
self.state = state;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use net_lattice_ip::Ipv4Address;
#[test]
fn new_entry_has_no_mac_and_unknown_state() {
let entry = NeighborEntry::new(
NeighborId::new(1),
1,
IpAddress::from(Ipv4Address::new(192, 168, 1, 1)),
);
assert!(entry.mac.is_none());
assert_eq!(entry.state, NeighborState::Unknown);
}
#[test]
fn builder_methods_set_optional_fields() {
let mac = MacAddress::new([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
let entry = NeighborEntry::new(
NeighborId::new(1),
1,
IpAddress::from(Ipv4Address::new(192, 168, 1, 1)),
)
.with_mac(mac)
.with_state(NeighborState::Reachable);
assert_eq!(entry.mac, Some(mac));
assert_eq!(entry.state, NeighborState::Reachable);
}
}