diff --git a/src/data/load.rs b/src/data/load.rs index eaa2d59..fd8ead2 100644 --- a/src/data/load.rs +++ b/src/data/load.rs @@ -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, LoadingError> { +/// Load a single Set Bundle and create a [Vec] with the cards contained in it. +fn load_setbundle(path: PathBuf) -> Result, LoadingError> { let file = std::fs::File::open(path) .map_err(LoadingError::IO)?; let data = serde_json::de::from_reader::>(file) @@ -22,9 +24,9 @@ fn load_file(path: PathBuf) -> Result, LoadingError> { } -/// Load a single data file, and handle errors by logging them as warnings. -fn load_file_and_warn(path: PathBuf) -> Vec { - 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 { + match load_setbundle(path) { Ok(v) => v, Err(e) => { warn!("{:?}", e); @@ -34,11 +36,11 @@ fn load_file_and_warn(path: PathBuf) -> Vec { } -/// Create a [Vec] of all cards contained in the data files. -pub fn load_files() -> Vec { - 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 { + glob(pattern) + .expect("a valid glob") .filter_map(GlobResult::ok) - .map(load_file_and_warn) + .map(load_setbundle_infallible) .concat() }