fdm_toolkit/
collectable.rs

1use serde::{
2	de::{Unexpected, Visitor, Error as DeserializationError},
3	Deserializer, Deserialize
4};
5use serde_derive::{Deserialize, Serialize};
6
7use core::fmt::{Formatter, Display, Result as FmtResult};
8
9
10
11
12
13/// An object which can be held in a player's inventory.
14pub trait Collectable:Sized {
15	fn name(&self) -> &str;
16	fn typ(&self)  -> CollectableType;
17}
18
19
20
21
22
23/// Test
24#[doc(hidden)]
25struct CollectableTypeVisitor;
26
27impl<'de> Visitor<'de> for CollectableTypeVisitor {
28	type Value = CollectableType;
29	
30	fn expecting(&self, f:&mut std::fmt::Formatter) -> std::fmt::Result { f.write_str("the name of a CollectableType (variant)") }
31	fn visit_str<E:DeserializationError>(self, v:&str) -> Result<Self::Value, E> {
32		if v.eq_ignore_ascii_case("item") || v.eq_ignore_ascii_case("material") { return Ok(CollectableType::Item); }
33		match v {
34			_ if v.eq_ignore_ascii_case("block") => Ok(CollectableType::Block),
35			_ if v.eq_ignore_ascii_case("tool")  => Ok(CollectableType::Tool),
36			_ => Err(E::invalid_value(Unexpected::Other("unrecognized value"), &self))
37		}
38	}
39}
40
41
42
43/// The type of collectable that something is.
44#[derive(Serialize, Clone, Debug, Copy)]
45#[serde(rename_all="lowercase")]
46pub enum CollectableType {
47	Block,
48	#[serde(rename="material")]
49	Item,
50	Tool
51}
52
53impl<'de> Deserialize<'de> for CollectableType {
54	fn deserialize<D:Deserializer<'de>>(d:D) -> Result<Self, D::Error> { d.deserialize_enum("CollectableType", &["Block", "Item", "Tool"], CollectableTypeVisitor) }
55}
56
57impl Display for CollectableType {
58	fn fmt(&self, f:&mut Formatter) -> FmtResult {
59		f.write_str(match self {
60			Self::Block => "block",
61			Self::Item  => "material",
62			Self::Tool  => "tool"
63		})
64	}
65}
66
67
68
69/// The standard set of items in 4D Miner.
70#[non_exhaustive]
71#[derive(Deserialize, Serialize, Clone, Debug, Hash, Copy)]
72#[serde(rename_all="snake_case")]
73pub enum Item {
74	Stick,
75	Hammer,
76	IronPick,
77	DeadlyPick,
78	IronAxe,
79	DeadlyAxe,
80	Ultrahammer,
81	SolenoidCollector,
82	Rock,
83	Hypersilk,
84	IronBars,
85	DeadlyBars,
86	SolenoidBars,
87	Compass,
88	Glasses,
89	KleinBottle,
90	HealthPotion,
91	RedLens,
92	GreenLens,
93	BlueLens,
94	Alidade
95}
96
97impl Item {
98	#[inline(always)] pub const fn as_str(&self) -> &'static str {
99		match self {
100			Self::SolenoidCollector => "Solenoid Collector",
101			Self::SolenoidBars      => "Solenoid Bars",
102			Self::HealthPotion      => "Health Potion",
103			Self::KleinBottle       => "Klein Bottle",
104			Self::Ultrahammer       => "Ultrahammer",
105			Self::DeadlyBars        => "Deadly Bars",
106			Self::DeadlyPick        => "Deadly Pick",
107			Self::DeadlyAxe         => "Deadly Axe",
108			Self::GreenLens         => "Green Lens",
109			Self::Hypersilk         => "Hypersilk",
110			Self::BlueLens          => "Blue Lens",
111			Self::IronBars          => "Iron Bars",
112			Self::IronPick          => "Iron Pick",
113			Self::Alidade           => "Alidade",
114			Self::Compass           => "Compass",
115			Self::Glasses           => "4D Glasses",
116			Self::IronAxe           => "Iron Axe",
117			Self::RedLens           => "Red Lens",
118			Self::Hammer            => "Hammer",
119			Self::Stick             => "Stick",
120			Self::Rock              => "Rock"
121		}
122	}
123}
124
125impl Collectable for Item {
126	fn name(&self) -> &str { self.as_str() }
127	fn typ(&self)  -> CollectableType {
128		match self {
129			Self::SolenoidCollector => CollectableType::Tool,
130			Self::SolenoidBars      => CollectableType::Item,
131			Self::HealthPotion      => CollectableType::Item,
132			Self::KleinBottle       => CollectableType::Item,
133			Self::Ultrahammer       => CollectableType::Tool,
134			Self::DeadlyBars        => CollectableType::Item,
135			Self::DeadlyPick        => CollectableType::Tool,
136			Self::DeadlyAxe         => CollectableType::Tool,
137			Self::GreenLens         => CollectableType::Item,
138			Self::Hypersilk         => CollectableType::Item,
139			Self::BlueLens          => CollectableType::Item,
140			Self::IronBars          => CollectableType::Item,
141			Self::IronPick          => CollectableType::Tool,
142			Self::Alidade           => CollectableType::Item,
143			Self::Compass           => CollectableType::Item,
144			Self::Glasses           => CollectableType::Item,
145			Self::IronAxe           => CollectableType::Tool,
146			Self::RedLens           => CollectableType::Item,
147			Self::Hammer            => CollectableType::Tool,
148			Self::Stick             => CollectableType::Tool,
149			Self::Rock              => CollectableType::Item
150		}
151	}
152}
153
154impl Display for Item {
155	fn fmt(&self, f:&mut Formatter) -> FmtResult { f.write_str(self.as_str()) }
156}