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
//! Encodes XDR objects into a byte slice.

use xdr_rs_serialize::ser::XDROut;

/// Encoder for returning a number of arguments.
/// To push a value to the encoder it must implement the Serialize trait for
/// encoding.
pub struct Encoder {
    values: Vec<u8>,
}

impl Default for Encoder {
    fn default() -> Self {
        Encoder { values: Vec::new() }
    }
}

impl Encoder {
    /// Consume `val` to the Encoder
    pub fn push<T: XDROut>(&mut self, val: T, typ: &'static str) {
        let mut val_bytes: Vec<u8> = Vec::new();
        val.write_json(&mut val_bytes).unwrap();

        // Append bytes after the length
        match typ {
            "String" | "u64" | "i64" => self
                .values_mut()
                .extend_from_slice(&val_bytes[1..val_bytes.len() - 1]),
            _ => self.values_mut().extend_from_slice(&val_bytes),
        };
    }

    /// Mutable reference to the Encoder vector
    pub fn values_mut(&mut self) -> &mut Vec<u8> {
        &mut self.values
    }

    /// return the vector of values
    pub fn values(self) -> Vec<u8> {
        self.values
    }
}