Skip to main content

battler_data/conditions/
condition_data.rs

1use alloc::string::String;
2
3use serde::{
4    Deserialize,
5    Serialize,
6};
7use serde_string_enum::{
8    DeserializeLabeledStringEnum,
9    SerializeLabeledStringEnum,
10};
11
12/// The type of a condition.
13#[derive(Debug, Clone, PartialEq, Eq, SerializeLabeledStringEnum, DeserializeLabeledStringEnum)]
14pub enum ConditionType {
15    /// A condition that is built into the battle engine.
16    #[string = "Built-in"]
17    #[alias = "BuiltIn"]
18    BuiltIn,
19    /// An ordinary condition that can be applied to anything in a battle.
20    #[string = "Condition"]
21    Condition,
22    /// Weather, which is applied to the entire battlefield.
23    #[string = "Weather"]
24    Weather,
25    /// Status, which is applied to a single Mon for a finite amount of time.
26    #[string = "Status"]
27    Status,
28    /// Type, which is applied to a single Mon for a finite amount of time while the user has the
29    /// type.
30    #[string = "Type"]
31    Type,
32    /// Volatile, which is applied to a single Mon for a finite amount of time.
33    #[string = "Volatile"]
34    Volatile,
35    /// Z-Power, which is applied to the user of a Z-Move.
36    #[string = "ZPower"]
37    #[alias = "Z-Power"]
38    ZPower,
39}
40
41/// Data about a particular condition.
42///
43/// Conditions can be applied to Mons as the result of moves or abilities.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct ConditionData {
46    /// Condition name.
47    pub name: String,
48    /// Condition type.
49    pub condition_type: ConditionType,
50    /// Can this condition be copied from one Mon to another?
51    ///
52    /// This relates to how "Baton Pass" affects this condition.
53    #[serde(default)]
54    pub no_copy: bool,
55
56    /// Dynamic battle effects.
57    #[serde(default)]
58    pub condition: serde_json::Value,
59}
60
61#[cfg(test)]
62mod condition_test {
63    use crate::{
64        conditions::ConditionType,
65        test_util::{
66            test_string_deserialization,
67            test_string_serialization,
68        },
69    };
70
71    #[test]
72    fn serializes_to_string() {
73        test_string_serialization(ConditionType::BuiltIn, "Built-in");
74        test_string_serialization(ConditionType::Condition, "Condition");
75        test_string_serialization(ConditionType::Weather, "Weather");
76        test_string_serialization(ConditionType::Status, "Status");
77    }
78
79    #[test]
80    fn deserializes_lowercase() {
81        test_string_deserialization("built-in", ConditionType::BuiltIn);
82        test_string_deserialization("builtin", ConditionType::BuiltIn);
83        test_string_deserialization("condition", ConditionType::Condition);
84        test_string_deserialization("weather", ConditionType::Weather);
85        test_string_deserialization("status", ConditionType::Status);
86    }
87}