use clap::{App, Arg}; use lazy_static::lazy_static; use serde_json::from_reader; use std::error::Error; use std::fs::File; use std::io::BufReader; use std::path::Path; use std::process::exit; const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); lazy_static! { static ref COMMANDS: Vec<&'static str> = vec!["print", "searches", "favorites"]; } mod model; fn read_animeboxes_backup>( path: P, ) -> Result> { let file = File::open(path)?; let reader = BufReader::new(file); let result: model::anime_boxes::Backup = from_reader(reader)?; Ok(result) } fn searches_command(backup: model::anime_boxes::Backup) { let mut searches: Vec = backup .search_history .iter() .map(|s| String::from(&s.search_text)) .collect(); searches.sort(); for search in searches { println!("{}", search); } } fn favorites_command(backup: model::anime_boxes::Backup) { let mut favorites: Vec = backup .favorites .iter() .map(|f| String::from(&f.file.url)) .collect(); for favorite in favorites { println!("{}", favorite); } } fn main() { let matches = App::new("AnimeBoxes Sync") .version(VERSION.unwrap_or("UNKNOWN")) .author("Drew Short ") .about("Parses AnimeBoxes backup files") .arg( Arg::with_name("config") .short("c") .value_name("FILE") .help("Set a custom config file") .takes_value(true), ) .arg( Arg::with_name("INPUT") .help("The AnimeBoxes file to process") .required(true) .index(1), ) .arg( Arg::with_name("COMMAND") .help("The command to run on the backup") .required(true) .index(2) .possible_values(&COMMANDS), ) .get_matches(); let config = matches.value_of("config").unwrap_or("abs-default.conf"); let path = matches.value_of("INPUT").unwrap(); let result = read_animeboxes_backup(path).unwrap(); let command = matches.value_of("COMMAND").unwrap(); match command { "favorites" => { favorites_command(result); } "print" => println!("{:#?}", result), "searches" => { searches_command(result); } _ => { println!("{} is unrecognized", command); exit(1) } } }