use std::collections::{BTreeMap, HashMap}; use chrono::{DateTime, Utc}; use serde::Deserialize; use torn_api_macros::ApiCategory; use crate::de_util::{self, null_is_empty_dict}; pub use crate::common::{Attack, AttackFull, LastAction, Status, Territory}; #[derive(Debug, Clone, Copy, ApiCategory)] #[api(category = "faction")] pub enum Selection { #[api(type = "Basic", flatten)] Basic, #[api(type = "BTreeMap", field = "attacks")] AttacksFull, #[api(type = "BTreeMap", field = "attacks")] Attacks, #[api( type = "HashMap", field = "territory", with = "null_is_empty_dict" )] Territory, } #[derive(Debug, Clone, Deserialize)] pub struct Member<'a> { pub name: &'a str, pub level: i16, pub days_in_faction: i16, pub position: &'a str, pub status: Status<'a>, pub last_action: LastAction, } #[derive(Debug, Clone, Deserialize)] pub struct FactionTerritoryWar<'a> { pub territory: &'a str, pub assaulting_faction: i32, pub defending_faction: i32, pub score: i32, pub required_score: i32, #[serde(with = "chrono::serde::ts_seconds")] pub start_time: DateTime, #[serde(with = "chrono::serde::ts_seconds")] pub end_time: DateTime, } #[derive(Debug, Clone, Deserialize)] pub struct Basic<'a> { #[serde(rename = "ID")] pub id: i32, pub name: &'a str, pub leader: i32, pub respect: i32, pub age: i16, pub capacity: i16, pub best_chain: i32, #[serde(borrow)] pub members: BTreeMap>, #[serde(deserialize_with = "de_util::datetime_map")] pub peace: BTreeMap>, #[serde(borrow, deserialize_with = "de_util::empty_dict_is_empty_array")] pub territory_wars: Vec>, } #[cfg(test)] mod tests { use super::*; use crate::tests::{async_test, setup, Client, ClientTrait}; #[async_test] async fn faction() { let key = setup(); let response = Client::default() .torn_api(key) .faction(|b| { b.selections(&[Selection::Basic, Selection::Attacks, Selection::Territory]) }) .await .unwrap(); response.basic().unwrap(); response.attacks().unwrap(); response.attacks_full().unwrap(); response.territory().unwrap(); } #[async_test] async fn destroyed_faction() { let key = setup(); let response = Client::default() .torn_api(key) .faction(|b| { b.id(8981) .selections(&[Selection::Basic, Selection::Territory]) }) .await .unwrap(); response.basic().unwrap(); response.territory().unwrap(); } }