Skip to content

Commit

Permalink
Revert "remove some boilerplate"
Browse files Browse the repository at this point in the history
This reverts commit 4591ab6.
  • Loading branch information
B-2U committed Aug 2, 2024
1 parent 4591ab6 commit 08091a8
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 51 deletions.
14 changes: 1 addition & 13 deletions src/structs/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use serde::de::{self, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use std::fmt;

use crate::utils::{IsacError, IsacInfo};

/// the status code of the response
#[derive(Debug, Deserialize, Serialize)]
pub struct Status {
Expand All @@ -14,23 +12,13 @@ pub struct Status {
}

impl Status {
/// return an [`IsacInfo::APIError`] if the status is not `ok`
pub fn error_for_status(&self) -> Result<(), IsacError> {
match self.status.as_str() == "ok" {
true => Ok(()),
false => Err(IsacInfo::APIError {
msg: self.err_msg(),
})?,
}
}
/// true if the status code is "ok"
pub fn ok(&self) -> bool {
matches!(self.status.as_str(), "ok")
}
/// return the error message, return "Unknown Error" if its None
pub fn err_msg(&self) -> String {
pub fn err_msg(self) -> String {
self.error
.as_ref()
.map(|e| e.message.clone())
.unwrap_or("Unknown Error".to_string())
}
Expand Down
9 changes: 7 additions & 2 deletions src/structs/clan_battles_season_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashMap;
use serde::{Deserialize, Serialize};

use super::api;
use crate::utils::IsacError;
use crate::utils::{IsacError, IsacInfo};

#[derive(Serialize, Deserialize, Debug)]
pub struct PlayerClanBattleAPIRes {
Expand All @@ -24,7 +24,12 @@ impl TryFrom<PlayerClanBattleAPIRes> for PlayerClanBattle {
type Error = IsacError;

fn try_from(value: PlayerClanBattleAPIRes) -> Result<Self, Self::Error> {
value.status.error_for_status()?;
if !value.status.ok() {
return Err(IsacInfo::APIError {
msg: value.status.err_msg(),
}
.into());
}
Ok(value
.data
.into_iter()
Expand Down
11 changes: 8 additions & 3 deletions src/structs/clan_detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use serde::{Deserialize, Serialize};

use super::{api, ClanTag};
use crate::utils::IsacError;
use crate::utils::{IsacError, IsacInfo};

// #[derive(Debug, Deserialize, Serialize)]
// pub struct ClanDetailMember {
Expand Down Expand Up @@ -47,8 +47,13 @@ pub struct ClanDetailAPIRes {
impl ClanDetailAPIRes {
/// check the status is "ok" before getting the data
pub fn data(self) -> Result<ClanDetail, IsacError> {
self.status.error_for_status()?;
Ok(self.data.into_values().next().unwrap())
if !self.status.ok() {
Err(IsacInfo::APIError {
msg: self.status.err_msg(),
})?
} else {
Ok(self.data.into_values().next().unwrap())
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/structs/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
api, Dogtag, PartialClan, PlayerClanBattle, Region, Ship, ShipModeStatsPair,
ShipStatsCollection,
},
utils::{wws_api::WowsApi, IsacError, LoadSaveFromJson},
utils::{wws_api::WowsApi, IsacError, IsacInfo, LoadSaveFromJson},
Context,
};

Expand Down Expand Up @@ -140,7 +140,11 @@ impl TryFrom<VortexPlayerAPIRes> for VortexPlayer {
type Error = IsacError;

fn try_from(value: VortexPlayerAPIRes) -> Result<Self, Self::Error> {
value.status.error_for_status()?;
if !value.status.ok() {
Err(IsacInfo::APIError {
msg: value.status.err_msg(),
})?
};
Ok(value.data.into_iter().next().unwrap().1)
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/structs/ship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,12 @@ impl TryFrom<VortexShipAPIRes> for ShipStatsCollection {
type Error = IsacError;

fn try_from(mut value: VortexShipAPIRes) -> Result<Self, Self::Error> {
value.status.error_for_status()?;

if !value.status.ok() {
return Err(IsacInfo::APIError {
msg: value.status.err_msg(),
}
.into());
}
let player_stats = value.data.values_mut().last().ok_or(IsacInfo::APIError {
msg: "expected PlayerStats".to_string(),
})?;
Expand Down
56 changes: 30 additions & 26 deletions src/structs/ships_para.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,36 @@ impl TryFrom<VortexVehicleAPIRes> for ShipsPara {
type Error = IsacError;

fn try_from(res: VortexVehicleAPIRes) -> Result<Self, Self::Error> {
res.status.error_for_status()?;

let output = res
.data
.into_iter()
.map(|(k, mut v)| {
let class = v
.tags
.iter()
.find_map(ShipClass::from_tag)
.expect("missing ship class tag");
let ship = Ship {
ship_id: k,
tier: v.level,
tier_roman: v.level.into(),
class,
name: v.localization.mark.remove("en").expect("missing en"),
short_name: v.localization.shortmark.remove("en").expect("missing en"),
nation: v.nation,
icon: v.icons.small,
};
(k, ship)
})
.collect::<HashMap<ShipId, Ship>>()
.into();
Ok(output)
if !res.status.ok() {
Err(IsacInfo::APIError {
msg: res.status.err_msg(),
})?
} else {
let output = res
.data
.into_iter()
.map(|(k, mut v)| {
let class = v
.tags
.iter()
.find_map(ShipClass::from_tag)
.expect("missing ship class tag");
let ship = Ship {
ship_id: k,
tier: v.level,
tier_roman: v.level.into(),
class,
name: v.localization.mark.remove("en").expect("missing en"),
short_name: v.localization.shortmark.remove("en").expect("missing en"),
nation: v.nation,
icon: v.icons.small,
};
(k, ship)
})
.collect::<HashMap<ShipId, Ship>>()
.into();
Ok(output)
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/utils/wws_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,13 @@ impl TryFrom<VortexPlayerSearchAPIRes> for Vec<VortexPlayerSearch> {
type Error = IsacError;

fn try_from(res: VortexPlayerSearchAPIRes) -> Result<Self, Self::Error> {
res.status.error_for_status()?;

Ok(res.data)
if !res.status.ok() {
Err(IsacInfo::APIError {
msg: res.status.err_msg(),
})?
} else {
Ok(res.data)
}
}
}

Expand Down

0 comments on commit 08091a8

Please sign in to comment.