1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SPDX-License-Identifier: MIT

use netlink_packet_utils::{
    traits::{Emitable, Parseable},
    DecodeError,
};

const MAX_PHYS_ITEM_ID_LEN: usize = 32;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
pub struct LinkPhysId {
    pub id: [u8; MAX_PHYS_ITEM_ID_LEN],
    pub len: usize,
}

impl Parseable<[u8]> for LinkPhysId {
    fn parse(buf: &[u8]) -> Result<Self, DecodeError> {
        let len = buf.len() % MAX_PHYS_ITEM_ID_LEN;
        let mut id = [0; MAX_PHYS_ITEM_ID_LEN];
        id[..len].copy_from_slice(&buf[..len]);
        Ok(Self { id, len })
    }
}

impl Emitable for LinkPhysId {
    fn buffer_len(&self) -> usize {
        self.len
    }

    fn emit(&self, buffer: &mut [u8]) {
        buffer[..self.len].copy_from_slice(&self.id[..self.len])
    }
}