decopule interface

This commit is contained in:
vms
2020-12-21 13:35:37 +03:00
parent 44d559cca1
commit 4b5da87cca
37 changed files with 521 additions and 522 deletions

View File

@@ -0,0 +1,9 @@
[package]
name = "fluence-it-types"
version = "0.1.0"
description = "Definitions of IValue and IType"
authors = ["Fluence Labs"]
edition = "2018"
[dependencies]
serde = { version = "1.0.118", features = ["derive", "rc"]}

View File

@@ -0,0 +1,11 @@
mod types;
mod values;
pub mod vec1;
// types
pub use types::InterfaceType as IType;
pub use types::RecordFieldType as IRecordFieldType;
pub use types::RecordType as IRecordType;
// values
pub use values::InterfaceValue as IValue;

View File

@@ -0,0 +1,94 @@
//! This module defines the WIT types.
use crate::vec1::Vec1;
use serde::Deserialize;
use serde::Serialize;
/// Represents the types supported by WIT.
#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
pub enum InterfaceType {
/// A 8-bits signed integer.
S8,
/// A 16-bits signed integer.
S16,
/// A 32-bits signed integer.
S32,
/// A 64-bits signed integer.
S64,
/// A 8-bits unsigned integer.
U8,
/// A 16-bits unsigned integer.
U16,
/// A 32-bits unsigned integer.
U32,
/// A 64-bits unsigned integer.
U64,
/// A 32-bits float.
F32,
/// A 64-bits float.
F64,
/// A string.
String,
/// An array of values of the same type.
Array(Box<InterfaceType>),
/// An `any` reference.
Anyref,
/// A 32-bits integer (as defined in WebAssembly core).
I32,
/// A 64-bits integer (as defined in WebAssembly core).
I64,
/// A record contains record index from interfaces AST.
Record(u64),
}
/// Represents a record field type.
#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct RecordFieldType {
// TODO: make name optional to support structures with anonymous fields in Rust
/// A field name.
pub name: String,
/// A field type.
pub ty: InterfaceType,
}
/// Represents a record type.
#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct RecordType {
/// A record name.
pub name: String,
/// Types and names representing the fields.
/// A record must have at least one field, hence the
/// [`Vec1`][crate::vec1::Vec1].
pub fields: Vec1<RecordFieldType>,
}
impl Default for RecordType {
fn default() -> Self {
Self {
name: String::new(),
fields: Vec1::new(vec![RecordFieldType {
name: String::new(),
ty: InterfaceType::S8,
}])
.unwrap(),
}
}
}

View File

@@ -0,0 +1,59 @@
//! Defines WIT values and associated operations.
use crate::vec1::Vec1;
/// A WIT value.
#[derive(Debug, Clone, PartialEq)]
pub enum InterfaceValue {
/// A 8-bits signed integer.
S8(i8),
/// A 16-bits signed integer.
S16(i16),
/// A 32-bits signed integer.
S32(i32),
/// A 64-bits signed integer.
S64(i64),
/// A 8-bits unsigned integer.
U8(u8),
/// A 16-bits unsigned integer.
U16(u16),
/// A 32-bits unsigned integer.
U32(u32),
/// A 64-bits unsigned integer.
U64(u64),
/// A 32-bits float.
F32(f32),
/// A 64-bits float.
F64(f64),
/// A string.
String(String),
/// A byte array.
Array(Vec<InterfaceValue>),
//Anyref(?),
/// A 32-bits integer (as defined in WebAssembly core).
I32(i32),
/// A 64-bits integer (as defiend in WebAssembly core).
I64(i64),
/// A record.
Record(Vec1<InterfaceValue>),
}
impl Default for InterfaceValue {
fn default() -> Self {
Self::I32(0)
}
}

View File

@@ -0,0 +1,68 @@
//! `Vec1<T>` represents a non-empty `Vec<T>`.
use serde::{Deserialize, Serialize};
use std::{
error,
fmt::{self, Debug},
ops,
};
/// `Vec1<T>` represents a non-empty `Vec<T>`. It derefs to `Vec<T>`
/// directly.
#[derive(Clone, PartialEq, Eq, Serialize, Hash, Deserialize, Default)]
pub struct Vec1<T>(Vec<T>)
where
T: Debug;
/// Represents the only error that can be emitted by `Vec1`, i.e. when
/// the number of items is zero.
#[derive(Debug)]
pub struct EmptyVec;
impl error::Error for EmptyVec {}
impl fmt::Display for EmptyVec {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "Vec1 must as least contain one item, zero given")
}
}
impl<T> Vec1<T>
where
T: Debug,
{
/// Creates a new non-empty vector, based on an inner `Vec<T>`. If
/// the inner vector is empty, a `EmptyVec` error is returned.
pub fn new(items: Vec<T>) -> Result<Self, EmptyVec> {
if items.is_empty() {
Err(EmptyVec)
} else {
Ok(Self(items))
}
}
/// Converts this Vec1 into Vec
pub fn into_vec(self) -> Vec<T> {
self.0
}
}
impl<T> fmt::Debug for Vec1<T>
where
T: Debug,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{:?}", self.0)
}
}
impl<T> ops::Deref for Vec1<T>
where
T: Debug,
{
type Target = Vec<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}