Skip to content

Commit

Permalink
WIP on encoding/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Oct 7, 2024
1 parent bc2087a commit 9fa89f1
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 6 deletions.
3 changes: 3 additions & 0 deletions rust/codecs/proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ publish = false

[dependencies]
ixc_schema = { path = "../../schema" }
ixc_message_api = { path = "../../message_api" }
simple_time = { path = "../../util/simple_time", version = "0.0.1" }
integer-encoding = "4.0.2"

[lints]
workspace = true
100 changes: 99 additions & 1 deletion rust/codecs/proto/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
struct Decoder {}
use ixc_schema::decoder::DecodeError;
use ixc_schema::mem::MemoryManager;
use ixc_schema::structs::{StructDecodeVisitor, StructType};

struct Decoder<'a> {
data: &'a [u8],
}

impl <'a> ixc_schema::decoder::Decoder<'a> for Decoder<'a> {
fn decode_bool(&mut self) -> Result<bool, DecodeError> {
todo!()
}

fn decode_u8(&mut self) -> Result<u8, DecodeError> {
todo!()
}

fn decode_u16(&mut self) -> Result<u16, DecodeError> {
todo!()
}

fn decode_u32(&mut self) -> Result<u32, DecodeError> {
todo!()
}

fn decode_u64(&mut self) -> Result<u64, DecodeError> {
todo!()
}

fn decode_u128(&mut self) -> Result<u128, DecodeError> {
todo!()
}

fn decode_i8(&mut self) -> Result<i8, DecodeError> {
todo!()
}

fn decode_i16(&mut self) -> Result<i16, DecodeError> {
todo!()
}

fn decode_i32(&mut self) -> Result<i32, DecodeError> {
todo!()
}

fn decode_i64(&mut self) -> Result<i64, DecodeError> {
todo!()
}

fn decode_i128(&mut self) -> Result<i128, DecodeError> {
todo!()
}

fn decode_borrowed_str(&mut self) -> Result<&'a str, DecodeError> {
todo!()
}

fn decode_owned_str(&mut self) -> Result<String, DecodeError> {
todo!()
}

fn decode_borrowed_bytes(&mut self) -> Result<&'a [u8], DecodeError> {
todo!()
}

fn decode_owned_bytes(&mut self) -> Result<Vec<u8>, DecodeError> {
todo!()
}

fn decode_struct(&mut self, visitor: &mut dyn StructDecodeVisitor<'a>, struct_type: &StructType) -> Result<(), DecodeError> {
todo!()
}

fn decode_list(&mut self, visitor: &mut dyn ixc_schema::list::ListDecodeVisitor<'a>) -> Result<(), DecodeError> {
// if it's a packed tag
// decode size
// for each list item
// decode element
// else
// decode next element
todo!()
}

fn decode_account_id(&mut self) -> Result<ixc_message_api::AccountID, DecodeError> {
todo!()
}

fn decode_time(&mut self) -> Result<simple_time::Time, DecodeError> {
todo!()
}

fn decode_duration(&mut self) -> Result<simple_time::Duration, DecodeError> {
todo!()
}

fn mem_manager(&self) -> &'a MemoryManager {
todo!()
}
}
97 changes: 96 additions & 1 deletion rust/codecs/proto/src/encoder.rs
Original file line number Diff line number Diff line change
@@ -1 +1,96 @@
struct Encoder {}
use ixc_schema::encoder::EncodeError;
use ixc_schema::structs::{StructEncodeVisitor, StructType};
use integer_encoding::VarInt;
use ixc_schema::buffer::ReverseSliceWriter;

struct Encoder<'a> {
writer: ReverseSliceWriter<'a>,
}

