coc_rs/models/
war.rs

1use chrono::TimeZone;
2use serde::{Deserialize, Serialize};
3
4use crate::models::badge_urls::BadgeUrls;
5
6#[derive(Debug, Serialize, Deserialize, Clone)]
7#[serde(rename_all = "camelCase")]
8pub struct War {
9    pub state: String,
10    pub team_size: Option<i32>,
11    pub attacks_per_member: Option<i8>,
12    preparation_start_time: Option<String>,
13    start_time: Option<String>,
14    end_time: Option<String>,
15    pub clan: Option<WarClan>,
16    pub opponent: Option<WarClan>,
17}
18
19#[derive(Debug, Serialize, Deserialize, Clone)]
20#[serde(rename_all = "camelCase")]
21pub struct WarClan {
22    pub tag: Option<String>,
23    pub name: Option<String>,
24    pub badge_urls: BadgeUrls,
25    pub clan_level: Option<i8>,
26    pub attacks: Option<i32>,
27    pub stars: Option<i32>,
28    pub destruction_percentage: Option<f64>,
29    pub members: Option<Vec<Member>>,
30}
31
32#[derive(Debug, Serialize, Deserialize, Clone)]
33#[serde(rename_all = "camelCase")]
34pub struct Member {
35    pub tag: String,
36    pub name: String,
37    pub townhall_level: i8,
38    pub map_position: i32,
39    pub attacks: Option<Vec<Attack>>,
40    pub opponent_attacks: i32,
41    pub best_opponent_attack: Option<Attack>,
42}
43
44#[derive(Debug, Serialize, Deserialize, Clone)]
45#[serde(rename_all = "camelCase")]
46pub struct Attack {
47    pub attacker_tag: String,
48    pub defender_tag: String,
49    pub stars: i32,
50    pub destruction_percentage: f32,
51    pub order: i32,
52    pub duration: i32,
53}
54
55impl War {
56    /// Returns the start time of this [`War`].
57    ///
58    /// # Panics
59    ///
60    /// Panics if parsing the start time fails, which should never happen.
61    #[must_use]
62    pub fn start_time(&self) -> Option<chrono::DateTime<chrono::Utc>> {
63        self.start_time.as_ref().map(|start_time| {
64            chrono::Utc.from_utc_datetime(
65                &chrono::NaiveDateTime::parse_from_str(start_time, "%Y%m%dT%H%M%S.%fZ").unwrap(),
66            )
67        })
68    }
69
70    /// Returns the end time of this [`War`].
71    ///
72    /// # Panics
73    ///
74    /// Panics if parsing the end time fails, which should never happen.
75    #[must_use]
76    pub fn end_time(&self) -> Option<chrono::DateTime<chrono::Utc>> {
77        self.end_time.as_ref().map(|end_time| {
78            chrono::Utc.from_utc_datetime(
79                &chrono::NaiveDateTime::parse_from_str(end_time, "%Y%m%dT%H%M%S.%fZ").unwrap(),
80            )
81        })
82    }
83
84    /// Returns the preparation start time of this [`War`].
85    ///
86    /// # Panics
87    ///
88    /// Panics if parsing the preparation start time fails, which should never happen.
89    #[must_use]
90    pub fn preparation_start_time(&self) -> Option<chrono::DateTime<chrono::Utc>> {
91        self.preparation_start_time.as_ref().map(|preparation_start_time| {
92            chrono::Utc.from_utc_datetime(
93                &chrono::NaiveDateTime::parse_from_str(preparation_start_time, "%Y%m%dT%H%M%S.%fZ")
94                    .unwrap(),
95            )
96        })
97    }
98}