Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit e6b07c4

Browse files
committed
♻️ Introduce TankId new-type
1 parent 58d74d9 commit e6b07c4

File tree

17 files changed

+708
-669
lines changed

17 files changed

+708
-669
lines changed

.idea/runConfigurations/Check.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/db/sessions.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ use mongodb::{
77
};
88
use uuid::Uuid;
99

10-
use crate::{
11-
models::{AccountId, User},
12-
prelude::*,
13-
};
10+
use crate::{models::User, prelude::*};
1411

1512
/// Wrapper around the tree to manage client-side sessions.
1613
#[derive(Clone)]
@@ -43,6 +40,8 @@ impl Sessions {
4340
pub async fn insert_test_session(&self) -> Result<Uuid> {
4441
use chrono::Duration;
4542

43+
use crate::models::AccountId;
44+
4645
let session_id = Uuid::new_v4();
4746
self.insert(&User {
4847
session_id,
@@ -73,7 +72,7 @@ mod tests {
7372
use chrono::Duration;
7473

7574
use super::*;
76-
use crate::db::Db;
75+
use crate::{db::Db, models::AccountId};
7776

7877
#[tokio::test]
7978
#[ignore]

src/db/votes.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use mongodb::{
77
};
88

99
use crate::{
10-
models::{AccountId, Vote, VoteId},
10+
models::{AccountId, TankId, Vote, VoteId},
1111
prelude::*,
1212
tankopedia::vendored::TANKOPEDIA,
1313
};
@@ -22,7 +22,7 @@ impl Votes {
2222
Ok(Self(collection))
2323
}
2424

25-
#[instrument(skip_all, fields(account_id = %vote.id.account_id, tank_id = vote.id.tank_id))]
25+
#[instrument(skip_all, fields(account_id = %vote.id.account_id, tank_id = %vote.id.tank_id))]
2626
pub async fn insert(&self, vote: &Vote) -> Result {
2727
let options = UpdateOptions::builder().upsert(true).build();
2828
self.0
@@ -38,8 +38,8 @@ impl Votes {
3838
Ok(())
3939
}
4040

41-
#[instrument(skip_all, fields(account_id = %account_id, tank_id = tank_id))]
42-
pub async fn delete(&self, account_id: AccountId, tank_id: u16) -> Result {
41+
#[instrument(skip_all, fields(account_id = %account_id, tank_id = %tank_id))]
42+
pub async fn delete(&self, account_id: AccountId, tank_id: TankId) -> Result {
4343
let vote_id = VoteId { account_id, tank_id };
4444
self.0
4545
.delete_one(doc! { "_id": to_document(&vote_id)? }, None)
@@ -81,7 +81,7 @@ mod tests {
8181
#[ignore]
8282
async fn get_all_by_account_id_ok() -> Result {
8383
let manager = Db::open_unittests().await?.votes().await?;
84-
let mut vote = Vote::new(1, 42, Rating::Like);
84+
let mut vote = Vote::new(1, TankId(42), Rating::Like);
8585
vote.timestamp = vote.timestamp.duration_round(Duration::seconds(1))?;
8686
manager.insert(&vote).await?;
8787

@@ -117,9 +117,9 @@ mod tests {
117117
#[ignore]
118118
async fn delete_vote_ok() -> Result {
119119
let manager = Db::open_unittests().await?.votes().await?;
120-
let vote = Vote::new(1, 42, Rating::Like);
120+
let vote = Vote::new(1, TankId(42), Rating::Like);
121121
manager.insert(&vote).await?;
122-
manager.delete(vote.id.account_id, 42).await?;
122+
manager.delete(vote.id.account_id, vote.id.tank_id).await?;
123123
assert!(
124124
manager
125125
.iter_by_account_id(vote.id.account_id)

src/models.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod account_id;
22
mod rated_tank_id;
33
mod rating;
4+
mod tank_id;
45
mod user;
56
mod vehicle;
67
mod vote;
@@ -9,6 +10,7 @@ pub use self::{
910
account_id::AccountId,
1011
rated_tank_id::RatedTankId,
1112
rating::Rating,
13+
tank_id::TankId,
1214
user::{Anonymous, User},
1315
vehicle::{Vehicle, VehicleAvailability, VehicleType},
1416
vote::{Vote, VoteId},

src/models/rated_tank_id.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ use std::cmp::Ordering;
22

33
use serde::{Deserialize, Serialize};
44

5+
use crate::models::TankId;
6+
57
/// Pair of tank ID and its rating.
68
///
79
/// # Notes
810
///
911
/// - Natural sorting order is of decreasing rating.
1012
/// - I also use it for tank ID & similarity pair.
1113
#[derive(Serialize, Deserialize)]
12-
pub struct RatedTankId(pub u16, pub f64);
14+
pub struct RatedTankId(pub TankId, pub f64);
1315

1416
impl Eq for RatedTankId {}
1517

src/models/tank_id.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use mongodb::bson::Bson;
2+
3+
#[derive(
4+
Clone,
5+
Copy,
6+
Debug,
7+
Eq,
8+
Hash,
9+
Ord,
10+
PartialEq,
11+
PartialOrd,
12+
derive_more::Display,
13+
derive_more::From,
14+
derive_more::FromStr,
15+
derive_more::Into,
16+
serde::Deserialize,
17+
serde::Serialize,
18+
)]
19+
pub struct TankId(pub u16); // FIXME
20+
21+
impl From<TankId> for Bson {
22+
#[inline]
23+
fn from(tank_id: TankId) -> Self {
24+
Bson::from(tank_id.0 as i32)
25+
}
26+
}

src/models/vehicle.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use crate::models::TankId;
2+
13
pub struct Vehicle {
2-
pub tank_id: u16,
4+
pub tank_id: TankId,
35
pub name: &'static str,
46
pub tier: u8,
57
pub type_: VehicleType,

src/models/vote.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use mongodb::bson::serde_helpers;
33
use serde::{Deserialize, Serialize};
44

55
use crate::{
6-
models::{rating::Rating, AccountId},
6+
models::{rating::Rating, AccountId, TankId},
77
prelude::*,
88
};
99

@@ -13,7 +13,7 @@ pub struct VoteId {
1313
pub account_id: AccountId,
1414

1515
#[serde(rename = "tid")]
16-
pub tank_id: u16,
16+
pub tank_id: TankId,
1717
}
1818

1919
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
@@ -36,7 +36,7 @@ pub struct Vote {
3636
}
3737

3838
impl Vote {
39-
pub fn new(account_id: impl Into<AccountId>, tank_id: u16, rating: Rating) -> Self {
39+
pub fn new(account_id: impl Into<AccountId>, tank_id: TankId, rating: Rating) -> Self {
4040
Self {
4141
id: VoteId {
4242
account_id: account_id.into(),

src/tankopedia/bundler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl BundleTankopedia {
8787
writeln!(&mut module)?;
8888
writeln!(
8989
&mut module,
90-
"use crate::models::{{Vehicle, VehicleAvailability::*, VehicleType::*}};"
90+
"use crate::models::{{TankId, Vehicle, VehicleAvailability::*, VehicleType::*}};"
9191
)?;
9292
writeln!(&mut module)?;
9393
writeln!(&mut module, "pub static TANKOPEDIA: Map<u16, Vehicle> = phf_map! {{")?;
@@ -103,7 +103,7 @@ impl BundleTankopedia {
103103
.unwrap_or(&json_details.user_string);
104104

105105
writeln!(&mut module, " {}_u16 => Vehicle {{", json_details.tank_id)?;
106-
writeln!(&mut module, " tank_id: {:?},", json_details.tank_id)?;
106+
writeln!(&mut module, " tank_id: TankId({:?}),", json_details.tank_id)?;
107107
writeln!(&mut module, " name: {:?},", name)?;
108108
writeln!(&mut module, " tier: {:?},", json_details.tier)?;
109109
writeln!(&mut module, " type_: {:?},", json_details.type_)?;

0 commit comments

Comments
 (0)