This commit is contained in:
Anthony Cicchetti 2022-03-08 23:09:17 -05:00
parent d56b9d4952
commit 32f6e72dec

View file

@ -1,7 +1,9 @@
use std::collections::HashMap;
use eyre::eyre;
use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
use eyre::{eyre};
use std::collections::HashMap;
use tokio::fs;
use tokio::sync::mpsc;
#[derive(Deserialize, Debug, Serialize, Copy, Clone)]
#[serde(rename_all = "lowercase")]
@ -45,7 +47,9 @@ impl TryFrom<Monster> for ValidMonster {
Ok(ValidMonster {
id: value.id,
name: value.name.clone(),
health: value.health.ok_or(eyre!("Couldn't unwrap health for {}", value.name))?,
health: value
.health
.ok_or(eyre!("Couldn't unwrap health for {}", value.name))?,
attributes: value.attributes,
attack_level: value.attack_level,
strength_level: value.strength_level,
@ -80,18 +84,16 @@ struct Monster {
defence_ranged: i32,
}
use tokio::fs;
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> eyre::Result<()> {
let pool = SqlitePool::connect("sqlite:monsters.db").await?;
let f = fs::read_to_string("monsters-complete.json").await?;
let a= serde_json::from_str::<HashMap<usize, Monster>>(&f)?;
let a = serde_json::from_str::<HashMap<usize, Monster>>(&f)?;
let validmonsters: Vec<ValidMonster> = a.into_iter()
.filter_map(|(_, monster)| {monster.try_into().ok()})
let validmonsters: Vec<ValidMonster> = a
.into_iter()
.filter_map(|(_, monster)| monster.try_into().ok())
.collect();
println!("Found {} valid monsters", validmonsters.len());
@ -100,8 +102,15 @@ async fn main() -> eyre::Result<()> {
validmonsters.into_iter().for_each(|monster| {
let sender = tx.clone();
match sender.send(monster.clone()) {
Ok(_) => {println!("Sent {} successfully", &monster.name)}
Err(_) => {eprintln!("{}", eyre::eyre!("Couldn't send {} successfully", &monster.name))}
Ok(_) => {
println!("Sent {} successfully", &monster.name)
}
Err(_) => {
eprintln!(
"{}",
eyre::eyre!("Couldn't send {} successfully", &monster.name)
)
}
};
});
@ -110,7 +119,7 @@ async fn main() -> eyre::Result<()> {
while let Some(monster) = rx.recv().await {
println!("Writing {}", &monster.name);
add_monster(&pool, &monster).await?;
};
}
Ok(())
}
@ -160,4 +169,4 @@ DO UPDATE SET
.await?;
Ok(())
}
}