From 14c4fc7d1284cb2e70bb69aa91898cc8241ce673 Mon Sep 17 00:00:00 2001 From: TotallyNot <44345987+TotallyNot@users.noreply.github.com> Date: Fri, 11 Aug 2023 16:45:19 +0200 Subject: [PATCH] added territory war report selection and marked non-exhaustive enums --- torn-api/Cargo.toml | 2 +- torn-api/src/faction.rs | 5 +- torn-api/src/key.rs | 21 +++++++ torn-api/src/torn.rs | 129 +++++++++++++++++++++++++++++++++++++--- torn-api/src/user.rs | 5 +- 5 files changed, 152 insertions(+), 10 deletions(-) diff --git a/torn-api/Cargo.toml b/torn-api/Cargo.toml index 2d9fea9..05a8eb8 100644 --- a/torn-api/Cargo.toml +++ b/torn-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "torn-api" -version = "0.5.21" +version = "0.5.22" edition = "2021" authors = ["Pyrit [2111649]"] license = "MIT" diff --git a/torn-api/src/faction.rs b/torn-api/src/faction.rs index 298b00e..804532e 100644 --- a/torn-api/src/faction.rs +++ b/torn-api/src/faction.rs @@ -11,7 +11,8 @@ pub use crate::common::{Attack, AttackFull, LastAction, Status, Territory}; #[derive(Debug, Clone, Copy, ApiCategory)] #[api(category = "faction")] -pub enum Selection { +#[non_exhaustive] +pub enum FactionSelection { #[api(type = "Basic", flatten)] Basic, @@ -29,6 +30,8 @@ pub enum Selection { Territory, } +pub type Selection = FactionSelection; + #[derive(Debug, Clone, Deserialize)] pub struct Member<'a> { pub name: &'a str, diff --git a/torn-api/src/key.rs b/torn-api/src/key.rs index c02fa0a..a39a943 100644 --- a/torn-api/src/key.rs +++ b/torn-api/src/key.rs @@ -5,12 +5,14 @@ use torn_api_macros::ApiCategory; #[derive(Debug, Clone, Copy, ApiCategory)] #[api(category = "key")] +#[non_exhaustive] pub enum Selection { #[api(type = "Info", flatten)] Info, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[non_exhaustive] pub enum AccessType { #[serde(rename = "Custom")] Custom, @@ -30,12 +32,14 @@ pub enum AccessType { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] +#[non_exhaustive] pub enum KeySelection { Info, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] +#[non_exhaustive] pub enum UserSelection { Ammo, Attacks, @@ -83,10 +87,13 @@ pub enum UserSelection { WorkStats, Lookup, PublicStatus, + #[serde(other)] + Unknown, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] +#[non_exhaustive] pub enum FactionSelection { Applications, Armor, @@ -124,10 +131,13 @@ pub enum FactionSelection { Lookup, Caches, CrimeExp, + #[serde(other)] + Unknown, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] +#[non_exhaustive] pub enum CompanySelection { Applications, Companies, @@ -139,10 +149,13 @@ pub enum CompanySelection { Stock, Timestamp, Lookup, + #[serde(other)] + Unknown, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] +#[non_exhaustive] pub enum TornSelection { Bank, Cards, @@ -177,24 +190,32 @@ pub enum TornSelection { TerritoryNames, TerritoryWarReport, RaidReport, + #[serde(other)] + Unknown, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] +#[non_exhaustive] pub enum MarketSelection { Bazaar, ItemMarket, PointsMarket, Timestamp, Lookup, + #[serde(other)] + Unknown, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] +#[non_exhaustive] pub enum PropertySelection { Property, Timestamp, Lookup, + #[serde(other)] + Unknown, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/torn-api/src/torn.rs b/torn-api/src/torn.rs index 32fd641..f013f8a 100644 --- a/torn-api/src/torn.rs +++ b/torn-api/src/torn.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use chrono::{DateTime, Utc}; use serde::{ de::{self, MapAccess, Visitor}, - Deserialize, + Deserialize, Deserializer, }; use torn_api_macros::ApiCategory; @@ -12,7 +12,8 @@ use crate::user; #[derive(Debug, Clone, Copy, ApiCategory)] #[api(category = "torn")] -pub enum Selection { +#[non_exhaustive] +pub enum TornSelection { #[api( field = "competition", with = "decode_competition", @@ -26,11 +27,20 @@ pub enum Selection { #[api(type = "HashMap", field = "rackets")] Rackets, - #[api(type = "HashMap", field = "territory")] + #[api( + type = "HashMap", + with = "decode_territory", + field = "territory" + )] Territory, + + #[api(type = "TerritoryWarReport", field = "territorywarreport")] + TerritoryWarReport, } -#[derive(Deserialize)] +pub type Selection = TornSelection; + +#[derive(Debug, Clone, Deserialize)] pub struct EliminationLeaderboard { pub position: i16, pub team: user::EliminationTeam, @@ -41,6 +51,7 @@ pub struct EliminationLeaderboard { pub losses: i32, } +#[derive(Debug, Clone)] pub enum Competition { Elimination { teams: Vec }, Unkown(String), @@ -109,6 +120,7 @@ where #[derive(Debug, Clone, Deserialize)] pub struct TerritoryWar { + pub territory_war_id: i32, pub assaulting_faction: i32, pub defending_faction: i32, @@ -143,6 +155,58 @@ pub struct Territory { pub racket: Option, } +fn decode_territory<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + Ok(Option::deserialize(deserializer)?.unwrap_or_default()) +} + +#[derive(Clone, Debug, Deserialize)] +pub struct TerritoryWarReportTerritory { + pub name: String, +} + +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum TerritoryWarOutcome { + EndWithPeaceTreaty, + FailAssault, + SuccessAssault, +} + +#[derive(Clone, Debug, Deserialize)] +pub struct TerritoryWarReportWar { + #[serde(with = "chrono::serde::ts_seconds")] + pub start: DateTime, + #[serde(with = "chrono::serde::ts_seconds")] + pub end: DateTime, + + pub result: TerritoryWarOutcome, +} + +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum TerritoryWarReportRole { + Aggressor, + Defender, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct TerritoryWarReportFaction { + pub name: String, + pub score: i32, + pub joins: i32, + pub clears: i32, +} + +#[derive(Clone, Debug, Deserialize)] +pub struct TerritoryWarReport { + pub territory: TerritoryWarReportTerritory, + pub war: TerritoryWarReportWar, + pub factions: HashMap, +} + #[cfg(test)] mod tests { use super::*; @@ -156,9 +220,9 @@ mod tests { .torn_api(key) .torn(|b| { b.selections(&[ - Selection::Competition, - Selection::TerritoryWars, - Selection::Rackets, + TornSelection::Competition, + TornSelection::TerritoryWars, + TornSelection::Rackets, ]) }) .await @@ -182,4 +246,55 @@ mod tests { let territory = response.territory().unwrap(); assert!(territory.contains_key("NSC")); } + + #[async_test] + async fn invalid_territory() { + let key = setup(); + + let response = Client::default() + .torn_api(key) + .torn(|b| b.selections(&[Selection::Territory]).id("AAA")) + .await + .unwrap(); + + assert!(response.territory().unwrap().is_empty()); + } + + #[async_test] + async fn territory_war_report() { + let key = setup(); + + let response = Client::default() + .torn_api(&key) + .torn(|b| b.selections(&[Selection::TerritoryWarReport]).id(37403)) + .await + .unwrap(); + + assert_eq!( + response.territory_war_report().unwrap().war.result, + TerritoryWarOutcome::SuccessAssault + ); + + let response = Client::default() + .torn_api(&key) + .torn(|b| b.selections(&[Selection::TerritoryWarReport]).id(37502)) + .await + .unwrap(); + + assert_eq!( + response.territory_war_report().unwrap().war.result, + TerritoryWarOutcome::FailAssault + ); + + let response = Client::default() + .torn_api(&key) + .torn(|b| b.selections(&[Selection::TerritoryWarReport]).id(37860)) + .await + .unwrap(); + + assert_eq!( + response.territory_war_report().unwrap().war.result, + TerritoryWarOutcome::EndWithPeaceTreaty + ); + } } diff --git a/torn-api/src/user.rs b/torn-api/src/user.rs index eb46acc..54f3239 100644 --- a/torn-api/src/user.rs +++ b/torn-api/src/user.rs @@ -12,7 +12,8 @@ pub use crate::common::{Attack, AttackFull, LastAction, Status}; #[derive(Debug, Clone, Copy, ApiCategory)] #[api(category = "user")] -pub enum Selection { +#[non_exhaustive] +pub enum UserSelection { #[api(type = "Basic", flatten)] Basic, #[api(type = "Profile", flatten)] @@ -29,6 +30,8 @@ pub enum Selection { Attacks, } +pub type Selection = UserSelection; + #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] pub enum Gender { Male,