summaryrefslogtreecommitdiff
path: root/src/item.rs
blob: c8337f96b057f70322c71ef29364b4729a628fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


use std::collections::HashSet;
use serde;
use serde::{Deserialize, Serialize};
use crate::{
	Template,
	components::{
		Flag,
		equipment::Equippable
	}
};



#[derive(Debug, Default, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
pub struct ItemId(pub String);

#[derive(Debug, Clone)]
pub struct Item {
	pub ent: Template,
	pub name: String,
	pub action: ItemAction
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ItemAction {
	Eat(i64),
	Build(Template, HashSet<Flag>, HashSet<Flag>),
	Equip(Equippable),
	None
}


#[cfg(test)]
mod tests {
	use super::*;
	use crate::hashmap;
	use crate::components::equipment::*;
	use serde_json::json;
	
	#[test]
	fn equip_deserialise() {
		assert_eq!(
			ItemAction::deserialize(&json!({"equip": {"slot": "hand", "stats": {"strength": 10}}})).unwrap(),
			ItemAction::Equip(Equippable {slot: Slot::Hand, stats: hashmap!(Stat::Strength => 10), sprite: Option::None})
		);
	}
	#[test]
	fn invalid_stat() {
		assert_eq!(
			ItemAction::deserialize(&json!({"equip": {"slot": "hand", "stats": {"attack": 50}}})).ok(),
			Option::None
		);
	}
}