use crate::descriptors::entry_list::{Entry, list_len, parse_list, write_list};
use crate::error::Result;
use alloc::vec::Vec;
use broadcast_common::{Parse, Serialize};
use core::net::Ipv4Addr;
pub const TAG: u8 = 0x10;
const ENTRY_LEN: usize = 10;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Ipv4SourceDestSlash {
pub source_addr: Ipv4Addr,
pub source_slash_mask: u8,
pub dest_addr: Ipv4Addr,
pub dest_slash_mask: u8,
}
impl Entry<ENTRY_LEN> for Ipv4SourceDestSlash {
fn read(b: &[u8; ENTRY_LEN]) -> Self {
Self {
source_addr: Ipv4Addr::new(b[0], b[1], b[2], b[3]),
source_slash_mask: b[4],
dest_addr: Ipv4Addr::new(b[5], b[6], b[7], b[8]),
dest_slash_mask: b[9],
}
}
fn write(&self, b: &mut [u8; ENTRY_LEN]) {
b[..4].copy_from_slice(&self.source_addr.octets());
b[4] = self.source_slash_mask;
b[5..9].copy_from_slice(&self.dest_addr.octets());
b[9] = self.dest_slash_mask;
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
pub struct TargetIpSourceSlashDescriptor<'a> {
pub entries: Vec<Ipv4SourceDestSlash>,
pub trailing: &'a [u8],
}
impl<'a> Parse<'a> for TargetIpSourceSlashDescriptor<'a> {
type Error = crate::error::Error;
fn parse(bytes: &'a [u8]) -> Result<Self> {
let (entries, trailing) = parse_list::<ENTRY_LEN, Ipv4SourceDestSlash>(
bytes,
TAG,
"TargetIpSourceSlashDescriptor",
)?;
Ok(Self { entries, trailing })
}
}
impl Serialize for TargetIpSourceSlashDescriptor<'_> {
type Error = crate::error::Error;
fn serialized_len(&self) -> usize {
list_len(self.entries.len(), ENTRY_LEN, self.trailing.len())
}
fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
write_list::<ENTRY_LEN, _>(buf, TAG, &self.entries, self.trailing)
}
}
impl<'a> crate::traits::DescriptorDef<'a> for TargetIpSourceSlashDescriptor<'a> {
const TAG: u8 = TAG;
const NAME: &'static str = "TARGET_IP_SOURCE_SLASH";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialize_rejects_entry_sized_trailing() {
let mut d = TargetIpSourceSlashDescriptor::parse(&[TAG, 0]).unwrap();
let long = [0u8; ENTRY_LEN];
d.trailing = &long;
assert!(matches!(
d.serialize_into(&mut [0u8; 64]),
Err(crate::Error::InvalidDescriptor { .. })
));
let mut buf = [0u8; 64];
d.trailing = &long[..ENTRY_LEN - 1];
let n = d.serialize_into(&mut buf).unwrap();
assert_eq!(TargetIpSourceSlashDescriptor::parse(&buf[..n]).unwrap(), d);
}
#[test]
fn parse_and_round_trip() {
let bytes = [TAG, 10, 1, 2, 3, 4, 8, 5, 6, 7, 8, 16];
let d = TargetIpSourceSlashDescriptor::parse(&bytes).unwrap();
assert_eq!(d.entries.len(), 1);
assert_eq!(d.entries[0].source_addr, Ipv4Addr::new(1, 2, 3, 4));
assert_eq!(d.entries[0].source_slash_mask, 8);
assert_eq!(d.entries[0].dest_addr, Ipv4Addr::new(5, 6, 7, 8));
assert_eq!(d.entries[0].dest_slash_mask, 16);
let mut out = vec![0u8; d.serialized_len()];
d.serialize_into(&mut out).unwrap();
assert_eq!(out, bytes);
}
#[test]
fn empty_and_partial_tail_round_trip() {
for bytes in [&[TAG, 0][..], &[TAG, 4, 1, 2, 3, 4][..]] {
let d = TargetIpSourceSlashDescriptor::parse(bytes).unwrap();
let mut out = vec![0u8; d.serialized_len()];
d.serialize_into(&mut out).unwrap();
assert_eq!(out, bytes);
}
assert!(matches!(
TargetIpSourceSlashDescriptor::parse(&[0x0F, 0]),
Err(crate::Error::InvalidDescriptor { .. })
));
}
}