openstranded_common_items/
item_stack.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub struct ItemStack {
19 pub item_id: u32,
21
22 pub amount: u32,
24
25 #[serde(default)]
30 pub durability: Option<f32>,
31}
32
33impl ItemStack {
34 #[must_use]
38 pub const fn new(item_id: u32, amount: u32) -> Self {
39 Self {
40 item_id,
41 amount,
42 durability: None,
43 }
44 }
45
46 #[must_use]
50 pub const fn with_durability(item_id: u32, amount: u32, durability: f32) -> Self {
51 Self {
52 item_id,
53 amount,
54 durability: Some(durability),
55 }
56 }
57
58 #[must_use]
60 pub const fn is_empty(&self) -> bool {
61 self.amount == 0
62 }
63
64 pub const MAX_STACK: u32 = 999;
69
70 pub fn merge(&mut self, other: &Self) -> u32 {
75 if self.item_id != other.item_id {
76 return other.amount;
77 }
78
79 let total = self.amount + other.amount;
80 if total <= Self::MAX_STACK {
81 self.amount = total;
82 0
83 } else {
84 self.amount = Self::MAX_STACK;
85 total - Self::MAX_STACK
86 }
87 }
88
89 #[must_use]
94 pub fn split(&mut self, count: u32) -> Option<Self> {
95 if count == 0 || count > self.amount {
96 return None;
97 }
98 self.amount -= count;
99 Some(Self {
100 item_id: self.item_id,
101 amount: count,
102 durability: self.durability,
103 })
104 }
105}
106
107#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn test_new_stack() {
115 let stack = ItemStack::new(23, 5);
116 assert_eq!(stack.item_id, 23);
117 assert_eq!(stack.amount, 5);
118 assert!(stack.durability.is_none());
119 }
120
121 #[test]
122 fn test_with_durability() {
123 let stack = ItemStack::with_durability(30, 1, 100.0);
124 assert_eq!(stack.durability, Some(100.0));
125 }
126
127 #[test]
128 fn test_is_empty() {
129 assert!(ItemStack::new(1, 0).is_empty());
130 assert!(!ItemStack::new(1, 1).is_empty());
131 }
132
133 #[test]
134 fn test_merge_same_type() {
135 let mut dst = ItemStack::new(1, 500);
136 let src = ItemStack::new(1, 300);
137 let overflow = dst.merge(&src);
138 assert_eq!(dst.amount, 800);
139 assert_eq!(overflow, 0);
140 }
141
142 #[test]
143 fn test_merge_overflow() {
144 let mut dst = ItemStack::new(1, 700);
145 let src = ItemStack::new(1, 500);
146 let overflow = dst.merge(&src);
147 assert_eq!(dst.amount, ItemStack::MAX_STACK);
148 assert_eq!(overflow, 700 + 500 - ItemStack::MAX_STACK);
149 }
150
151 #[test]
152 fn test_merge_different_type() {
153 let mut dst = ItemStack::new(1, 5);
154 let src = ItemStack::new(2, 3);
155 assert_eq!(dst.merge(&src), 3);
156 assert_eq!(dst.amount, 5);
157 }
158
159 #[test]
160 fn test_split_happy() {
161 let mut stack = ItemStack::new(1, 10);
162 let part = stack.split(3).unwrap();
163 assert_eq!(part.amount, 3);
164 assert_eq!(part.item_id, 1);
165 assert_eq!(stack.amount, 7);
166 }
167
168 #[test]
169 fn test_split_all() {
170 let mut stack = ItemStack::new(1, 5);
171 let part = stack.split(5).unwrap();
172 assert_eq!(part.amount, 5);
173 assert_eq!(stack.amount, 0);
174 }
175
176 #[test]
177 fn test_split_too_many() {
178 let mut stack = ItemStack::new(1, 3);
179 assert!(stack.split(10).is_none());
180 assert_eq!(stack.amount, 3);
181 }
182
183 #[test]
184 fn test_split_zero() {
185 let mut stack = ItemStack::new(1, 3);
186 assert!(stack.split(0).is_none());
187 assert_eq!(stack.amount, 3);
188 }
189
190 #[test]
191 fn test_serde_derives_compile() {
192 let stack = ItemStack::with_durability(42, 3, 75.5);
194 assert_eq!(stack.item_id, 42);
195 assert_eq!(stack.amount, 3);
196 assert_eq!(stack.durability, Some(75.5));
197 }
198}