1
Fork 0
mirror of https://github.com/Steffo99/patched-porobot.git synced 2024-12-23 10:04:21 +00:00

Create functions to load data files into a single Vec

This commit is contained in:
Steffo 2022-07-31 10:22:34 +02:00
parent 72e75a795f
commit 2e9e63ed9f
Signed by: steffo
GPG key ID: 6965406171929D01
3 changed files with 49 additions and 0 deletions

44
src/data/load.rs Normal file
View file

@ -0,0 +1,44 @@
use std::path::*;
use glob::{glob, GlobResult};
use itertools::Itertools;
use crate::data::schema::Card;
use log::*;
#[derive(Debug)]
enum LoadingError {
IO(std::io::Error),
Parsing(serde_json::Error),
}
/// Load a single data file.
fn load_file(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)
.map_err(LoadingError::Parsing)?;
Ok(data)
}
/// 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) {
Ok(v) => v,
Err(e) => {
warn!("{:?}", e);
vec![]
}
}
}
/// Load all possible data files.
pub fn load_files() -> Vec<Card> {
glob("./data/*/en_us/data/*.json")
.unwrap()
.filter_map(GlobResult::ok)
.map(load_file_and_warn)
.concat()
}

View file

@ -1 +1,2 @@
pub mod schema; pub mod schema;
pub mod load;

View file

@ -8,6 +8,10 @@ mod data;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
let data = data::load::load_files();
debug!("{:?}", data);
info!("patched-porobot is starting..."); info!("patched-porobot is starting...");
let bot = Bot::from_env().auto_send(); let bot = Bot::from_env().auto_send();