1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! This module defines the WIT types.

use crate::vec1::Vec1;
use serde::{Deserialize, 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(),
        }
    }
}