1use clap::{App, Arg};
2use std::error::Error;
3mod print;
4use print::*;
5mod location;
6use location::*;
7mod forecast;
8use forecast::*;
9
10type WeatherResult<T> = Result<T, Box<dyn Error>>;
11pub struct Config {
12 celsius: bool,
13 hours: usize,
14 color: bool,
15}
16
17pub fn run(config: Config) -> WeatherResult<()> {
18 let location = get_data_from_ip()?;
19 let hourly_forecast = get_hourly_forecast(&location.latitude, &location.longitude)?;
20 if config.color {
21 print_hourly_forecast_colored(location, hourly_forecast, config)?;
22 } else {
23 print_hourly_forecast(location, hourly_forecast, config)?;
24 }
25
26 Ok(())
27}
28
29pub fn get_args() -> WeatherResult<Config> {
30 let matches = App::new("yawcli")
31 .version("0.2.2")
32 .author("John Bullard <johnbcooding@gmail.com>")
33 .about("Uses your IP to get the local forecast, only works in USA.")
34 .arg(
35 Arg::with_name("celsius")
36 .short("c")
37 .long("celsius")
38 .help("Converts temperature to celsius.")
39 .takes_value(false),
40 )
41 .arg(
42 Arg::with_name("hours")
43 .long("hours")
44 .help("How many hours to show in hourly forecast, max 24.")
45 .default_value("1")
46 .takes_value(true),
47 )
48 .arg(
49 Arg::with_name("color")
50 .long("color")
51 .help("Prints out the forecast in color")
52 .takes_value(false),
53 )
54 .get_matches();
55
56 let hours = matches.value_of_lossy("hours").unwrap();
57
58 Ok(Config {
59 celsius: matches.is_present("celsius"),
60 hours: hours.parse::<usize>()?,
61 color: matches.is_present("color"),
62 })
63}