interface-types/src/types.rs

93 lines
1.9 KiB
Rust
Raw Normal View History

//! This module defines the WIT types.
use crate::vec1::Vec1;
2020-07-25 11:01:41 +03:00
use serde::{Deserialize, Serialize};
/// Represents the types supported by WIT.
2020-09-13 23:29:09 +03:00
#[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,
2020-09-20 21:53:03 +03:00
/// An array of values of the same type.
Array(Box<InterfaceType>),
2020-07-09 02:46:54 +03:00
/// An `any` reference.
Anyref,
/// A 32-bits integer (as defined in WebAssembly core).
I32,
2020-09-20 21:53:03 +03:00
/// A 64-bits integer (as defined in WebAssembly core).
I64,
2020-09-15 19:58:35 +03:00
/// A record contains record index from interfaces AST.
Record(u64),
2020-08-13 21:02:23 +03:00
}
/// Represents a record field type.
2020-09-13 23:29:09 +03:00
#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
2020-08-13 21:02:23 +03:00
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.
2020-09-13 23:29:09 +03:00
#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
pub struct RecordType {
2020-08-13 21:02:23 +03:00
/// 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].
2020-08-13 21:02:23 +03:00
pub fields: Vec1<RecordFieldType>,
}
2020-09-15 19:58:35 +03:00
impl Default for RecordType {
fn default() -> Self {
Self {
name: String::new(),
fields: Vec1::new(vec![RecordFieldType {
name: String::new(),
ty: InterfaceType::S8,
}])
.unwrap(),
}
}
}