Crate epee_encoding

Source
Expand description

Epee Encoding

This library contains the Epee binary format found in Monero, unlike other crates this crate does not use serde.

example without derive:

use epee_encoding::{EpeeObject, EpeeObjectBuilder, read_epee_value, write_field, to_bytes, from_bytes};
use epee_encoding::io::{Read, Write};

pub struct Test {
    val: u64
}

#[derive(Default)]
pub struct __TestEpeeBuilder {
    val: Option<u64>,
}

impl EpeeObjectBuilder<Test> for __TestEpeeBuilder {
    fn add_field<R: Read>(&mut self, name: &str, r: &mut R) -> epee_encoding::error::Result<bool> {
        match name {
            "val" => {self.val = Some(read_epee_value(r)?);}
            _ => return Ok(false),
        }
        Ok(true)
    }

    fn finish(self) -> epee_encoding::error::Result<Test> {
        Ok(
            Test {
                val: self.val.ok_or_else(|| epee_encoding::error::Error::Format("Required field was not found!"))?
            }
        )
    }
}

impl EpeeObject for Test {
    type Builder = __TestEpeeBuilder;

    fn number_of_fields(&self) -> u64 {
        1
    }

    fn write_fields<W: Write>(&self, w: &mut W) -> epee_encoding::error::Result<()> {
       // write the fields
       write_field(&self.val, "val", w)
   }
}


let data = [1, 17, 1, 1, 1, 1, 2, 1, 1, 4, 3, 118, 97, 108, 5, 4, 0, 0, 0, 0, 0, 0, 0]; // the data to decode;
let val: Test = from_bytes(&data).unwrap();
let data = to_bytes(&val).unwrap();

example with derive:

use epee_encoding::{EpeeObject, from_bytes, to_bytes};

#[derive(EpeeObject)]
struct Test2 {
    val: u64
}


let data = [1, 17, 1, 1, 1, 1, 2, 1, 1, 4, 3, 118, 97, 108, 5, 4, 0, 0, 0, 0, 0, 0, 0]; // the data to decode;
let val: Test2 = from_bytes(&data).unwrap();
let data = to_bytes(&val).unwrap();

Re-exports§

pub use marker::InnerMarker;
pub use marker::Marker;
pub use error::*;

Modules§

error
io
marker

Traits§

EpeeObject
A trait for an object that can be turned into epee bytes.
EpeeObjectBuilder
A trait for an object that can build a type T from the epee format.
EpeeValue
A trait for epee values, this trait is sealed as all possible epee values are defined in the lib, to make an EpeeValue outside the lib you will need to use the trait EpeeObject.

Functions§

from_bytes
Read the object T from a byte array.
read_epee_value
Read an epee value from the stream, an epee value is the part after the key including the marker.
read_marker
Read a marker from the Read, this function should only be used for custom serialisation based on the marker otherwise just use read_epee_value.
to_bytes
Turn the object into epee bytes.
write_field
Write an epee field.

Derive Macros§

EpeeObject