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
//! APIs related to sides of blocks.

use crate::error;

/// 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 Absolute {
	Down,
	Up,
	North,
	South,
	West,
	East,
}

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

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

impl TryFrom<u8> for Absolute {
	type Error = error::TryFromInt;

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

impl Side for Absolute {}

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

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

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

impl TryFrom<u8> for Relative {
	type Error = error::TryFromInt;

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

impl Side for Relative {}