Skip to main content

terra_items_xbox360/
slot.rs

1use crate::{Item, Prefix};
2use terra_types::PositiveI16;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct ItemSlot {
6	pub item: Item,
7	pub prefix: Option<Prefix>,
8	pub count: PositiveI16,
9}
10
11impl ItemSlot {
12	pub fn new(item: Item) -> Self {
13		Self::from(item)
14	}
15
16	pub fn prefix(mut self, prefix: Prefix) -> Self {
17		self.prefix = Some(prefix);
18		self
19	}
20
21	pub fn count(mut self, count: PositiveI16) -> Self {
22		self.count = count;
23		self
24	}
25}
26
27impl From<Item> for ItemSlot {
28	fn from(value: Item) -> Self {
29		Self {
30			item: value,
31			prefix: None,
32			count: PositiveI16::MIN,
33		}
34	}
35}
36
37impl From<SingleItemSlot> for ItemSlot {
38	fn from(value: SingleItemSlot) -> Self {
39		ItemSlot {
40			item: value.item,
41			prefix: value.prefix,
42			count: PositiveI16::MIN,
43		}
44	}
45}
46
47#[cfg(feature = "convert")]
48impl TryFrom<terra_items::ItemSlot> for ItemSlot {
49	type Error = ();
50	fn try_from(value: terra_items::ItemSlot) -> Result<Self, ()> {
51		Ok(Self {
52			item: value.item.try_into()?,
53			prefix: value.prefix.and_then(|x| x.try_into().ok()),
54			count: PositiveI16::new(value.count.get().min(i16::MAX as i32) as i16)
55				.expect("Same min bound"),
56		})
57	}
58}
59
60#[cfg(feature = "convert")]
61impl TryFrom<ItemSlot> for terra_items::ItemSlot {
62	type Error = ();
63	fn try_from(value: ItemSlot) -> Result<Self, ()> {
64		Ok(Self {
65			item: value.item.try_into()?,
66			prefix: value.prefix.map(|x| x.into()),
67			count: terra_types::PositiveI32::new(value.count.get() as i32).expect("Same min bound"),
68		})
69	}
70}
71
72#[cfg(feature = "convert")]
73impl TryFrom<terra_items::WorldItemSlot> for ItemSlot {
74	type Error = ();
75	fn try_from(value: terra_items::WorldItemSlot) -> Result<Self, ()> {
76		Ok(Self {
77			item: value.item.try_into()?,
78			prefix: value.prefix.and_then(|x| x.try_into().ok()),
79			count: value.count,
80		})
81	}
82}
83
84#[cfg(feature = "convert")]
85impl TryFrom<ItemSlot> for terra_items::WorldItemSlot {
86	type Error = ();
87	fn try_from(value: ItemSlot) -> Result<Self, ()> {
88		Ok(Self {
89			item: value.item.try_into()?,
90			prefix: value.prefix.map(|x| x.into()),
91			count: value.count,
92		})
93	}
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
97pub struct SingleItemSlot {
98	pub item: Item,
99	pub prefix: Option<Prefix>,
100}
101
102impl SingleItemSlot {
103	pub fn new(item: Item) -> Self {
104		Self::from(item)
105	}
106
107	pub fn prefix(mut self, prefix: Prefix) -> Self {
108		self.prefix = Some(prefix);
109		self
110	}
111}
112
113impl From<Item> for SingleItemSlot {
114	fn from(value: Item) -> Self {
115		Self {
116			item: value,
117			prefix: None,
118		}
119	}
120}
121
122impl From<ItemSlot> for SingleItemSlot {
123	fn from(item_slot: ItemSlot) -> Self {
124		SingleItemSlot {
125			item: item_slot.item,
126			prefix: item_slot.prefix,
127		}
128	}
129}
130
131#[cfg(feature = "convert")]
132impl TryFrom<terra_items::SingleItemSlot> for SingleItemSlot {
133	type Error = ();
134	fn try_from(value: terra_items::SingleItemSlot) -> Result<Self, ()> {
135		Ok(Self {
136			item: value.item.try_into()?,
137			prefix: value.prefix.and_then(|x| x.try_into().ok()),
138		})
139	}
140}
141
142#[cfg(feature = "convert")]
143impl TryFrom<SingleItemSlot> for terra_items::SingleItemSlot {
144	type Error = ();
145	fn try_from(value: SingleItemSlot) -> Result<Self, ()> {
146		Ok(Self {
147			item: value.item.try_into()?,
148			prefix: value.prefix.map(|x| x.into()),
149		})
150	}
151}