impl <'a> ixc_schema::encoder::Encoder for Encoder<'a> {
fn encode_bool(&mut self, x: bool) -> Result<(), EncodeError> {
todo!()
}

fn encode_u8(&mut self, x: u8) -> Result<(), EncodeError> {
todo!()
}

fn encode_u16(&mut self, x: u16) -> Result<(), EncodeError> {
todo!()
}

fn encode_u32(&mut self, x: u32) -> Result<(), EncodeError> {
todo!()
}

fn encode_u64(&mut self, x: u64) -> Result<(), EncodeError> {
// fixed size buffer
// <u64 as VarInt>::encode_var(x, &mut self.writer);
todo!()
}

fn encode_u128(&mut self, x: u128) -> Result<(), EncodeError> {
todo!()
}

fn encode_i8(&mut self, x: i8) -> Result<(), EncodeError> {
todo!()
}

fn encode_i16(&mut self, x: i16) -> Result<(), EncodeError> {
todo!()
}

fn encode_i32(&mut self, x: i32) -> Result<(), EncodeError> {
todo!()
}

fn encode_i64(&mut self, x: i64) -> Result<(), EncodeError> {
todo!()
}

fn encode_i128(&mut self, x: i128) -> Result<(), EncodeError> {
todo!()
}

fn encode_str(&mut self, x: &str) -> Result<(), EncodeError> {
todo!()
}

fn encode_bytes(&mut self, x: &[u8]) -> Result<(), EncodeError> {
todo!()
}

fn encode_list(&mut self, visitor: &dyn ixc_schema::list::ListEncodeVisitor) -> Result<(), EncodeError> {
todo!()
// if it's a packed list type
// for each list item in reverse order
// encode element
// encode size
// encode tag
// else
// for each list item in reverse order
// encode element
// encode tag
}

fn encode_struct(&mut self, visitor: &dyn StructEncodeVisitor, struct_type: &StructType) -> Result<(), EncodeError> {
// for each field in reverse order
// encode field
// encode tag
todo!()
}

fn encode_account_id(&mut self, x: ixc_message_api::AccountID) -> Result<(), EncodeError> {
todo!()
}

fn encode_time(&mut self, x: simple_time::Time) -> Result<(), EncodeError> {
todo!()
}

fn encode_duration(&mut self, x: simple_time::Duration) -> Result<(), EncodeError> {
todo!()
}
}
6 changes: 3 additions & 3 deletions rust/codecs/proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod encoder;
mod decoder;

use ixc_schema::buffer::WriterFactory;
use ixc_schema::codec::Codec;
use ixc_schema::codec::{Codec, ValueDecodeVisitor, ValueEncodeVisitor};
use ixc_schema::decoder::DecodeError;
use ixc_schema::encoder::EncodeError;
use ixc_schema::mem::MemoryManager;
Expand All @@ -11,11 +11,11 @@ use ixc_schema::value::SchemaValue;
pub struct ProtobufCodec;

impl Codec for ProtobufCodec {
fn encode_value<'a, V: SchemaValue<'a>, F: WriterFactory>(value: &V, writer_factory: &F) -> Result<F::Output, EncodeError> {
fn encode_value<'a>(&self, value: &dyn ValueEncodeVisitor, writer_factory: &'a dyn WriterFactory) -> Result<&'a [u8], EncodeError> {
todo!()
}

fn decode_value<'b, 'a: 'b, V: SchemaValue<'a>>(input: &'a [u8], memory_manager: &'b MemoryManager<'a, 'a>) -> Result<V, DecodeError> {
fn decode_value<'a>(&self, input: &'a [u8], memory_manager: &'a MemoryManager, visitor: &mut dyn ValueDecodeVisitor<'a>) -> Result<(), DecodeError> {
todo!()
}
}
2 changes: 1 addition & 1 deletion rust/schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod oneof;
pub mod state_object;
pub mod codec;
pub mod decoder;
mod list;
pub mod list;
pub mod binary;
pub mod encoder;
mod kind;
Expand Down
11 changes: 11 additions & 0 deletions rust/schema/src/list.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
//! Traits for encoding and decoding list types.
use allocator_api2::alloc::Allocator;
use allocator_api2::vec::Vec;
use crate::decoder::{DecodeError, Decoder};
use crate::encoder::{EncodeError, Encoder};
use crate::mem::MemoryManager;
use crate::value::SchemaValue;

/// A visitor for encoding list types.
pub trait ListEncodeVisitor {
/// Get the size of the list if it is known or None if it is not known.
fn size_hint(&self) -> Option<u32>;
/// Encode the list.
fn encode(&self, encoder: &mut dyn Encoder) -> Result<u32, EncodeError>;
/// Encode the list in reverse order.
fn encode_reverse(&self, encoder: &mut dyn Encoder) -> Result<u32, EncodeError>;
}

/// A visitor for decoding list types.
pub trait ListDecodeVisitor<'a> {
/// Initialize the visitor with the length of the list.
/// This method may or may not be called depending on whether the underlying
/// encoding specifies the length of the list.
fn init(&mut self, len: usize, scope: &'a MemoryManager) -> Result<(), DecodeError>;
/// Decode the next element in the list.
fn next(&mut self, decoder: &mut dyn Decoder<'a>) -> Result<(), DecodeError>;
}

/// A builder for decoding Vec's with a specified allocator.
pub struct AllocatorVecBuilder<'a, T: SchemaValue<'a>> {
pub(crate) xs: Option<Vec<T, &'a dyn Allocator>>,
}
Expand Down

0 comments on commit 9fa89f1

Please sign in to comment.