diff --git a/src/main.rs b/src/main.rs index 5f65abf..26b2579 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,64 +1,43 @@ mod app; +mod crossterm; mod literature; mod ui; +use clap::Parser; use log::error; use serde_json; +use std::{env, error::Error, fs::OpenOptions}; use crate::app::AppState; -// use crate::snowballing::get_citing_papers; -// #[tokio::main] -// async fn main() -> Result<(), reqwest::Error> { -// let publications = get_citing_papers("w2963127785", "an.tsouchlos@gmail.com").await?; -// -// let app = App { -// pending_publications: publications, -// ..Default::default() -// }; -// -// if let Ok(serialized) = serde_json::to_string_pretty(&app) { -// std::fs::write("temp.json", serialized) -// .expect("We can't really deal with io errors ourselves"); -// } -// -// Ok(()) -// } - -fn deserialize_savefile( - filename: &String, -) -> Result { - match std::fs::read_to_string(filename) { - Ok(content) => { - let app: AppState = serde_json::from_str(&content)?; - Ok(app) - } - Err(_) => { - let app = AppState::default(); - if let Ok(serialized) = serde_json::to_string_pretty(&app) { - let _ = std::fs::write(filename, serialized); - } - Ok(app) - } +fn deserialize_savefile(filename: &String) -> Result> { + if !std::fs::exists(filename)? { + return Ok(AppState::default()); } + + let app_state: AppState = + serde_json::from_str::(&std::fs::read_to_string(filename)?)?; + + Ok(app_state) } fn serialize_savefile( app: &AppState, filename: &String, -) -> Result<(), serde_json::Error> { - if let Ok(serialized) = serde_json::to_string_pretty(&app) { - std::fs::write(filename, serialized) - .expect("We can't really deal with io errors ourselves"); - } +) -> Result<(), Box> { + let serialized = serde_json::to_string_pretty(&app)?; + Ok(std::fs::write(filename, serialized)?) +} + +async fn run(args: &Args) -> Result<(), Box> { + let starting_app_state = deserialize_savefile(&args.savefile)?; + let final_app_state = crate::crossterm::run(starting_app_state).await?; + + serialize_savefile(&final_app_state, &args.savefile)?; Ok(()) } -use clap::Parser; -mod crossterm; -use std::{env, error::Error, fs::OpenOptions}; - #[derive(Parser)] #[command(name = "Brittling")] #[command(about = "A tool to perform snowballing for literature studies", long_about = None)] @@ -89,21 +68,9 @@ async fn main() { ))) .init(); - match run(&args).await { - Err(e) => { - error!("Application error: {}", e); - print!("{e:?}"); - std::process::exit(1); - } - _ => {} + if let Err(e) = run(&args).await { + error!("Application error: {}", e); + print!("{e:?}"); + std::process::exit(1); } } - -async fn run(args: &Args) -> Result<(), Box> { - let starting_app_state = deserialize_savefile(&args.savefile)?; - let final_app_state = crate::crossterm::run(starting_app_state).await?; - - serialize_savefile(&final_app_state, &args.savefile)?; - - Ok(()) -}