terra_types/
lib.rs

1use std::{
2	marker::PhantomData,
3	ops::{Deref, DerefMut},
4};
5
6use bounded_integer::{BoundedI16, BoundedI32, BoundedI8};
7
8pub type NonNegativeI32 = BoundedI32<0, { i32::MAX }>;
9pub type PositiveI32 = BoundedI32<1, { i32::MAX }>;
10pub type PositiveI16 = BoundedI16<1, { i16::MAX }>;
11pub type NonNegativeI16 = BoundedI16<0, { i16::MAX }>;
12pub type NonNegativeI8 = BoundedI8<0, { i8::MAX }>;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub struct Color {
16	pub red: u8,
17	pub green: u8,
18	pub blue: u8,
19}
20
21impl Color {
22	pub fn new((red, green, blue): (u8, u8, u8)) -> Self {
23		Self { red, green, blue }
24	}
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum TerrariaFileType {
29	Unknown,
30	Map,
31	World,
32	Player,
33}
34
35impl TerrariaFileType {
36	pub fn name(self) -> &'static str {
37		match self {
38			Self::Map => "map",
39			Self::Player => "player",
40			Self::World => "world",
41			Self::Unknown => "unknown",
42		}
43	}
44}
45
46impl TerrariaFileType {
47	pub fn to_id(self) -> Option<u8> {
48		Some(match self {
49			Self::Map => 1,
50			Self::World => 2,
51			Self::Player => 3,
52			Self::Unknown => return None,
53		})
54	}
55
56	pub fn from_id(id: u8) -> Self {
57		match id {
58			1 => Self::Map,
59			2 => Self::World,
60			3 => Self::Player,
61			_ => Self::Unknown,
62		}
63	}
64}
65
66pub trait Unit {}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
69pub struct InTiles;
70impl Unit for InTiles {}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
73pub struct InPixels;
74impl Unit for InPixels {}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
77pub struct InMph;
78
79impl Unit for InMph {}
80
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82pub struct InDays;
83impl Unit for InDays {}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
86pub struct Vec2<U: Unit, T> {
87	pub x: T,
88	pub y: T,
89	_unit: PhantomData<U>,
90}
91
92impl<U: Unit, T> Vec2<U, T> {
93	pub fn new(x: T, y: T) -> Self {
94		Vec2 {
95			x,
96			y,
97			_unit: PhantomData,
98		}
99	}
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
103pub struct Value<U: Unit, T> {
104	value: T,
105	_unit: PhantomData<U>,
106}
107
108impl<U: Unit, T> Value<U, T> {
109	pub fn new(v: T) -> Self {
110		Self {
111			value: v,
112			_unit: PhantomData,
113		}
114	}
115
116	pub fn value(self) -> T {
117		self.value
118	}
119}
120
121impl<U: Unit, T> From<T> for Value<U, T> {
122	fn from(value: T) -> Self {
123		Self {
124			value,
125			_unit: PhantomData,
126		}
127	}
128}
129
130impl<U: Unit, T> Deref for Value<U, T> {
131	type Target = T;
132	fn deref(&self) -> &Self::Target {
133		&self.value
134	}
135}
136
137impl<U: Unit, T> DerefMut for Value<U, T> {
138	fn deref_mut(&mut self) -> &mut Self::Target {
139		&mut self.value
140	}
141}
142
143pub type YAxis<U, T> = Value<U, T>;
144pub type XAxis<U, T> = Value<U, T>;