Compare commits

..

2 commits

5 changed files with 299 additions and 348 deletions

591
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,7 @@ camino = "1.2.2"
clap = { version = "4.5.53", features = ["derive"] }
directories = "6.0.0"
fluent-templates = "0.13.2"
reqwest = { version = "0.12.26", default-features = false, features = [
reqwest = { version = "0.12.28", default-features = false, features = [
"brotli",
"http2",
"gzip",
@ -21,7 +21,7 @@ serde = { version = "1.0.228", default-features = false, features = [
"derive",
"std",
] }
serde_json = "1.0.147"
serde_json = "1.0.148"
strum = { version = "0.27.2", features = ["derive"] }
tokio = { version = "1.48.0", default-features = false, features = [
"fs",

View file

@ -19,6 +19,7 @@ pub enum EditionCode {
/// Sword and Shield
Ssh,
/// SV Promos
#[serde(alias = "PR-SV")]
Svp,
/// Scarlet and Violet
Svi,
@ -52,6 +53,8 @@ pub enum EditionCode {
Blk,
/// White Flare
Wht,
/// Mega Evolution Promos
Mep,
/// Mega Evolution
Meg,
/// Phantasmal Flames
@ -79,6 +82,7 @@ impl EditionCode {
EditionCode::Dri => "SV10",
EditionCode::Blk => "SV10pt5ZSV",
EditionCode::Wht => "SV10pt5RSV",
EditionCode::Mep => "MEP",
EditionCode::Meg => "MEG1",
EditionCode::Pfl => "MEG2",
}
@ -104,6 +108,7 @@ impl EditionCode {
EditionCode::Dri => "destined-rivals",
EditionCode::Blk | EditionCode::Wht => "black-white",
EditionCode::Meg => "mega-evolution",
EditionCode::Mep => "mega-evolution-promos",
EditionCode::Pfl => "phantasmal-flames",
}
}
@ -128,7 +133,7 @@ impl EditionCode {
| EditionCode::Dri
| EditionCode::Blk
| EditionCode::Wht => EditionBlock::Sv,
EditionCode::Meg | EditionCode::Pfl => EditionBlock::Meg,
EditionCode::Meg | EditionCode::Mep | EditionCode::Pfl => EditionBlock::Meg,
}
}
}

View file

@ -7,7 +7,7 @@ use tokio_stream::StreamExt;
use tokio_util::io::StreamReader;
use tracing::debug;
use super::models::Index;
use super::models::RawIndex;
use crate::directories::data_cache_directory;
/// Client to download data from mallie.io
@ -41,12 +41,12 @@ impl Client {
Ok(())
}
async fn load_tcgl_index(&self) -> Result<Index> {
async fn load_tcgl_index(&self) -> Result<RawIndex> {
let file_path = self.data_cache_directory.join("tcgl_index.json");
let index = tokio::fs::read_to_string(&file_path)
.await
.with_context(|| format!("Failed to read {file_path}"))?;
let index: Index =
let index: RawIndex =
serde_json::from_str(&index).with_context(|| format!("Couldn't parse {file_path}"))?;
Ok(index)
}

View file

@ -2,13 +2,15 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::str::FromStr;
use anyhow::Context;
use serde::{Deserialize, de};
use tracing::warn;
use crate::editions::EditionCode;
pub type Index = HashMap<Lang, HashMap<String, Edition>>;
pub type RawIndex = HashMap<Lang, HashMap<String, RawEdition>>;
pub type Index = HashMap<Lang, Vec<Edition>>;
#[derive(Debug, Deserialize, Eq, PartialEq, Hash)]
pub enum Lang {
@ -29,12 +31,17 @@ pub enum Lang {
}
#[derive(Debug, Deserialize)]
pub struct Edition {
pub struct RawEdition {
path: String,
#[serde(deserialize_with = "deserialize_edition_code")]
abbr: Option<EditionCode>,
}
pub struct Edition {
path: String,
abbr: EditionCode,
}
fn deserialize_edition_code<'de, D>(deserializer: D) -> Result<Option<EditionCode>, D::Error>
where
D: de::Deserializer<'de>,
@ -45,11 +52,25 @@ where
return Ok(None);
}
let buf = if buf.to_uppercase() == "PR-SV" {
"SVP".into()
} else {
buf
};
let result = serde_json::from_str::<EditionCode>(&format!("\"{buf}\""))
.with_context(|| format!("couldn't deserialize edition code {buf}"))
.inspect_err(|e| warn!("{e}"));
Ok(result.ok())
}
Ok(EditionCode::from_str(&buf).ok())
fn filter_invalid_editions(index: RawIndex) -> Index {
index
.into_iter()
.map(|(k, v)| {
let v = v
.into_values()
.map(|e| match e.abbr {
Some(abbr) => Some(Edition { path: e.path, abbr }),
None => None,
})
.flatten()
.collect();
(k, v)
})
.collect()
}