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
use serde::{Deserialize, Serialize};
use tracing::{error, warn};
/// A struct representing a Dimensions object.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Default, specta::Type)]
pub struct Dimensions {
x: i32,
y: i32,
}
#[allow(dead_code)] // Until we add graphics parsing
impl Dimensions {
/// Function to create a new Dimensions object with x and y set to 0.
///
/// # Returns
///
/// * `Dimensions` - The new Dimensions object with x and y set to 0.
#[must_use]
pub const fn zero() -> Self {
Self { x: 0, y: 0 }
}
/// Function to create a new Dimensions object with given x and y values.
///
/// # Parameters
///
/// * `x` - The x value for the new Dimensions object.
/// * `y` - The y value for the new Dimensions object.
///
/// # Returns
///
/// * `Dimensions` - The new Dimensions object with the given x and y values.
#[must_use]
pub const fn from_xy(x: i32, y: i32) -> Self {
Self { x, y }
}
/// Function to create a new Dimensions object from a token.
///
/// # Parameters
///
/// * `token` - The token to parse.
///
/// # Returns
///
/// * `Dimensions` - The new Dimensions object parsed from the token.
#[must_use]
pub fn from_token(token: &str) -> Self {
let split = token.split(':').collect::<Vec<&str>>();
// [TILE_DIM:32:32]
let Some(dim_x) = split.first() else {
error!(
"Missing required number of tokens for Dimensions! {}",
token
);
return Self { x: 0, y: 0 };
};
let Some(dim_y) = split.get(1) else {
error!(
"Missing required number of tokens for Dimensions! {}",
token
);
return Self { x: 0, y: 0 };
};
let x: i32 = match dim_x.parse() {
Ok(n) => n,
Err(e) => {
warn!("Failed to parse {} as Dimensions:x, {:?}", token, e);
0
}
};
let y: i32 = match dim_y.parse() {
Ok(n) => n,
Err(e) => {
warn!("Failed to parse {} as Dimensions:y, {:?}", token, e);
0
}
};
Self { x, y }
}
/// Returns an empty Dimensions object.
///
/// # Returns
///
/// * `Dimensions` - The empty Dimensions object.
#[must_use]
pub const fn empty() -> Self {
Self::zero()
}
/// Create a new Dimensions object.
///
/// # Returns
///
/// * `Dimensions` - The new Dimensions object.
#[must_use]
pub const fn new() -> Self {
Self::zero()
}
/// Whether the Dimensions object is the default.
///
/// # Returns
///
/// * `true` - If the Dimensions object is the default.
#[must_use]
pub const fn is_default(self) -> bool {
self.x == 0 && self.y == 0
}
/// Whether the Dimensions object is empty.
///
/// # Returns
///
/// * `true` - If the Dimensions object is empty.
#[must_use]
pub const fn is_empty(&self) -> bool {
self.is_default()
}
}