use net_lattice_core::Id;
use crate::address::{IpAddress, Network};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Route {
pub id: RouteId,
pub destination: Network,
pub gateway: Option<IpAddress>,
pub metric: Option<u32>,
pub interface_index: Option<u32>,
}
pub type RouteId = Id<Route>;
impl Route {
pub fn new(id: RouteId, destination: Network) -> Self {
Self {
id,
destination,
gateway: None,
metric: None,
interface_index: None,
}
}
pub fn with_gateway(mut self, gateway: IpAddress) -> Self {
self.gateway = Some(gateway);
self
}
pub fn with_metric(mut self, metric: u32) -> Self {
self.metric = Some(metric);
self
}
pub fn with_interface_index(mut self, interface_index: u32) -> Self {
self.interface_index = Some(interface_index);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use net_lattice_ip::{Ipv4Address, Ipv4Network, Ipv4PrefixLength};
fn destination() -> Network {
Network::from(Ipv4Network::new(
Ipv4Address::new(10, 0, 0, 0),
Ipv4PrefixLength::new(24).unwrap(),
))
}
#[test]
fn new_route_has_no_gateway_or_metric() {
let route = Route::new(RouteId::new(1), destination());
assert!(route.gateway.is_none());
assert!(route.metric.is_none());
}
#[test]
fn builder_methods_set_optional_fields() {
let gateway = IpAddress::from(Ipv4Address::new(10, 0, 0, 1));
let route = Route::new(RouteId::new(1), destination())
.with_gateway(gateway)
.with_metric(100)
.with_interface_index(2);
assert_eq!(route.gateway, Some(gateway));
assert_eq!(route.metric, Some(100));
assert_eq!(route.interface_index, Some(2));
}
}