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
use base64::{decode_config_slice, encode_config_slice, Config};

const B64_CONF: Config = Config::new(base64::CharacterSet::Standard, true);

/// Errors from Loadable::load
#[derive(Debug)]
pub enum LoadError {
    /// Buffer smaller than expected
    TooSmallBuffer,
    /// Unexpected/corrupted data encountered
    InvalidData,
    /// Unimplemented
    #[cfg(debug_assertions)]
    Todo,
}

impl std::fmt::Display for LoadError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::TooSmallBuffer => write!(f, "LoadError: TooSmallBuffer"),
            Self::InvalidData => write!(f, "LoadError: InvalidData"),
            #[cfg(debug_assertions)]
            Self::Todo => write!(f, "LoadError: TODO!"),
        }
    }
}

/// Load an object from the buffer
pub trait Loadable: Sized {
    /// Read the buffer, building the object and returning the amount of bytes read.
    /// If anything is wrong with the buffer, None should be returned.
    fn load(buffer: &[u8]) -> Result<(Self, usize), LoadError>;

    /// Load data from a base64-encoded buffer
    fn load_base64(buffer: &[u8]) -> Result<(Self, usize), LoadError> {
        let mut buffer2 = [0u8; crate::socket::PACKET_BUFFER_SIZE];
        let len = decode_config_slice(buffer, B64_CONF, &mut buffer2)
            .map_err(|_| LoadError::InvalidData)?;
        Self::load(&buffer2[..len])
    }
}

/// Errors from Dumpable::dump
#[derive(Debug)]
pub enum DumpError {
    /// Buffer not big enough to dump data into
    TooSmallBuffer,
    /// Data cannot be dumped
    Unsupported,
    /// Unimplemented
    #[cfg(debug_assertions)]
    Todo,
}

impl std::fmt::Display for DumpError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::TooSmallBuffer => write!(f, "DumpError: TooSmallBuffer"),
            Self::Unsupported => write!(f, "DumpError: Unsupported"),
            #[cfg(debug_assertions)]
            Self::Todo => write!(f, "DumpError: TODO!"),
        }
    }
}

/// Dump an object into the buffer
pub trait Dumpable {
    /// Write the object to the buffer, returning the amount of bytes written.
    /// If anything is wrong, false should be returned.
    fn dump(&self, buffer: &mut [u8]) -> Result<usize, DumpError>;

    /// Dump data as base64-encoded.
    /// Useful for transmitting data as text.
    fn dump_base64(&self, buffer: &mut [u8]) -> Result<usize, DumpError> {
        let mut buffer2 = [0u8; crate::socket::PACKET_BUFFER_SIZE];
        let len = self.dump(&mut buffer2)?;
        let len = encode_config_slice(&buffer2[..len], B64_CONF, buffer);
        Ok(len)
    }
}