Clean up/fix error handling in main
This commit is contained in:
parent
2e942a9b31
commit
a168c00ee7
77
src/main.rs
77
src/main.rs
@ -1,64 +1,43 @@
|
|||||||
mod app;
|
mod app;
|
||||||
|
mod crossterm;
|
||||||
mod literature;
|
mod literature;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
use log::error;
|
use log::error;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
use std::{env, error::Error, fs::OpenOptions};
|
||||||
|
|
||||||
use crate::app::AppState;
|
use crate::app::AppState;
|
||||||
|
|
||||||
// use crate::snowballing::get_citing_papers;
|
fn deserialize_savefile(filename: &String) -> Result<AppState, Box<dyn Error>> {
|
||||||
// #[tokio::main]
|
if !std::fs::exists(filename)? {
|
||||||
// async fn main() -> Result<(), reqwest::Error> {
|
return Ok(AppState::default());
|
||||||
// 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(
|
let app_state: AppState =
|
||||||
filename: &String,
|
serde_json::from_str::<AppState>(&std::fs::read_to_string(filename)?)?;
|
||||||
) -> Result<AppState, serde_json::Error> {
|
|
||||||
match std::fs::read_to_string(filename) {
|
Ok(app_state)
|
||||||
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 serialize_savefile(
|
fn serialize_savefile(
|
||||||
app: &AppState,
|
app: &AppState,
|
||||||
filename: &String,
|
filename: &String,
|
||||||
) -> Result<(), serde_json::Error> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
if let Ok(serialized) = serde_json::to_string_pretty(&app) {
|
let serialized = serde_json::to_string_pretty(&app)?;
|
||||||
std::fs::write(filename, serialized)
|
Ok(std::fs::write(filename, serialized)?)
|
||||||
.expect("We can't really deal with io errors ourselves");
|
}
|
||||||
}
|
|
||||||
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
use clap::Parser;
|
|
||||||
mod crossterm;
|
|
||||||
use std::{env, error::Error, fs::OpenOptions};
|
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(name = "Brittling")]
|
#[command(name = "Brittling")]
|
||||||
#[command(about = "A tool to perform snowballing for literature studies", long_about = None)]
|
#[command(about = "A tool to perform snowballing for literature studies", long_about = None)]
|
||||||
@ -89,21 +68,9 @@ async fn main() {
|
|||||||
)))
|
)))
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
match run(&args).await {
|
if let Err(e) = run(&args).await {
|
||||||
Err(e) => {
|
|
||||||
error!("Application error: {}", e);
|
error!("Application error: {}", e);
|
||||||
print!("{e:?}");
|
print!("{e:?}");
|
||||||
std::process::exit(1);
|
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