oc_wasm_helpers/
sides.rs

1//! APIs related to sides of blocks.
2
3use crate::error;
4
5/// The number of sides on a block.
6pub const BLOCK_SIDES: usize = 6;
7
8/// A trait implemented by both absolute and relative block side enumerations.
9pub trait Side: Into<u8> + Into<usize> {}
10
11/// An absolute side of a block.
12#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
13pub enum Absolute {
14	Down,
15	Up,
16	North,
17	South,
18	West,
19	East,
20}
21
22impl From<Absolute> for u8 {
23	fn from(x: Absolute) -> Self {
24		match x {
25			Absolute::Down => 0,
26			Absolute::Up => 1,
27			Absolute::North => 2,
28			Absolute::South => 3,
29			Absolute::West => 4,
30			Absolute::East => 5,
31		}
32	}
33}
34
35impl From<Absolute> for usize {
36	fn from(x: Absolute) -> Self {
37		u8::from(x) as usize
38	}
39}
40
41impl TryFrom<u8> for Absolute {
42	type Error = error::TryFromInt;
43
44	fn try_from(x: u8) -> Result<Self, Self::Error> {
45		match x {
46			0 => Ok(Absolute::Down),
47			1 => Ok(Absolute::Up),
48			2 => Ok(Absolute::North),
49			3 => Ok(Absolute::South),
50			4 => Ok(Absolute::West),
51			5 => Ok(Absolute::East),
52			_ => Err(error::TryFromInt),
53		}
54	}
55}
56
57impl Side for Absolute {}
58
59/// A relative side of a block.
60#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
61pub enum Relative {
62	Bottom,
63	Top,
64	Back,
65	Front,
66	Right,
67	Left,
68}
69
70impl From<Relative> for u8 {
71	fn from(x: Relative) -> Self {
72		match x {
73			Relative::Bottom => 0,
74			Relative::Top => 1,
75			Relative::Back => 2,
76			Relative::Front => 3,
77			Relative::Right => 4,
78			Relative::Left => 5,
79		}
80	}
81}
82
83impl From<Relative> for usize {
84	fn from(x: Relative) -> Self {
85		u8::from(x) as usize
86	}
87}
88
89impl TryFrom<u8> for Relative {
90	type Error = error::TryFromInt;
91
92	fn try_from(x: u8) -> Result<Self, Self::Error> {
93		match x {
94			0 => Ok(Relative::Bottom),
95			1 => Ok(Relative::Top),
96			2 => Ok(Relative::Back),
97			3 => Ok(Relative::Front),
98			4 => Ok(Relative::Right),
99			5 => Ok(Relative::Left),
100			_ => Err(error::TryFromInt),
101		}
102	}
103}
104
105impl Side for Relative {}