use net_lattice_core::Id;
use crate::IpAddress;
use crate::address::Network;
pub type InterfaceAddressId = Id<InterfaceAddress>;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct InterfaceAddress {
pub id: InterfaceAddressId,
pub interface_index: u32,
pub address: Network,
pub broadcast: Option<IpAddress>,
}
impl InterfaceAddress {
pub fn new(id: InterfaceAddressId, interface_index: u32, address: Network) -> Self {
Self {
id,
interface_index,
address,
broadcast: None,
}
}
pub fn with_broadcast(mut self, broadcast: IpAddress) -> Self {
self.broadcast = Some(broadcast);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use net_lattice_ip::{Ipv4Address, Ipv4Network, Ipv4PrefixLength};
fn sample_network() -> Network {
Network::from(Ipv4Network::new(
Ipv4Address::new(192, 168, 1, 10),
Ipv4PrefixLength::new(24).unwrap(),
))
}
#[test]
fn new_address_has_no_broadcast() {
let addr = InterfaceAddress::new(InterfaceAddressId::new(1), 1, sample_network());
assert!(addr.broadcast.is_none());
}
#[test]
fn with_broadcast_sets_the_field() {
let broadcast = IpAddress::from(Ipv4Address::new(192, 168, 1, 255));
let addr = InterfaceAddress::new(InterfaceAddressId::new(1), 1, sample_network())
.with_broadcast(broadcast);
assert_eq!(addr.broadcast, Some(broadcast));
}
}