ssh_agent_lib/proto/message/
unparsed.rs

1//! Generic container for [`Extension`](super::Extension)-specific content
2
3use ssh_encoding::{self, Decode, Encode, Writer};
4
5/// Generic container for [`Extension`](super::Extension)-specific content.
6/// Accessing the inner `Vec<u8>` is only possible via conversion methods.
7#[derive(Debug, PartialEq, Clone)]
8pub struct Unparsed(Vec<u8>);
9
10impl Unparsed {
11    /// Decode unparsed bytes as SSH structures.
12    pub fn parse<T>(&self) -> std::result::Result<T, <T as Decode>::Error>
13    where
14        T: Decode,
15    {
16        let mut v = &self.0[..];
17        T::decode(&mut v)
18    }
19
20    /// Obtain the unparsed bytes as a `Vec<u8>`, consuming the `Unparsed`
21    pub fn into_bytes(self) -> Vec<u8> {
22        self.0
23    }
24}
25
26impl From<Vec<u8>> for Unparsed {
27    fn from(value: Vec<u8>) -> Self {
28        Self(value)
29    }
30}
31
32impl AsRef<[u8]> for Unparsed {
33    fn as_ref(&self) -> &[u8] {
34        self.0.as_ref()
35    }
36}
37
38impl Encode for Unparsed {
39    fn encoded_len(&self) -> ssh_encoding::Result<usize> {
40        Ok(self.0.len())
41    }
42
43    fn encode(&self, writer: &mut impl Writer) -> ssh_encoding::Result<()> {
44        // NOTE: Unparsed fields do not embed a length u32,
45        // as the inner Vec<u8> encoding is implementation-defined
46        // (usually an Extension)
47        writer.write(&self.0[..])?;
48
49        Ok(())
50    }
51}