Clean up/fix error handling in main
This commit is contained in:
parent
2e942a9b31
commit
a168c00ee7
75
src/main.rs
75
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<AppState, Box<dyn Error>> {
|
||||
if !std::fs::exists(filename)? {
|
||||
return Ok(AppState::default());
|
||||
}
|
||||
|
||||
fn deserialize_savefile(
|
||||
filename: &String,
|
||||
) -> Result<AppState, serde_json::Error> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
let app_state: AppState =
|
||||
serde_json::from_str::<AppState>(&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<dyn Error>> {
|
||||
let serialized = serde_json::to_string_pretty(&app)?;
|
||||
Ok(std::fs::write(filename, serialized)?)
|
||||
}
|
||||
|
||||
async fn run(args: &Args) -> Result<(), Box<dyn Error>> {
|
||||
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) => {
|
||||
if let Err(e) = run(&args).await {
|
||||
error!("Application error: {}", e);
|
||||
print!("{e:?}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(args: &Args) -> Result<(), Box<dyn Error>> {
|
||||
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(())
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user