mod api; mod cmd; mod config; use api::API; use chrono::{NaiveDateTime, ParseError}; use cmd::*; use config::Config; use std::fs::File; use std::io::prelude::*; fn main() { let env: EnvOpt = argh::from_env(); match env.subcommand { Subcommand::Run(opt) => { let mut file = match File::open("config.toml") { Ok(file) => file, Err(e) => { eprintln!("Could not read config file: {}\nMake sure one exists with init subcommand.", e); return; } }; let mut string = String::new(); if let Err(e) = file.read_to_string(&mut string) { eprintln!("Config file data not valid UTF-8: {}", e); return; } let config: Config = match toml::from_str(&string) { Ok(config) => config, Err(e) => { eprintln!("Config file could not be parsed: {}", e); return; } }; run(&config, None, None).ok(); } Subcommand::Init(opt) => { let config = Config::default(); let mut file = File::create("config.toml").unwrap(); if let Err(e) = file.write_all(&toml::to_vec(&config).unwrap()) { eprintln!("Could not write to file: {}", e); } } } } fn run(config: &Config, from: Option, to: Option) -> Result<(), ParseError> { let mut api = API::new(config.clone()); let from = get_opt(from.map(|f| NaiveDateTime::parse_from_str(&f, &config.between_format)))?; let to = get_opt(to.map(|t| NaiveDateTime::parse_from_str(&t, &config.between_format)))?; let tags = api.get_tags().unwrap(); let first_tag = tags[0].id.clone().unwrap(); let state_list = api.get_states(&first_tag).unwrap(); let states = API::get_between(state_list.clone(), from, to, true, &config); let len = states.len(); let mut locations = Vec::new(); for (idx, (_, state)) in states.iter().enumerate() { println!("Expected {}s left", exp_time(&api, (len - idx) as u32)); let mut location_list = api .get_locations(&first_tag, state.id.as_ref().unwrap()) .unwrap(); locations.append(&mut location_list); } dbg!(API::get_between(locations, from, to, false, &config)); Ok(()) } fn get_opt(option: Option>) -> Result, B> { if let Some(res) = option { match res { Ok(a) => Ok(Some(a)), Err(b) => Err(b), } } else { Ok(None) } } fn exp_time(api: &API, reqs_left: u32) -> f32 { (reqs_left * api.last_response_ping) as f32 / 1000. }