toe_beans/v4/message/options/
time.rs

1use serde::{Deserialize, Serialize};
2
3/// An option that represents seconds as a u32.
4#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
5pub struct TimeOption(u32);
6
7impl TimeOption {
8    /// Create a `TimeOption`.
9    pub fn new(seconds: u32) -> Self {
10        Self(seconds)
11    }
12
13    /// Used, when serializing, to add to the byte buffer.
14    #[inline]
15    pub fn extend_into(&self, bytes: &mut Vec<u8>, tag: u8) {
16        bytes.push(tag); // tag
17        bytes.push(4); // length
18        bytes.extend_from_slice(&self.0.to_be_bytes()); // value
19    }
20}
21
22impl From<&[u8]> for TimeOption {
23    fn from(value: &[u8]) -> Self {
24        let bytes: [u8; 4] = value.try_into().unwrap();
25        Self(u32::from_be_bytes(bytes))
26    }
27}