use serde::{Deserialize, Serialize};
/// An option that represents seconds as a u32.
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct TimeOption(u32);
impl TimeOption {
/// Create a `TimeOption`.
pub fn new(seconds: u32) -> Self {
Self(seconds)
}
/// 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(4); // length
bytes.extend_from_slice(&self.0.to_be_bytes()); // value
}
}
impl From<&[u8]> for TimeOption {
fn from(value: &[u8]) -> Self {
let bytes: [u8; 4] = value.try_into().unwrap();
Self(u32::from_be_bytes(bytes))
}
}