tetsy_wasm/builder/
import.rs

1use alloc::{borrow::ToOwned, string::String};
2use super::invoke::{Identity, Invoke};
3use crate::elements;
4
5/// Import builder
6pub struct ImportBuilder<F=Identity> {
7	callback: F,
8	module: String,
9	field: String,
10	binding: elements::External,
11}
12
13impl ImportBuilder {
14	/// New import builder
15	pub fn new() -> Self {
16		ImportBuilder::with_callback(Identity)
17	}
18}
19
20impl<F> ImportBuilder<F> {
21	/// New import builder with callback (in chained context)
22	pub fn with_callback(callback: F) -> Self {
23		ImportBuilder {
24			callback: callback,
25			module: String::new(),
26			field: String::new(),
27			binding: elements::External::Function(0),
28		}
29	}
30
31	/// Set/override module name
32	pub fn module(mut self, name: &str) -> Self {
33		self.module = name.to_owned();
34		self
35	}
36
37	/// Set/override field name
38	pub fn field(mut self, name: &str) -> Self {
39		self.field = name.to_owned();
40		self
41	}
42
43	/// Set/override both module name and field name
44	pub fn path(self, module: &str, field: &str) -> Self {
45		self.module(module).field(field)
46	}
47
48	/// Set/override external mapping for this import
49	pub fn with_external(mut self, external: elements::External) -> Self {
50		self.binding = external;
51		self
52	}
53
54	/// Start new external mapping builder
55	pub fn external(self) -> ImportExternalBuilder<Self> {
56		ImportExternalBuilder::with_callback(self)
57	}
58}
59
60impl<F> ImportBuilder<F> where F: Invoke<elements::ImportEntry> {
61	/// Finalize current builder spawning the resulting struct
62	pub fn build(self) -> F::Result {
63		self.callback.invoke(elements::ImportEntry::new(self.module, self.field, self.binding))
64	}
65}
66
67impl<F> Invoke<elements::External> for ImportBuilder<F> {
68	type Result = Self;
69	fn invoke(self, val: elements::External) -> Self {
70		self.with_external(val)
71	}
72}
73
74/// Import to external mapping builder
75pub struct ImportExternalBuilder<F=Identity> {
76	callback: F,
77	binding: elements::External,
78}
79
80impl<F> ImportExternalBuilder<F> where F: Invoke<elements::External> {
81	/// New import to external mapping builder with callback (in chained context)
82	pub fn with_callback(callback: F) -> Self {
83		ImportExternalBuilder{
84			callback: callback,
85			binding: elements::External::Function(0),
86		}
87	}
88
89	/// Function mapping with type reference
90	pub fn func(mut self, index: u32) -> F::Result {
91		self.binding = elements::External::Function(index);
92		self.callback.invoke(self.binding)
93	}
94
95	/// Memory mapping with specified limits
96	pub fn memory(mut self, min: u32, max: Option<u32>) -> F::Result {
97		self.binding = elements::External::Memory(elements::MemoryType::new(min, max));
98		self.callback.invoke(self.binding)
99	}
100
101	/// Table mapping with specified limits
102	pub fn table(mut self, min: u32, max: Option<u32>) -> F::Result {
103		self.binding = elements::External::Table(elements::TableType::new(min, max));
104		self.callback.invoke(self.binding)
105	}
106
107	/// Global mapping with speciifed type and mutability
108	pub fn global(mut self, value_type: elements::ValueType, is_mut: bool) -> F::Result {
109		self.binding = elements::External::Global(elements::GlobalType::new(value_type, is_mut));
110		self.callback.invoke(self.binding)
111	}
112}
113
114/// New builder for import entry
115pub fn import() -> ImportBuilder {
116	ImportBuilder::new()
117}
118
119#[cfg(test)]
120mod tests {
121	use super::import;
122
123	#[test]
124	fn example() {
125		let entry = import().module("env").field("memory").external().memory(256, Some(256)).build();
126
127		assert_eq!(entry.module(), "env");
128		assert_eq!(entry.field(), "memory");
129	}
130}