decouple ToBytes into a separate crate

This commit is contained in:
vms
2020-12-29 12:32:17 +03:00
parent b8ca780473
commit ffbe1a282a
9 changed files with 201 additions and 196 deletions

View File

@ -1,12 +1,14 @@
[package]
name = "fluence-it-types"
version = "0.1.0"
version = "0.1.1"
description = "Definitions of IValue and IType"
authors = ["Fluence Labs"]
edition = "2018"
license = "Apache-2.0"
[dependencies]
it-to-bytes = { path = "../to-bytes/", version = "0.1.0" }
serde = { version = "1.0.118", features = ["derive", "rc"]}
nom = { version = "5.1", optional = true }

View File

@ -3,10 +3,69 @@ use crate::IRecordFieldType;
use crate::IRecordType;
use crate::IType;
use it_to_bytes::ToBytes;
use wast::parser::Parse;
use wast::parser::Parser;
use wast::Error as ParseError;
use std::io;
use std::io::Write;
/// Encode an `IType` into bytes.
impl<W> ToBytes<W> for IType
where
W: Write,
{
fn to_bytes(&self, writer: &mut W) -> io::Result<()> {
match self {
IType::S8 => 0x00_u8.to_bytes(writer),
IType::S16 => 0x01_u8.to_bytes(writer),
IType::S32 => 0x02_u8.to_bytes(writer),
IType::S64 => 0x03_u8.to_bytes(writer),
IType::U8 => 0x04_u8.to_bytes(writer),
IType::U16 => 0x05_u8.to_bytes(writer),
IType::U32 => 0x06_u8.to_bytes(writer),
IType::U64 => 0x07_u8.to_bytes(writer),
IType::F32 => 0x08_u8.to_bytes(writer),
IType::F64 => 0x09_u8.to_bytes(writer),
IType::String => 0x0a_u8.to_bytes(writer),
IType::Array(ty) => {
0x36_u8.to_bytes(writer)?;
ty.to_bytes(writer)
}
IType::Anyref => 0x0b_u8.to_bytes(writer),
IType::I32 => 0x0c_u8.to_bytes(writer),
IType::I64 => 0x0d_u8.to_bytes(writer),
IType::Record(record_id) => {
0x0e_u8.to_bytes(writer)?;
record_id.to_bytes(writer)
}
}
}
}
/// Encode a `RecordType` into bytes.
impl<W> ToBytes<W> for IRecordFieldType
where
W: Write,
{
fn to_bytes(&self, writer: &mut W) -> io::Result<()> {
self.name.as_str().to_bytes(writer)?;
self.ty.to_bytes(writer)
}
}
/// Encode a `RecordType` into bytes.
impl<W> ToBytes<W> for IRecordType
where
W: Write,
{
fn to_bytes(&self, writer: &mut W) -> io::Result<()> {
self.name.as_str().to_bytes(writer)?;
self.fields.to_bytes(writer)
}
}
mod keyword {
pub use wast::{
custom_keyword,