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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use core::convert::TryFrom;
use minicbor::{Decode, Encode};
use oc_wasm_futures::invoke::Buffer;
use oc_wasm_safe::component::Invoker;

/// An error returned when converting an invalid value to an enumeration.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TryFromIntError(pub(crate) ());

impl core::fmt::Display for TryFromIntError {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
		"invalid value".fmt(f)
	}
}

#[cfg(feature = "std")]
impl std::error::Error for TryFromIntError {}

/// A rectangular dimension
#[derive(Clone, Copy, Debug, Decode, Encode, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cbor(array)]
pub struct Dimension {
	/// The width of the rectangle.
	#[n(0)]
	pub width: u32,

	/// The height of the rectangle.
	#[n(1)]
	pub height: u32,
}

/// A nonnegative position coordinate.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Point {
	/// The X coordinate.
	pub x: u32,

	/// The Y coordinate.
	pub y: u32,
}

/// A 2D integer vector.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Vector2 {
	/// The vector’s X component.
	pub x: i32,

	/// The vector’s Y component.
	pub y: i32,
}

/// The number of sides on a block.
pub const BLOCK_SIDES: usize = 6;

/// A trait implemented by both absolute and relative block side enumerations.
pub trait Side: Into<u8> + Into<usize> {}

/// An absolute side of a block.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum AbsoluteSide {
	Down,
	Up,
	North,
	South,
	West,
	East,
}

impl From<AbsoluteSide> for u8 {
	fn from(x: AbsoluteSide) -> Self {
		match x {
			AbsoluteSide::Down => 0,
			AbsoluteSide::Up => 1,
			AbsoluteSide::North => 2,
			AbsoluteSide::South => 3,
			AbsoluteSide::West => 4,
			AbsoluteSide::East => 5,
		}
	}
}

impl From<AbsoluteSide> for usize {
	fn from(x: AbsoluteSide) -> Self {
		u8::from(x) as usize
	}
}

impl TryFrom<u8> for AbsoluteSide {
	type Error = TryFromIntError;

	fn try_from(x: u8) -> Result<Self, Self::Error> {
		match x {
			0 => Ok(AbsoluteSide::Down),
			1 => Ok(AbsoluteSide::Up),
			2 => Ok(AbsoluteSide::North),
			3 => Ok(AbsoluteSide::South),
			4 => Ok(AbsoluteSide::West),
			5 => Ok(AbsoluteSide::East),
			_ => Err(TryFromIntError(())),
		}
	}
}

impl Side for AbsoluteSide {}

/// A relative side of a block.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum RelativeSide {
	Bottom,
	Top,
	Back,
	Front,
	Right,
	Left,
}

impl From<RelativeSide> for u8 {
	fn from(x: RelativeSide) -> Self {
		match x {
			RelativeSide::Bottom => 0,
			RelativeSide::Top => 1,
			RelativeSide::Back => 2,
			RelativeSide::Front => 3,
			RelativeSide::Right => 4,
			RelativeSide::Left => 5,
		}
	}
}

impl From<RelativeSide> for usize {
	fn from(x: RelativeSide) -> Self {
		u8::from(x) as usize
	}
}

impl TryFrom<u8> for RelativeSide {
	type Error = TryFromIntError;

	fn try_from(x: u8) -> Result<Self, Self::Error> {
		match x {
			0 => Ok(RelativeSide::Bottom),
			1 => Ok(RelativeSide::Top),
			2 => Ok(RelativeSide::Back),
			3 => Ok(RelativeSide::Front),
			4 => Ok(RelativeSide::Right),
			5 => Ok(RelativeSide::Left),
			_ => Err(TryFromIntError(())),
		}
	}
}

impl Side for RelativeSide {}

/// The number of colours in the standard Minecraft colour table.
pub const COLOURS: usize = 16;

/// A colour.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Colour {
	White,
	Orange,
	Magenta,
	LightBlue,
	Yellow,
	Lime,
	Pink,
	Grey,
	LightGrey,
	Cyan,
	Purple,
	Blue,
	Brown,
	Green,
	Red,
	Black,
}

impl From<Colour> for u8 {
	fn from(x: Colour) -> u8 {
		match x {
			Colour::White => 0,
			Colour::Orange => 1,
			Colour::Magenta => 2,
			Colour::LightBlue => 3,
			Colour::Yellow => 4,
			Colour::Lime => 5,
			Colour::Pink => 6,
			Colour::Grey => 7,
			Colour::LightGrey => 8,
			Colour::Cyan => 9,
			Colour::Purple => 10,
			Colour::Blue => 11,
			Colour::Brown => 12,
			Colour::Green => 13,
			Colour::Red => 14,
			Colour::Black => 15,
		}
	}
}

impl From<Colour> for usize {
	fn from(x: Colour) -> usize {
		u8::from(x) as usize
	}
}

impl TryFrom<u8> for Colour {
	type Error = TryFromIntError;

	fn try_from(x: u8) -> Result<Self, Self::Error> {
		match x {
			0 => Ok(Colour::White),
			1 => Ok(Colour::Orange),
			2 => Ok(Colour::Magenta),
			3 => Ok(Colour::LightBlue),
			4 => Ok(Colour::Yellow),
			5 => Ok(Colour::Lime),
			6 => Ok(Colour::Pink),
			7 => Ok(Colour::Grey),
			8 => Ok(Colour::LightGrey),
			9 => Ok(Colour::Cyan),
			10 => Ok(Colour::Purple),
			11 => Ok(Colour::Blue),
			12 => Ok(Colour::Brown),
			13 => Ok(Colour::Green),
			14 => Ok(Colour::Red),
			15 => Ok(Colour::Black),
			_ => Err(TryFromIntError(())),
		}
	}
}

/// A 24-bit RGB colour.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Rgb(pub u32);

/// A component that can be given an [`Invoker`](Invoker) and a byte buffer in order to access its
/// methods.
pub trait Lockable<'invoker, 'buffer, B: Buffer> {
	/// The type obtained when locking the component.
	type Locked;

	/// Locks the component so methods can be invoked on it.
	///
	/// The [`Invoker`](Invoker) and a scratch buffer must be provided. They are released and can
	/// be reused once the locked value is dropped.
	#[must_use = "This function is only useful for its return value"]
	fn lock(&self, invoker: &'invoker mut Invoker, buffer: &'buffer mut B) -> Self::Locked;
}