diameter_interface/modeling/avp/
utf8_string.rs

1use crate::errors::DiameterResult;
2use crate::modeling::avp::avp::AvpValue;
3use crate::modeling::avp::AvpData;
4use std::io::{Read, Write};
5
6pub type UTF8String = AvpData<String>;
7pub type Identity = UTF8String;
8
9impl UTF8String {
10    pub fn from_str(value: &'static str) -> Self {
11        Self(value.to_string())
12    }
13}
14
15impl UTF8String {
16    pub(super) fn encode_to<W: Write>(&self, writer: &mut W) -> DiameterResult<()> {
17        writer.write(self.0.as_bytes())?;
18        Ok(())
19    }
20
21    pub(super) fn decode_from<R: Read>(
22        reader: &mut R,
23        length: usize,
24    ) -> DiameterResult<AvpData<String>> {
25        let mut buffer = vec![0u8; length];
26        reader.read_exact(&mut buffer)?;
27        let string = String::from_utf8(buffer).unwrap();
28        Ok(UTF8String::new(string))
29    }
30
31    pub(super) fn len(&self) -> u32 {
32        self.0.len() as u32
33    }
34}
35
36impl Into<AvpValue> for UTF8String {
37    fn into(self) -> AvpValue {
38        AvpValue::UTF8String(self)
39    }
40}