1
Fork 0
mirror of https://github.com/Steffo99/patched-porobot.git synced 2024-10-16 17:47:29 +00:00

Miscellaneous data::load improvements

This commit is contained in:
Steffo 2022-07-31 13:08:38 +02:00
parent 782c3259a8
commit e17c2b446f
Signed by: steffo
GPG key ID: 6965406171929D01

View file

@ -1,3 +1,5 @@
//! This module contains functions to load **Set Bundles** from [Data Dragon](https://developer.riotgames.com/docs/lol).
use std::path::*; use std::path::*;
use glob::{glob, GlobResult}; use glob::{glob, GlobResult};
use itertools::Itertools; use itertools::Itertools;
@ -12,8 +14,8 @@ enum LoadingError {
} }
/// Load a single data file. /// Load a single Set Bundle and create a [Vec] with the cards contained in it.
fn load_file(path: PathBuf) -> Result<Vec<Card>, LoadingError> { fn load_setbundle(path: PathBuf) -> Result<Vec<Card>, LoadingError> {
let file = std::fs::File::open(path) let file = std::fs::File::open(path)
.map_err(LoadingError::IO)?; .map_err(LoadingError::IO)?;
let data = serde_json::de::from_reader::<std::fs::File, Vec<Card>>(file) let data = serde_json::de::from_reader::<std::fs::File, Vec<Card>>(file)
@ -22,9 +24,9 @@ fn load_file(path: PathBuf) -> Result<Vec<Card>, LoadingError> {
} }
/// Load a single data file, and handle errors by logging them as warnings. /// Load a single Set Bundle (similarly to [load_setbundle]), but instead of returning a [Result], return an empty [Vec] in case of failure and log a [warn]ing.
fn load_file_and_warn(path: PathBuf) -> Vec<Card> { fn load_setbundle_infallible(path: PathBuf) -> Vec<Card> {
match load_file(path) { match load_setbundle(path) {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
warn!("{:?}", e); warn!("{:?}", e);
@ -34,11 +36,11 @@ fn load_file_and_warn(path: PathBuf) -> Vec<Card> {
} }
/// Create a [Vec] of all cards contained in the data files. /// Load all Set Bundles matched by the passed glob, using [load_setbundle_infallible] and then concatenating the resulting [Vec]s.
pub fn load_files() -> Vec<Card> { pub fn load_setbundles_infallible(pattern: &str) -> Vec<Card> {
glob("./data/*/en_us/data/*.json") glob(pattern)
.unwrap() .expect("a valid glob")
.filter_map(GlobResult::ok) .filter_map(GlobResult::ok)
.map(load_file_and_warn) .map(load_setbundle_infallible)
.concat() .concat()
} }