tetsy_wasm/builder/
export.rs

1use alloc::{borrow::ToOwned, string::String};
2use super::invoke::{Invoke, Identity};
3use crate::elements;
4
5/// Export entry builder
6pub struct ExportBuilder<F=Identity> {
7	callback: F,
8	field: String,
9	binding: elements::Internal,
10}
11
12impl ExportBuilder {
13	/// New export builder
14	pub fn new() -> Self {
15		ExportBuilder::with_callback(Identity)
16	}
17}
18
19impl<F> ExportBuilder<F> {
20
21	/// New export entry builder in the specified chained context
22	pub fn with_callback(callback: F) -> Self {
23		ExportBuilder {
24			callback: callback,
25			field: String::new(),
26			binding: elements::Internal::Function(0),
27		}
28	}
29
30	/// Set the field name of the export entry
31	pub fn field(mut self, field: &str) -> Self {
32		self.field = field.to_owned();
33		self
34	}
35
36	/// Specify the internal module mapping for this entry
37	pub fn with_internal(mut self, external: elements::Internal) -> Self {
38		self.binding = external;
39		self
40	}
41
42	/// Start the internal builder for this export entry
43	pub fn internal(self) -> ExportInternalBuilder<Self> {
44		ExportInternalBuilder::with_callback(self)
45	}
46}
47
48impl<F> ExportBuilder<F> where F: Invoke<elements::ExportEntry> {
49	/// Finalize export entry builder spawning the resulting struct
50	pub fn build(self) -> F::Result {
51		self.callback.invoke(elements::ExportEntry::new(self.field, self.binding))
52	}
53}
54
55impl<F> Invoke<elements::Internal> for ExportBuilder<F> {
56	type Result = Self;
57	fn invoke(self, val: elements::Internal) -> Self {
58		self.with_internal(val)
59	}
60}
61
62/// Internal mapping builder for export entry
63pub struct ExportInternalBuilder<F=Identity> {
64	callback: F,
65	binding: elements::Internal,
66}
67
68impl<F> ExportInternalBuilder<F> where F: Invoke<elements::Internal> {
69	/// New export entry internal mapping for the chained context
70	pub fn with_callback(callback: F) -> Self {
71		ExportInternalBuilder{
72			callback: callback,
73			binding: elements::Internal::Function(0),
74		}
75	}
76
77	/// Map to function by index
78	pub fn func(mut self, index: u32) -> F::Result {
79		self.binding = elements::Internal::Function(index);
80		self.callback.invoke(self.binding)
81	}
82
83	/// Map to memory
84	pub fn memory(mut self, index: u32) -> F::Result {
85		self.binding = elements::Internal::Memory(index);
86		self.callback.invoke(self.binding)
87	}
88
89	/// Map to table
90	pub fn table(mut self, index: u32) -> F::Result {
91		self.binding = elements::Internal::Table(index);
92		self.callback.invoke(self.binding)
93	}
94
95	/// Map to global
96	pub fn global(mut self, index: u32) -> F::Result {
97		self.binding = elements::Internal::Global(index);
98		self.callback.invoke(self.binding)
99	}
100}
101
102/// New builder for export entry
103pub fn export() -> ExportBuilder {
104	ExportBuilder::new()
105}
106
107#[cfg(test)]
108mod tests {
109	use super::export;
110
111	#[test]
112	fn example() {
113		let entry = export().field("memory").internal().memory(0).build();
114		assert_eq!(entry.field(), "memory");
115	}
116}