mazzaroth_rs/abi/
decoder.rs1use mazzaroth_xdr::Argument;
3use xdr_rs_serialize::de::{read_json_string, XDRIn};
4use xdr_rs_serialize::error::Error;
5
6pub struct Decoder<'a> {
9 payload: &'a [u8],
10}
11
12impl<'a> Decoder<'a> {
13 pub fn new(raw: &'a [u8]) -> Self {
15 Decoder { payload: raw }
16 }
17
18 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
25pub struct InputDecoder<'a> {
28 payload: &'a [Argument],
29 position: usize,
30}
31
32impl<'a> InputDecoder<'a> {
33 pub fn new(raw: &'a [Argument]) -> Self {
35 InputDecoder {
36 payload: raw,
37 position: 0,
38 }
39 }
40
41 pub fn pop<T: XDRIn>(&mut self, typ: &'static str) -> Result<T, Error> {
43 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 pub fn position(&self) -> usize {
54 self.position
55 }
56
57 pub fn payload(&self) -> &'a [Argument] {
59 self.payload
60 }
61}