parity_wasm_cp/builder/
import.rs

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