Skip to main content

battler_data/items/
item_target.rs

1use serde_string_enum::{
2    DeserializeLabeledStringEnum,
3    SerializeLabeledStringEnum,
4};
5
6/// The acceptable target of an item.
7#[derive(
8    Debug, Clone, Copy, PartialEq, Eq, SerializeLabeledStringEnum, DeserializeLabeledStringEnum,
9)]
10pub enum ItemTarget {
11    /// A Mon in the player's party.
12    #[string = "Party"]
13    Party,
14    /// The active Mon that the player is controlling.
15    #[string = "Active"]
16    Active,
17    /// A foe on the battle field.
18    #[string = "Foe"]
19    Foe,
20    /// An isolated foe on the battle field.
21    #[string = "IsolatedFoe"]
22    IsolatedFoe,
23}
24
25impl ItemTarget {
26    /// Is the item target choosable?
27    pub fn choosable(&self) -> bool {
28        match self {
29            Self::Party | Self::Foe => true,
30            _ => false,
31        }
32    }
33
34    /// Does the item require a single target?
35    pub fn requires_target(&self) -> bool {
36        true
37    }
38}
39
40#[cfg(test)]
41mod item_target_test {
42    use crate::{
43        items::ItemTarget,
44        test_util::{
45            test_string_deserialization,
46            test_string_serialization,
47        },
48    };
49
50    #[test]
51    fn serializes_to_string() {
52        test_string_serialization(ItemTarget::Party, "Party");
53        test_string_serialization(ItemTarget::Foe, "Foe");
54        test_string_serialization(ItemTarget::IsolatedFoe, "IsolatedFoe");
55    }
56
57    #[test]
58    fn deserializes_lowercase() {
59        test_string_deserialization("party", ItemTarget::Party);
60        test_string_deserialization("foe", ItemTarget::Foe);
61        test_string_deserialization("isolatedfoe", ItemTarget::IsolatedFoe);
62    }
63}