toe-beans 0.10.0

DHCP library, client, and server
Documentation
use crate::v4::error::Result;
use serde::{Deserialize, Serialize};

/// This option specifies the maximum length DHCP message that it is
/// willing to accept.
///
/// A client may use this option in DHCPDISCOVER or DHCPREQUEST messages,
/// but should not use the option in DHCPDECLINE messages.
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct MaxMessage(u16);

impl MaxMessage {
    /// Create a `MaxMessage`.
    pub fn new(size: u16) -> Result<Self> {
        if size < 576 {
            return Err("Maximum message size must be at least 576");
        }

        Ok(Self(size))
    }

    /// Used, when serializing, to add to the byte buffer.
    #[inline]
    pub fn extend_into(&self, bytes: &mut Vec<u8>, tag: u8) {
        bytes.push(tag); // tag
        bytes.push(2); // length
        bytes.extend_from_slice(&self.0.to_be_bytes()); // value
    }
}

impl From<&[u8]> for MaxMessage {
    fn from(value: &[u8]) -> Self {
        let bytes: [u8; 2] = value.try_into().unwrap();
        Self(u16::from_be_bytes(bytes))
    }
}