use log::{error, info}; use serde_json; mod app; mod ui; use crate::app::App; mod snowballing; // 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 mut app: App = serde_json::from_str(&content)?; app.should_quit = false; Ok(app) } Err(_) => { let app = App::default(); if let Ok(serialized) = serde_json::to_string_pretty(&app) { let _ = std::fs::write(filename, serialized); } Ok(app) } } } fn serialize_savefile( app: &App, 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"); } 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)] struct Args { #[arg(short, long)] savefile: String, #[arg(short, long, default_value = "/tmp/snowballing.log")] logfile: String, } #[tokio::main] async fn main() { let args = Args::parse(); if env::var("RUST_LOG").is_err() { unsafe { env::set_var("RUST_LOG", "info") } } env_logger::Builder::from_default_env() .format_module_path(false) .target(env_logger::Target::Pipe(Box::new( OpenOptions::new() .create(true) .append(true) .open(&args.logfile) .unwrap(), ))) .init(); match run(&args).await { Ok(()) => info!("Application completed successfully"), Err(e) => { 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(()) }