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

Commit d134fd2

Browse files
committed
♻️ Get rid of phf and just use matches
1 parent e6b07c4 commit d134fd2

File tree

10 files changed

+6227
-4987
lines changed

10 files changed

+6227
-4987
lines changed

Cargo.lock

Lines changed: 0 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ maud = { version = "0.25.0", features = ["axum"] }
3333
moka = { version = "0.11.2", default-features = false, features = ["future", "atomic64"] }
3434
mongodb = { version = "2.6.0", features = ["tracing", "bson-uuid-1", "bson-chrono-0_4"] }
3535
monostate = "0.1.9"
36-
phf = { version = "0.11.2", features = ["macros"] }
3736
quick-xml = { version = "0.29.0", features = ["serialize"] }
3837
rayon = "1.7.0"
3938
reqwest = { version = "0.11.18", default-features = false, features = ["gzip", "json", "rustls-tls"] }

src/db/votes.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use futures::TryStreamExt;
2-
use itertools::Itertools;
32
use mongodb::{
43
bson::{doc, to_document},
54
options::UpdateOptions,
@@ -9,7 +8,7 @@ use mongodb::{
98
use crate::{
109
models::{AccountId, TankId, Vote, VoteId},
1110
prelude::*,
12-
tankopedia::vendored::TANKOPEDIA,
11+
tankopedia::vendored::ALL_TANK_IDS,
1312
};
1413

1514
#[derive(Clone)]
@@ -59,7 +58,7 @@ impl Votes {
5958

6059
/// Iterate over **all** the votes. Only the **tankopedia vehicles** are taken into account.
6160
pub async fn iter_all(&self) -> Result<Cursor<Vote>> {
62-
let filter = doc! { "_id.tid": { "$in": TANKOPEDIA.keys().map(|tank_id| *tank_id as u32).collect_vec() } };
61+
let filter = doc! { "_id.tid": { "$in": ALL_TANK_IDS.as_slice() } };
6362
self.0.find(filter, None).await.context("failed to query all votes")
6463
}
6564

src/tankopedia/bundler.rs

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use image::{DynamicImage, ImageFormat};
2020
use img_parts::webp::WebP;
2121
use reqwest::{Client, StatusCode};
2222
use serde::Deserialize;
23-
use tokio::task::spawn_blocking;
2423

2524
use crate::{models::VehicleAvailability, prelude::*, tankopedia::dvpl::Dvpl};
2625

@@ -48,17 +47,12 @@ impl BundleTankopedia {
4847
pub async fn run(self) -> Result {
4948
let client = Client::new();
5049

51-
let translations: HashMap<String, String> = {
52-
let path = self.data_path.join("Strings").join("en.yaml.dvpl");
53-
serde_yaml::from_reader(Dvpl::read(path).await?.into_reader().await?)?
54-
};
50+
let vehicles_path = self.data_path.join("XML").join("item_defs").join("vehicles");
51+
let parameters_path = self.data_path.join("3d").join("Tanks").join("Parameters");
5552

5653
static NATIONS: [&str; 9] = [
5754
"germany", "usa", "china", "france", "uk", "japan", "other", "european", "ussr",
5855
];
59-
60-
let vehicles_path = self.data_path.join("XML").join("item_defs").join("vehicles");
61-
let parameters_path = self.data_path.join("3d").join("Tanks").join("Parameters");
6256
let mut vehicles: Vec<_> = stream::iter(NATIONS)
6357
.then(|nation| {
6458
self.load_nation(vehicles_path.join(nation), parameters_path.join(nation), &client)
@@ -70,6 +64,11 @@ impl BundleTankopedia {
7064
// Sort the vehicles for pretty Git diffs when new vehicles are added.
7165
vehicles.sort_unstable_by_key(|(_, vehicle, _)| vehicle.tank_id);
7266

67+
let translations: HashMap<String, String> = {
68+
let path = self.data_path.join("Strings").join("en.yaml.dvpl");
69+
serde_yaml::from_reader(Dvpl::read(path).await?.into_reader().await?)?
70+
};
71+
7372
let mut module = File::options()
7473
.write(true)
7574
.create(true)
@@ -78,21 +77,88 @@ impl BundleTankopedia {
7877
let vendored_path = Path::new("src").join("tankopedia").join("vendored");
7978
create_dir_all(&vendored_path)?;
8079

80+
if !self.skip_images {
81+
Self::save_images(&vendored_path, &vehicles)?;
82+
}
83+
8184
writeln!(
8285
&mut module,
8386
"//! Auto-generated tankopedia, to update run `blitz-tanks bundle-tankopedia`.",
8487
)?;
8588
writeln!(&mut module)?;
86-
writeln!(&mut module, "use phf::{{phf_map, Map}};")?;
87-
writeln!(&mut module)?;
8889
writeln!(
8990
&mut module,
9091
"use crate::models::{{TankId, Vehicle, VehicleAvailability::*, VehicleType::*}};"
9192
)?;
9293
writeln!(&mut module)?;
93-
writeln!(&mut module, "pub static TANKOPEDIA: Map<u16, Vehicle> = phf_map! {{")?;
94+
Self::write_all_tank_ids(&mut module, &vehicles)?;
95+
writeln!(&mut module)?;
96+
Self::write_is_known_tank_id_function(&mut module, &vehicles)?;
97+
writeln!(&mut module)?;
98+
Self::write_get_vehicle_function(&mut module, &vehicles, &translations)?;
99+
100+
Ok(())
101+
}
102+
103+
#[instrument(skip_all)]
104+
fn save_images(
105+
vendored_path: &Path,
106+
vehicles: &[(VehicleXmlDetails, VehicleJsonDetails, DynamicImage)],
107+
) -> Result {
108+
info!("📦 Saving images…");
94109
for (xml_details, json_details, image) in vehicles {
95-
info!(json_details.tank_id, json_details.user_string, "📦 Saving…");
110+
info!(json_details.tank_id, xml_details.user_string_key);
111+
let path = vendored_path.join(json_details.tank_id.to_string()).with_extension("webp");
112+
image.save(path)?;
113+
}
114+
Ok(())
115+
}
116+
117+
#[instrument(skip_all)]
118+
fn write_all_tank_ids(
119+
mut writer: impl Write,
120+
vehicles: &[(VehicleXmlDetails, VehicleJsonDetails, DynamicImage)],
121+
) -> Result {
122+
info!("📦 Writing tank IDs…");
123+
writeln!(writer, "pub static ALL_TANK_IDS: [TankId; {}] = [", vehicles.len())?;
124+
for (_, json_details, _) in vehicles {
125+
writeln!(writer, " TankId({}),", json_details.tank_id)?;
126+
}
127+
writeln!(writer, "];")?;
128+
Ok(())
129+
}
130+
131+
#[instrument(skip_all)]
132+
fn write_is_known_tank_id_function(
133+
mut writer: impl Write,
134+
vehicles: &[(VehicleXmlDetails, VehicleJsonDetails, DynamicImage)],
135+
) -> Result {
136+
info!("📦 Writing `is_known_tank_id()`…");
137+
writeln!(writer, "pub const fn is_known_tank_id(tank_id: TankId) -> bool {{")?;
138+
writeln!(writer, " matches!(")?;
139+
writeln!(writer, " tank_id,")?;
140+
for (i, (_, json_details, _)) in vehicles.iter().enumerate() {
141+
if i != 0 {
142+
writeln!(writer, " | TankId({})", json_details.tank_id)?;
143+
} else {
144+
writeln!(writer, " TankId({})", json_details.tank_id)?;
145+
}
146+
}
147+
writeln!(writer, " )")?;
148+
writeln!(writer, "}}")?;
149+
Ok(())
150+
}
151+
152+
#[instrument(skip_all)]
153+
fn write_get_vehicle_function(
154+
mut writer: impl Write,
155+
vehicles: &[(VehicleXmlDetails, VehicleJsonDetails, DynamicImage)],
156+
translations: &HashMap<String, String>,
157+
) -> Result {
158+
info!("📦 Writing `get_vehicle()`…");
159+
writeln!(writer, "pub const fn get_vehicle(tank_id: TankId) -> Option<Vehicle> {{")?;
160+
writeln!(writer, " match tank_id {{")?;
161+
for (xml_details, json_details, _) in vehicles {
96162
let short_user_string_key = xml_details.short_user_string_key();
97163
let name = translations
98164
// Take the short name from the client.
@@ -102,31 +168,26 @@ impl BundleTankopedia {
102168
// Fall back to the long name from the API.
103169
.unwrap_or(&json_details.user_string);
104170

105-
writeln!(&mut module, " {}_u16 => Vehicle {{", json_details.tank_id)?;
106-
writeln!(&mut module, " tank_id: TankId({:?}),", json_details.tank_id)?;
107-
writeln!(&mut module, " name: {:?},", name)?;
108-
writeln!(&mut module, " tier: {:?},", json_details.tier)?;
109-
writeln!(&mut module, " type_: {:?},", json_details.type_)?;
171+
writeln!(writer, " TankId({}) => Some(Vehicle {{", json_details.tank_id)?;
172+
writeln!(writer, " tank_id: TankId({:?}),", json_details.tank_id)?;
173+
writeln!(writer, " name: {:?},", name)?;
174+
writeln!(writer, " tier: {:?},", json_details.tier)?;
175+
writeln!(writer, " type_: {:?},", json_details.type_)?;
110176
writeln!(
111-
&mut module,
112-
" availability: {:?},",
113-
VehicleAvailability::from(&json_details),
177+
writer,
178+
" availability: {:?},",
179+
VehicleAvailability::from(json_details),
114180
)?;
115181
writeln!(
116-
&mut module,
117-
r#" image_content: include_bytes!("vendored/{}.webp"),"#,
182+
writer,
183+
r#" image_content: include_bytes!("vendored/{}.webp"),"#,
118184
json_details.tank_id
119185
)?;
120-
writeln!(&mut module, r#" }},"#)?;
121-
122-
if !self.skip_images {
123-
let path =
124-
vendored_path.join(json_details.tank_id.to_string()).with_extension("webp");
125-
spawn_blocking(move || image.save(path)).await??;
126-
}
186+
writeln!(writer, r#" }}),"#)?;
127187
}
128-
writeln!(&mut module, "}};")?;
129-
188+
writeln!(writer, " _ => None,")?;
189+
writeln!(writer, " }}")?;
190+
writeln!(writer, "}}")?;
130191
Ok(())
131192
}
132193

@@ -207,7 +268,7 @@ impl BundleTankopedia {
207268
.await?;
208269
let parameters: VehicleParameters =
209270
serde_yaml::from_reader(dvpl.into_reader().await?)?;
210-
self.extract_vehicle_icon(&parameters.resources_path.big_icon_path).await?
271+
self.extract_vehicle_icon(&parameters.resource_paths.big_icon_path).await?
211272
}
212273
};
213274
let Some(image) = image else {
@@ -364,11 +425,11 @@ enum VehicleType {
364425
#[derive(Deserialize)]
365426
struct VehicleParameters {
366427
#[serde(rename = "resourcesPath")]
367-
resources_path: ResourcesPath,
428+
resource_paths: ResourcePaths,
368429
}
369430

370431
#[derive(Deserialize)]
371-
struct ResourcesPath {
432+
struct ResourcePaths {
372433
/// Path to the icon resource, for example: `~res:/Gfx/UI/BigTankIcons/ussr-KV_1s_BP`.
373434
#[serde(rename = "bigIconPath")]
374435
big_icon_path: String,

0 commit comments

Comments
 (0)