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"] }
|
clap = { version = "4.5.53", features = ["derive"] }
|
||||||
directories = "6.0.0"
|
directories = "6.0.0"
|
||||||
fluent-templates = "0.13.2"
|
fluent-templates = "0.13.2"
|
||||||
reqwest = { version = "0.12.26", default-features = false, features = [
|
reqwest = { version = "0.12.28", default-features = false, features = [
|
||||||
"brotli",
|
"brotli",
|
||||||
"http2",
|
"http2",
|
||||||
"gzip",
|
"gzip",
|
||||||
|
|
@ -21,7 +21,7 @@ serde = { version = "1.0.228", default-features = false, features = [
|
||||||
"derive",
|
"derive",
|
||||||
"std",
|
"std",
|
||||||
] }
|
] }
|
||||||
serde_json = "1.0.147"
|
serde_json = "1.0.148"
|
||||||
strum = { version = "0.27.2", features = ["derive"] }
|
strum = { version = "0.27.2", features = ["derive"] }
|
||||||
tokio = { version = "1.48.0", default-features = false, features = [
|
tokio = { version = "1.48.0", default-features = false, features = [
|
||||||
"fs",
|
"fs",
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ pub enum EditionCode {
|
||||||
/// Sword and Shield
|
/// Sword and Shield
|
||||||
Ssh,
|
Ssh,
|
||||||
/// SV Promos
|
/// SV Promos
|
||||||
|
#[serde(alias = "PR-SV")]
|
||||||
Svp,
|
Svp,
|
||||||
/// Scarlet and Violet
|
/// Scarlet and Violet
|
||||||
Svi,
|
Svi,
|
||||||
|
|
@ -52,6 +53,8 @@ pub enum EditionCode {
|
||||||
Blk,
|
Blk,
|
||||||
/// White Flare
|
/// White Flare
|
||||||
Wht,
|
Wht,
|
||||||
|
/// Mega Evolution Promos
|
||||||
|
Mep,
|
||||||
/// Mega Evolution
|
/// Mega Evolution
|
||||||
Meg,
|
Meg,
|
||||||
/// Phantasmal Flames
|
/// Phantasmal Flames
|
||||||
|
|
@ -79,6 +82,7 @@ impl EditionCode {
|
||||||
EditionCode::Dri => "SV10",
|
EditionCode::Dri => "SV10",
|
||||||
EditionCode::Blk => "SV10pt5ZSV",
|
EditionCode::Blk => "SV10pt5ZSV",
|
||||||
EditionCode::Wht => "SV10pt5RSV",
|
EditionCode::Wht => "SV10pt5RSV",
|
||||||
|
EditionCode::Mep => "MEP",
|
||||||
EditionCode::Meg => "MEG1",
|
EditionCode::Meg => "MEG1",
|
||||||
EditionCode::Pfl => "MEG2",
|
EditionCode::Pfl => "MEG2",
|
||||||
}
|
}
|
||||||
|
|
@ -104,6 +108,7 @@ impl EditionCode {
|
||||||
EditionCode::Dri => "destined-rivals",
|
EditionCode::Dri => "destined-rivals",
|
||||||
EditionCode::Blk | EditionCode::Wht => "black-white",
|
EditionCode::Blk | EditionCode::Wht => "black-white",
|
||||||
EditionCode::Meg => "mega-evolution",
|
EditionCode::Meg => "mega-evolution",
|
||||||
|
EditionCode::Mep => "mega-evolution-promos",
|
||||||
EditionCode::Pfl => "phantasmal-flames",
|
EditionCode::Pfl => "phantasmal-flames",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -128,7 +133,7 @@ impl EditionCode {
|
||||||
| EditionCode::Dri
|
| EditionCode::Dri
|
||||||
| EditionCode::Blk
|
| EditionCode::Blk
|
||||||
| EditionCode::Wht => EditionBlock::Sv,
|
| 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::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
use serde::{Deserialize, de};
|
use serde::{Deserialize, de};
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
use crate::editions::EditionCode;
|
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)]
|
#[derive(Debug, Deserialize, Eq, PartialEq, Hash)]
|
||||||
pub enum Lang {
|
pub enum Lang {
|
||||||
|
|
@ -29,12 +31,17 @@ pub enum Lang {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Edition {
|
pub struct RawEdition {
|
||||||
path: String,
|
path: String,
|
||||||
#[serde(deserialize_with = "deserialize_edition_code")]
|
#[serde(deserialize_with = "deserialize_edition_code")]
|
||||||
abbr: Option<EditionCode>,
|
abbr: Option<EditionCode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Edition {
|
||||||
|
path: String,
|
||||||
|
abbr: EditionCode,
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize_edition_code<'de, D>(deserializer: D) -> Result<Option<EditionCode>, D::Error>
|
fn deserialize_edition_code<'de, D>(deserializer: D) -> Result<Option<EditionCode>, D::Error>
|
||||||
where
|
where
|
||||||
D: de::Deserializer<'de>,
|
D: de::Deserializer<'de>,
|
||||||
|
|
@ -45,11 +52,25 @@ where
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let buf = if buf.to_uppercase() == "PR-SV" {
|
let result = serde_json::from_str::<EditionCode>(&format!("\"{buf}\""))
|
||||||
"SVP".into()
|
.with_context(|| format!("couldn't deserialize edition code {buf}"))
|
||||||
} else {
|
.inspect_err(|e| warn!("{e}"));
|
||||||
buf
|
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