1#[macro_export]
17macro_rules! define_index {
18 ($name:ident, $inner:ty, $doc:expr) => {
19 #[doc = $doc]
20 #[repr(transparent)]
21 #[derive(
22 Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord
23 )]
24 pub struct $name($inner);
25
26 impl $name {
27
28 pub const fn from(var: $inner) -> Self {
30 Self(var)
31 }
32
33 pub const fn get(&self) -> $inner {
35 self.0
36 }
37 }
38
39 impl std::ops::Deref for $name {
40 type Target = $inner;
41
42 fn deref(&self) -> &Self::Target {
43 &self.0
44 }
45 }
46
47 impl From<$inner> for $name {
48 fn from(value: $inner) -> Self {
49 $name(value)
50 }
51 }
52
53 impl From<$name> for $inner {
54 fn from(value: $name) -> Self {
55 value.0
56 }
57 }
58
59 impl std::fmt::Display for $name {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(f, "{}", self.0)
62 }
63 }
64 };
65}
66
67#[macro_export]
82macro_rules! define_nonzero_count {
83 ($name:ident, $base:ty, $doc:expr) => {
84 #[doc = $doc]
85 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
86 pub struct $name {
87 value: $base,
88 }
89
90 impl $name {
91 pub fn new(value: $base) -> Result<Self, FeagiDataError> {
93 if value == 0 {
94 return Err(FeagiDataError::BadParameters("Count cannot be zero!".into()));
95 }
96 Ok($name{
97 value,
98 })
99 }
100 }
101 impl TryFrom<$base> for $name {
102 type Error = FeagiDataError;
103 fn try_from(value: $base) -> Result<Self, FeagiDataError> {
104 $name::new({value})
105 }
106 }
107
108 impl From<$name> for $base {
109 fn from(value: $name) -> $base {
110 value.value
111 }
112 }
113
114 impl std::ops::Deref for $name {
115 type Target = $base;
116 fn deref(&self) -> &Self::Target {
117 &self.value
118 }
119 }
120
121 impl std::fmt::Display for $name {
122 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
123 self.value.fmt(f)
124 }
125 }
126
127 }
128}
129
130#[macro_export]
148macro_rules! define_xy_coordinates {
149 ($name:ident, $var_type:ty, $friendly_name:expr, $doc_string:expr) => {
150
151 #[doc = $doc_string]
152 #[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)]
153 pub struct $name {
154 pub x: $var_type,
155 pub y: $var_type,
156 }
157
158 impl $name {
159 pub fn new(x: $var_type, y: $var_type) -> Self {
160 Self { x, y }
161 }
162 }
163
164 impl From<$name> for ($var_type, $var_type) {
165 fn from(value: $name) -> Self {
166 (value.x, value.y)
167 }
168 }
169
170 impl From<($var_type, $var_type)> for $name {
171 fn from(value: ($var_type, $var_type)) -> Self {
172 $name::new(value.0, value.1)
173 }
174 }
175
176 impl std::fmt::Display for $name {
177 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
178 write!(f, "{}({}, {})", $friendly_name, self.x, self.y)
179 }
180 }
181
182 };
183}
184
185#[macro_export]
201macro_rules! define_xy_dimensions {
202 ($name:ident, $var_type:ty, $friendly_name:expr, $invalid_zero_value:expr, $doc_string:expr) => {
203
204 #[doc = $doc_string]
205 #[derive(Clone, Debug, PartialEq, Copy, Hash, Eq)]
206 pub struct $name {
207 pub width: $var_type,
208 pub height: $var_type,
209 }
210
211 impl $name {
212 pub fn new(x: $var_type, y: $var_type) -> Result<Self, FeagiDataError> {
213 if x == $invalid_zero_value || y == $invalid_zero_value {
214 return Err(FeagiDataError::BadParameters(format!("Value cannot be {:?} in a {:?}!", $invalid_zero_value, $friendly_name)));
215 }
216 Ok(Self { width: x, height: y })
217 }
218 }
219
220 impl std::fmt::Display for $name {
221 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
222 write!(f, "{}<{}, {}>", $friendly_name, self.width, self.height)
223 }
224 }
225
226 impl From<$name> for ($var_type, $var_type) {
227 fn from(value: $name) -> Self {
228 (value.width, value.height)
229 }
230 }
231
232 impl TryFrom<($var_type, $var_type)> for $name {
233 type Error = FeagiDataError;
234 fn try_from(value: ($var_type, $var_type)) -> Result<Self, Self::Error> {
235 if value.0 == $invalid_zero_value {
236 return Err(FeagiDataError::BadParameters(format!("X value cannot be zero!")));
237 }
238 if value.1 == $invalid_zero_value {
239 return Err(FeagiDataError::BadParameters(format!("Y value cannot be zero!")));
240 }
241 Ok(Self { width: value.0, height: value.1 })
242 }
243 }
244
245 }
246}
247
248#[macro_export]
267macro_rules! define_xyz_coordinates {
268 ($name:ident, $var_type:ty, $friendly_name:expr, $doc_string:expr) => {
269
270 #[doc = $doc_string]
271 #[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)]
272 pub struct $name {
273 pub x: $var_type,
274 pub y: $var_type,
275 pub z: $var_type,
276 }
277
278 impl $name {
279 pub fn new(x: $var_type, y: $var_type, z: $var_type) -> Self {
280 Self { x, y, z }
281 }
282 }
283
284 impl From<$name> for ($var_type, $var_type, $var_type) {
285 fn from(value: $name) -> Self {
286 (value.x, value.y, value.z)
287 }
288 }
289
290 impl From<($var_type, $var_type, $var_type)> for $name {
291 fn from(value: ($var_type, $var_type, $var_type)) -> Self {
292 $name::new(value.0, value.1, value.2)
293 }
294 }
295
296 impl std::fmt::Display for $name {
297 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
298 write!(f, "{}({}, {}, {})", $friendly_name, self.x, self.y, self.z)
299 }
300 }
301
302 };
303}
304
305#[macro_export]
323macro_rules! define_xyz_dimensions {
324 ($name:ident, $var_type:ty, $friendly_name:expr, $invalid_zero_value:expr, $doc_string:expr) => {
325
326 #[doc = $doc_string]
327 #[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)]
328 pub struct $name {
329 pub width: $var_type,
330 pub height: $var_type,
331 pub depth: $var_type,
332 }
333
334 impl $name {
335 pub fn new(x: $var_type, y: $var_type, z: $var_type) -> Result<Self, FeagiDataError> {
336 if x == $invalid_zero_value || y == $invalid_zero_value || z == $invalid_zero_value {
337 return Err(FeagiDataError::BadParameters(format!("Value cannot be {:?} in a {:?}!", $invalid_zero_value, $friendly_name)));
338 }
339 Ok(Self { width: x, height: y, depth: z })
340 }
341
342 pub fn number_elements(&self) -> $var_type {
343 self.width * self.height * self.depth
344 }
345 }
346
347 impl std::fmt::Display for $name {
348 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
349 write!(f, "{}<{}, {}, {}>", $friendly_name, self.width, self.height, self.depth)
350 }
351 }
352
353 impl From<$name> for ($var_type, $var_type, $var_type) {
354 fn from(value: $name) -> Self {
355 (value.width, value.height, value.depth)
356 }
357 }
358
359 impl TryFrom<($var_type, $var_type, $var_type)> for $name {
360 type Error = FeagiDataError;
361 fn try_from(value: ($var_type, $var_type, $var_type)) -> Result<Self, Self::Error> {
362 if value.0 == $invalid_zero_value {
363 return Err(FeagiDataError::BadParameters(format!("X value cannot be zero!")));
364 }
365 if value.1 == $invalid_zero_value {
366 return Err(FeagiDataError::BadParameters(format!("Y value cannot be zero!")));
367 }
368 if value.2 == $invalid_zero_value {
369 return Err(FeagiDataError::BadParameters(format!("Z value cannot be zero!")));
370 }
371 Ok(Self { width: value.0, height: value.1, depth: value.2 })
372 }
373 }
374 }
375}
376
377#[macro_export]
393macro_rules! define_xyz_mapping{
394 ($XYZ_a:ident, $XYZ_b:ident) => {
395 impl From<$XYZ_a> for $XYZ_b {
396 fn from(a: $XYZ_a) -> Self {
397 $XYZ_b::new(a.width, a.height, a.depth).unwrap()
398 }
399 }
400 impl From<$XYZ_b> for $XYZ_a {
401 fn from(b: $XYZ_b) -> Self {
402 $XYZ_a::new(b.width, b.height, b.depth).unwrap()
403 }
404 }
405 }
406}
407
408#[macro_export]
425macro_rules! define_xyz_dimension_range {
426 ($name:ident, $var_type:ty, $coordinate_type:ty, $friendly_name:expr, $doc:expr) => {
427 #[doc = $doc]
428 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
429 pub struct $name {
430 pub width: std::ops::Range<$var_type>,
431 pub height: std::ops::Range<$var_type>,
432 pub depth: std::ops::Range<$var_type>
433 }
434
435 impl $name {
436 pub fn new(x: std::ops::Range<$var_type>, y: std::ops::Range<$var_type>, z: std::ops::Range<$var_type>) -> Result<Self, FeagiDataError> {
438 Ok($name {width: x, height: y, depth: z})
439 }
440
441 pub fn verify_coordinate_within_range(&self, coordinate: &$coordinate_type) -> Result<(), FeagiDataError> {
443 if self.width.contains(&coordinate.width) && self.height.contains(&coordinate.height) && self.depth.contains(&coordinate.depth) {
444 return Ok(());
445 }
446 Err(FeagiDataError::BadParameters(format!("Coordinate {:?} is not contained by this given range of {:?}!", coordinate, self)))
447
448 }
449 }
450
451 impl std::fmt::Display for $name {
452 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
453 write!(f, "{}<{:?}, {:?}, {:?}>", $friendly_name, self.width, self.height, self.depth)
454 }
455 }
456 };
457}
458
459