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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::{
    io::{Read, Write},
    Decode, Encode, Result,
};

/// Encoding/decoding configuration
#[derive(Debug, Clone)]
pub struct Config {
    /// Denotes endianness of encoded bytes
    pub endianness: Endianness,
}

impl Config {
    #[inline]
    /// Returns default configuration
    pub const fn new_default() -> Self {
        Self {
            endianness: Endianness::new_default(),
        }
    }

    /// Encodes a value in a `Vec`
    pub async fn encode<E: Encode + ?Sized>(&self, value: &E) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(value.size());
        // This will never fail because `encode_to()` returns `Err` only then there is an IO error which cannot happen when
        // writing to a `Vec`
        let _ = value.encode_to(self, &mut bytes).await.expect(
            "Failed to encode value. Log an issue on nimble's GitHub repository with backtrace.",
        );
        bytes
    }

    #[inline]
    /// Writes encoded byte array to writer and returns the number of bytes written
    pub async fn encode_to<E: Encode + ?Sized, W: Write + Unpin + Send>(
        &self,
        value: &E,
        writer: W,
    ) -> Result<usize> {
        value.encode_to(self, writer).await
    }

    #[inline]
    /// Decodes a value from bytes
    pub async fn decode<D: Decode, T: AsRef<[u8]>>(&self, bytes: T) -> Result<D> {
        self.decode_from(&mut bytes.as_ref()).await
    }

    #[inline]
    /// Decodes values from reader
    pub async fn decode_from<D: Decode, R: Read + Unpin + Send>(&self, reader: R) -> Result<D> {
        D::decode_from(self, reader).await
    }
}

impl Default for Config {
    #[inline]
    fn default() -> Self {
        Self::new_default()
    }
}

/// Endianness of encoded bytes
#[derive(Debug, Clone, Copy)]
pub enum Endianness {
    /// Little endian order
    LittleEndian,
    /// Big endian order
    BigEndian,
}

impl Endianness {
    #[inline]
    /// Returns default endianness
    pub const fn new_default() -> Self {
        Self::LittleEndian
    }
}

impl Default for Endianness {
    #[inline]
    fn default() -> Self {
        Self::new_default()
    }
}