1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use super::invoke::{Identity, Invoke};
use crate::elements;
use alloc::{borrow::ToOwned, string::String};

/// Import builder
pub struct ImportBuilder<F = Identity> {
	callback: F,
	module: String,
	field: String,
	binding: elements::External,
}

impl ImportBuilder {
	/// New import builder
	pub fn new() -> Self {
		ImportBuilder::with_callback(Identity)
	}
}

impl Default for ImportBuilder {
	fn default() -> Self {
		Self::new()
	}
}

impl<F> ImportBuilder<F> {
	/// New import builder with callback (in chained context)
	pub fn with_callback(callback: F) -> Self {
		ImportBuilder {
			callback,
			module: String::new(),
			field: String::new(),
			binding: elements::External::Function(0),
		}
	}

	/// Set/override module name
	pub fn module(mut self, name: &str) -> Self {
		self.module = name.to_owned();
		self
	}

	/// Set/override field name
	pub fn field(mut self, name: &str) -> Self {
		self.field = name.to_owned();
		self
	}

	/// Set/override both module name and field name
	pub fn path(self, module: &str, field: &str) -> Self {
		self.module(module).field(field)
	}

	/// Set/override external mapping for this import
	pub fn with_external(mut self, external: elements::External) -> Self {
		self.binding = external;
		self
	}

	/// Start new external mapping builder
	pub fn external(self) -> ImportExternalBuilder<Self> {
		ImportExternalBuilder::with_callback(self)
	}
}

impl<F> ImportBuilder<F>
where
	F: Invoke<elements::ImportEntry>,
{
	/// Finalize current builder spawning the resulting struct
	pub fn build(self) -> F::Result {
		self.callback
			.invoke(elements::ImportEntry::new(self.module, self.field, self.binding))
	}
}

impl<F> Invoke<elements::External> for ImportBuilder<F> {
	type Result = Self;
	fn invoke(self, val: elements::External) -> Self {
		self.with_external(val)
	}
}

/// Import to external mapping builder
pub struct ImportExternalBuilder<F = Identity> {
	callback: F,
	binding: elements::External,
}

impl<F> ImportExternalBuilder<F>
where
	F: Invoke<elements::External>,
{
	/// New import to external mapping builder with callback (in chained context)
	pub fn with_callback(callback: F) -> Self {
		ImportExternalBuilder { callback, binding: elements::External::Function(0) }
	}

	/// Function mapping with type reference
	pub fn func(mut self, index: u32) -> F::Result {
		self.binding = elements::External::Function(index);
		self.callback.invoke(self.binding)
	}

	/// Memory mapping with specified limits
	pub fn memory(mut self, min: u32, max: Option<u32>) -> F::Result {
		self.binding = elements::External::Memory(elements::MemoryType::new(min, max));
		self.callback.invoke(self.binding)
	}

	/// Table mapping with specified limits
	pub fn table(mut self, min: u32, max: Option<u32>) -> F::Result {
		self.binding = elements::External::Table(elements::TableType::new(min, max));
		self.callback.invoke(self.binding)
	}

	/// Global mapping with speciifed type and mutability
	pub fn global(mut self, value_type: elements::ValueType, is_mut: bool) -> F::Result {
		self.binding = elements::External::Global(elements::GlobalType::new(value_type, is_mut));
		self.callback.invoke(self.binding)
	}
}

/// New builder for import entry
pub fn import() -> ImportBuilder {
	ImportBuilder::new()
}

#[cfg(test)]
mod tests {
	use super::import;

	#[test]
	fn example() {
		let entry =
			import().module("env").field("memory").external().memory(256, Some(256)).build();

		assert_eq!(entry.module(), "env");
		assert_eq!(entry.field(), "memory");
	}
}