use serde for edition parsing and filter out editions
This commit is contained in:
parent
674eba3fd0
commit
e95f18c22c
4 changed files with 296 additions and 345 deletions
591
Cargo.lock
generated
591
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue