use crate::v4::error::Result;
use serde::{Deserialize, Serialize};
use std::ffi::{CStr, CString};
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct SNameOption(CString);
impl SNameOption {
pub fn new(name: &CStr) -> Result<Self> {
if name.count_bytes() + 1 >= 255 {
return Err("Larger than 255 octets for a single option");
}
Ok(Self(name.to_owned()))
}
pub fn to_string(self) -> CString {
self.0
}
pub fn len(&self) -> usize {
self.0.count_bytes() + 1
}
#[inline]
pub fn extend_into(&self, bytes: &mut Vec<u8>, tag: u8) {
bytes.push(tag); bytes.push(self.len() as u8); bytes.extend_from_slice(self.0.to_bytes_with_nul()); }
}
impl From<&[u8]> for SNameOption {
fn from(value: &[u8]) -> Self {
Self(CStr::from_bytes_with_nul(value).unwrap().to_owned())
}
}