toe_beans/v4/message/options/
sname.rs1use crate::v4::error::Result;
2use serde::{Deserialize, Serialize};
3use std::ffi::{CStr, CString};
4
5#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
10pub struct SNameOption(CString);
11
12impl SNameOption {
13 pub fn new(name: &CStr) -> Result<Self> {
15 if name.count_bytes() + 1 >= 255 {
16 return Err("Larger than 255 octets for a single option");
17 }
18
19 Ok(Self(name.to_owned()))
20 }
21
22 pub fn to_string(self) -> CString {
24 self.0
25 }
26
27 pub fn len(&self) -> usize {
29 self.0.count_bytes() + 1
30 }
31
32 #[inline]
33 pub fn extend_into(&self, bytes: &mut Vec<u8>, tag: u8) {
35 bytes.push(tag); bytes.push(self.len() as u8); bytes.extend_from_slice(self.0.to_bytes_with_nul()); }
39}
40
41impl From<&[u8]> for SNameOption {
42 fn from(value: &[u8]) -> Self {
43 Self(CStr::from_bytes_with_nul(value).unwrap().to_owned())
44 }
45}