use crate::{
ethernet::{EtherType, EthernetHeader, EthernetPacket},
packet::Packet,
};
use bytes::Bytes;
use nex_core::mac::MacAddr;
#[derive(Debug, Clone)]
pub struct EthernetPacketBuilder {
packet: EthernetPacket,
}
impl EthernetPacketBuilder {
pub fn new() -> Self {
Self {
packet: EthernetPacket {
header: EthernetHeader {
destination: MacAddr::zero(),
source: MacAddr::zero(),
ethertype: EtherType::Ipv4,
},
payload: Bytes::new(),
},
}
}
pub fn destination(mut self, mac: MacAddr) -> Self {
self.packet.header.destination = mac;
self
}
pub fn source(mut self, mac: MacAddr) -> Self {
self.packet.header.source = mac;
self
}
pub fn ethertype(mut self, ether_type: EtherType) -> Self {
self.packet.header.ethertype = ether_type;
self
}
pub fn payload(mut self, payload: Bytes) -> Self {
self.packet.payload = payload;
self
}
pub fn build(self) -> EthernetPacket {
self.packet
}
pub fn to_bytes(self) -> Bytes {
self.packet.to_bytes()
}
pub fn header_bytes(&self) -> Bytes {
self.packet.header.to_bytes()
}
}