mirror of
https://github.com/Steffo99/patched-porobot.git
synced 2024-12-23 01:54:22 +00:00
Miscellaneous data::load
improvements
This commit is contained in:
parent
782c3259a8
commit
e17c2b446f
1 changed files with 12 additions and 10 deletions
|
@ -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 glob::{glob, GlobResult};
|
||||
use itertools::Itertools;
|
||||
|
@ -12,8 +14,8 @@ enum LoadingError {
|
|||
}
|
||||
|
||||
|
||||
/// Load a single data file.
|
||||
fn load_file(path: PathBuf) -> Result<Vec<Card>, LoadingError> {
|
||||
/// Load a single Set Bundle and create a [Vec] with the cards contained in it.
|
||||
fn load_setbundle(path: PathBuf) -> Result<Vec<Card>, LoadingError> {
|
||||
let file = std::fs::File::open(path)
|
||||
.map_err(LoadingError::IO)?;
|
||||
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.
|
||||
fn load_file_and_warn(path: PathBuf) -> Vec<Card> {
|
||||
match load_file(path) {
|
||||
/// 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_setbundle_infallible(path: PathBuf) -> Vec<Card> {
|
||||
match load_setbundle(path) {
|
||||
Ok(v) => v,
|
||||
Err(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.
|
||||
pub fn load_files() -> Vec<Card> {
|
||||
glob("./data/*/en_us/data/*.json")
|
||||
.unwrap()
|
||||
/// Load all Set Bundles matched by the passed glob, using [load_setbundle_infallible] and then concatenating the resulting [Vec]s.
|
||||
pub fn load_setbundles_infallible(pattern: &str) -> Vec<Card> {
|
||||
glob(pattern)
|
||||
.expect("a valid glob")
|
||||
.filter_map(GlobResult::ok)
|
||||
.map(load_file_and_warn)
|
||||
.map(load_setbundle_infallible)
|
||||
.concat()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue