swasm/builder/
export.rs

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