mazzaroth_rs/abi/
decoder.rs

1//! Decodes encoded bytes into an XDR object.
2use mazzaroth_xdr::Argument;
3use xdr_rs_serialize::de::{read_json_string, XDRIn};
4use xdr_rs_serialize::error::Error;
5
6/// Decode a single payload of bytes into an XDR object.
7/// Value must implement XDRIn.
8pub struct Decoder<'a> {
9    payload: &'a [u8],
10}
11
12impl<'a> Decoder<'a> {
13    /// New decoder for known payload
14    pub fn new(raw: &'a [u8]) -> Self {
15        Decoder { payload: raw }
16    }
17
18    /// Pop next argument of known type
19    pub fn pop<T: XDRIn>(&mut self) -> Result<T, Error> {
20        let bytes = &self.payload;
21        Ok(T::read_xdr(bytes)?.0)
22    }
23}
24
25/// Decode a vector of Arguments into separate XDR object.
26/// Values must implement XDRIn.
27pub struct InputDecoder<'a> {
28    payload: &'a [Argument],
29    position: usize,
30}
31
32impl<'a> InputDecoder<'a> {
33    /// New decoder for known payload
34    pub fn new(raw: &'a [Argument]) -> Self {
35        InputDecoder {
36            payload: raw,
37            position: 0,
38        }
39    }
40
41    /// Pop next argument of known type
42    pub fn pop<T: XDRIn>(&mut self, typ: &'static str) -> Result<T, Error> {
43        // grab bytes from argument and advance 1
44        let bytes = &self.payload[self.position].t[..];
45        self.position += 1;
46        match typ {
47            "String" | "u64" | "i64" => read_json_string(format!(r#""{}""#, bytes.to_string())),
48            _ => read_json_string(bytes.to_string()),
49        }
50    }
51
52    /// Current position for the decoder
53    pub fn position(&self) -> usize {
54        self.position
55    }
56
57    /// Decoder payload
58    pub fn payload(&self) -> &'a [Argument] {
59        self.payload
60    }
61}