Skip to main content

wasmer_interface_types_fl/
ast.rs

1//! Represents the WIT language as a tree. This is the central
2//! representation of the language.
3
4use crate::{interpreter::Instruction, IRecordType, IType};
5
6use serde::Deserialize;
7use serde::Serialize;
8use std::str;
9use std::sync::Arc;
10
11/// Represents the kind of type.
12#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
13pub enum TypeKind {
14    /// A function type.
15    Function,
16
17    /// A record type.
18    Record,
19}
20
21/// Represents the function argument type.
22#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
23pub struct FunctionArg {
24    /// A function argument name.
25    pub name: String,
26
27    /// A function argument type.
28    pub ty: IType,
29}
30
31/// Represents a type.
32#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
33pub enum Type {
34    /// A function type, like:
35    ///
36    /// ```wasm,ignore
37    /// (@interface type (func (param i32 i32) (result string)))
38    /// ```
39    Function {
40        /// Types for the parameters (`(param (name i32))`).
41        arguments: Arc<Vec<FunctionArg>>,
42
43        /// Types for the results (`(result …)`).
44        output_types: Arc<Vec<IType>>,
45    },
46
47    /// A record type, like:
48    ///
49    /// ```wasm,ignore
50    /// (@interface type (record string i32))
51    /// ```
52    Record(Arc<IRecordType>),
53}
54
55/// Represents an imported function.
56#[derive(PartialEq, Eq, Debug, Default, Clone, Hash)]
57pub struct Import<'input> {
58    /// The function namespace.
59    pub namespace: &'input str,
60
61    /// The function name.
62    pub name: &'input str,
63
64    /// The type signature.
65    pub function_type: u32,
66}
67
68/// Represents an exported function signature.
69#[derive(PartialEq, Eq, Debug, Default, Clone, Hash)]
70pub struct Export<'input> {
71    /// The export name.
72    pub name: &'input str,
73
74    /// The WIT function type being exported.
75    pub function_type: u32,
76}
77
78/// Represents an adapter.
79#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
80pub struct Adapter {
81    /// The adapter function type.
82    pub function_type: u32,
83
84    /// The instructions.
85    pub instructions: Vec<Instruction>,
86}
87
88/// Represents an implementation.
89#[derive(PartialEq, Eq, Debug, Default, Clone, Hash, Serialize, Deserialize)]
90pub struct Implementation {
91    /// The core function type.
92    pub core_function_type: u32,
93
94    /// The adapter function type.
95    pub adapter_function_type: u32,
96}
97
98/// Represents the kind of interface.
99#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
100pub enum InterfaceKind {
101    /// A version.
102    Version,
103
104    /// A type.
105    Type,
106
107    /// An imported function.
108    Import,
109
110    /// An adapter.
111    Adapter,
112
113    /// An exported function.
114    Export,
115
116    /// An implementation.
117    Implementation,
118}
119
120/// Represents a set of interfaces, i.e. it entirely describes a WIT
121/// definition.
122#[derive(PartialEq, Eq, Debug, Clone, Hash)]
123pub struct Interfaces<'input> {
124    /// Version of IT.
125    pub version: semver::Version,
126
127    /// All the types.
128    pub types: Vec<Type>,
129
130    /// All the imported functions.
131    pub imports: Vec<Import<'input>>,
132
133    /// All the adapters.
134    pub adapters: Vec<Adapter>,
135
136    /// All the exported functions.
137    pub exports: Vec<Export<'input>>,
138
139    /// All the implementations.
140    pub implementations: Vec<Implementation>,
141}
142
143impl Interfaces<'_> {
144    /// Creates a new Interfaces where version comes from this package version.
145    pub fn new() -> Self {
146        use std::str::FromStr;
147
148        // it's safe because otherwise it won't compile
149        let version = semver::Version::from_str(env!("CARGO_PKG_VERSION")).unwrap();
150
151        Self {
152            version,
153            types: Vec::new(),
154            imports: Vec::new(),
155            adapters: Vec::new(),
156            exports: Vec::new(),
157            implementations: Vec::new(),
158        }
159    }
160
161    /// Creates a new Interfaces from the provided version.
162    pub fn from_version(version: semver::Version) -> Self {
163        Self {
164            version,
165            types: Vec::new(),
166            imports: Vec::new(),
167            adapters: Vec::new(),
168            exports: Vec::new(),
169            implementations: Vec::new(),
170        }
171    }
172}
173
174impl Default for Interfaces<'_> {
175    fn default() -> Self {
176        Self::new()
177    }
178}