parity_wasm/builder/
export.rs

1use super::invoke::{Identity, Invoke};
2use crate::elements;
3use alloc::{borrow::ToOwned, string::String};
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 Default for ExportBuilder {
20	fn default() -> Self {
21		Self::new()
22	}
23}
24
25impl<F> ExportBuilder<F> {
26	/// New export entry builder in the specified chained context
27	pub fn with_callback(callback: F) -> Self {
28		ExportBuilder { callback, field: String::new(), binding: elements::Internal::Function(0) }
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>
50where
51	F: Invoke<elements::ExportEntry>,
52{
53	/// Finalize export entry builder spawning the resulting struct
54	pub fn build(self) -> F::Result {
55		self.callback.invoke(elements::ExportEntry::new(self.field, self.binding))
56	}
57}
58
59impl<F> Invoke<elements::Internal> for ExportBuilder<F> {
60	type Result = Self;
61	fn invoke(self, val: elements::Internal) -> Self {
62		self.with_internal(val)
63	}
64}
65
66/// Internal mapping builder for export entry
67pub struct ExportInternalBuilder<F = Identity> {
68	callback: F,
69	binding: elements::Internal,
70}
71
72impl<F> ExportInternalBuilder<F>
73where
74	F: Invoke<elements::Internal>,
75{
76	/// New export entry internal mapping for the chained context
77	pub fn with_callback(callback: F) -> Self {
78		ExportInternalBuilder { callback, binding: elements::Internal::Function(0) }
79	}
80
81	/// Map to function by index
82	pub fn func(mut self, index: u32) -> F::Result {
83		self.binding = elements::Internal::Function(index);
84		self.callback.invoke(self.binding)
85	}
86
87	/// Map to memory
88	pub fn memory(mut self, index: u32) -> F::Result {
89		self.binding = elements::Internal::Memory(index);
90		self.callback.invoke(self.binding)
91	}
92
93	/// Map to table
94	pub fn table(mut self, index: u32) -> F::Result {
95		self.binding = elements::Internal::Table(index);
96		self.callback.invoke(self.binding)
97	}
98
99	/// Map to global
100	pub fn global(mut self, index: u32) -> F::Result {
101		self.binding = elements::Internal::Global(index);
102		self.callback.invoke(self.binding)
103	}
104}
105
106/// New builder for export entry
107pub fn export() -> ExportBuilder {
108	ExportBuilder::new()
109}
110
111#[cfg(test)]
112mod tests {
113	use super::export;
114
115	#[test]
116	fn example() {
117		let entry = export().field("memory").internal().memory(0).build();
118		assert_eq!(entry.field(), "memory");
119	}
120}