toe_beans/v4/message/options/
max_message.rs

1use crate::v4::error::Result;
2use serde::{Deserialize, Serialize};
3
4/// This option specifies the maximum length DHCP message that it is
5/// willing to accept.
6///
7/// A client may use this option in DHCPDISCOVER or DHCPREQUEST messages,
8/// but should not use the option in DHCPDECLINE messages.
9#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
10pub struct MaxMessage(u16);
11
12impl MaxMessage {
13    /// Create a `MaxMessage`.
14    pub fn new(size: u16) -> Result<Self> {
15        if size < 576 {
16            return Err("Maximum message size must be at least 576");
17        }
18
19        Ok(Self(size))
20    }
21
22    /// Used, when serializing, to add to the byte buffer.
23    #[inline]
24    pub fn extend_into(&self, bytes: &mut Vec<u8>, tag: u8) {
25        bytes.push(tag); // tag
26        bytes.push(2); // length
27        bytes.extend_from_slice(&self.0.to_be_bytes()); // value
28    }
29}
30
31impl From<&[u8]> for MaxMessage {
32    fn from(value: &[u8]) -> Self {
33        let bytes: [u8; 2] = value.try_into().unwrap();
34        Self(u16::from_be_bytes(bytes))
35    }
36}