openssh_mux_client/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use super::Result;
use serde::{Serialize, Serializer};
use std::convert::TryInto;

/// Serialize one `u32` as ssh_format.
pub(crate) fn serialize_u32(int: u32) -> [u8; 4] {
    int.to_be_bytes()
}

pub(crate) enum MaybeOwned<'a, T> {
    Owned(T),
    Borrowed(&'a T),
}

impl<T> MaybeOwned<'_, T> {
    pub(crate) fn as_ref(&self) -> &T {
        use MaybeOwned::*;

        match self {
            Owned(val) => val,
            Borrowed(reference) => reference,
        }
    }
}

impl<T: Serialize> Serialize for MaybeOwned<'_, T> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.as_ref().serialize(serializer)
    }
}

pub(crate) trait SliceExt {
    fn get_len_as_u32(&self) -> Result<u32>;
}

impl<T> SliceExt for [T] {
    fn get_len_as_u32(&self) -> Result<u32> {
        self.len()
            .try_into()
            .map_err(|_| ssh_format::Error::TooLong.into())
    }
}

impl SliceExt for str {
    fn get_len_as_u32(&self) -> Result<u32> {
        self.as_bytes().get_len_as_u32()
    }
}