Skip to main content

battler_data/clauses/
clause_data.rs

1use alloc::string::String;
2
3use serde::{
4    Deserialize,
5    Serialize,
6};
7use serde_string_enum::{
8    DeserializeLabeledStringEnum,
9    SerializeLabeledStringEnum,
10};
11
12use crate::SerializedRuleSet;
13
14/// The type of a clause value.
15#[derive(Debug, Clone, PartialEq, Eq, SerializeLabeledStringEnum, DeserializeLabeledStringEnum)]
16pub enum ClauseValueType {
17    #[string = "Type"]
18    Type,
19    #[string = "PositiveInteger"]
20    PositiveInteger,
21    #[string = "NonNegativeInteger"]
22    NonNegativeInteger,
23}
24
25/// Data for an individual clause.
26///
27/// A clause is a generalization of a rule: a clause can be a compound rule made up of several more
28/// rules, or it can be a simple rule with an assigned value.
29#[derive(Debug, Default, Clone, Serialize, Deserialize)]
30pub struct ClauseData {
31    /// Clause name.
32    pub name: String,
33    /// Clause description.
34    pub description: String,
35    /// Message added to the battle log on battle start.
36    #[serde(default)]
37    pub rule_log: Option<String>,
38    /// Is a value required?
39    #[serde(default)]
40    pub requires_value: bool,
41    /// Type of value enforced by validation.
42    #[serde(default)]
43    pub value_type: Option<ClauseValueType>,
44    /// The default value.
45    #[serde(default)]
46    pub default_value: String,
47    /// Nested rules added to the battle format.
48    #[serde(default)]
49    pub rules: SerializedRuleSet,
50
51    /// Dynamic battle effects.
52    #[serde(default)]
53    pub effect: serde_json::Value,
54}
55
56#[cfg(test)]
57mod clause_value_type_test {
58    use crate::{
59        ClauseValueType,
60        test_util::{
61            test_string_deserialization,
62            test_string_serialization,
63        },
64    };
65
66    #[test]
67    fn serializes_to_string() {
68        test_string_serialization(ClauseValueType::Type, "Type");
69        test_string_serialization(ClauseValueType::PositiveInteger, "PositiveInteger");
70        test_string_serialization(ClauseValueType::NonNegativeInteger, "NonNegativeInteger");
71    }
72
73    #[test]
74    fn deserializes_lowercase() {
75        test_string_deserialization("type", ClauseValueType::Type);
76        test_string_deserialization("positiveinteger", ClauseValueType::PositiveInteger);
77        test_string_deserialization("nonnegativeinteger", ClauseValueType::NonNegativeInteger);
78    }
79}