Skip to main content

weatherapi_rs/
lib.rs

1pub mod files;
2
3use files::{get_config, output_compact, Cli, Format, Weather};
4
5pub async fn run(args: Cli) -> Result<String, Box<dyn std::error::Error>> {
6    let client = reqwest::Client::new();
7    let config = get_config();
8    let mut query = Vec::new();
9
10    if args.aqi {
11        query.push(("aqi", String::from("yes")));
12    }
13
14    match args.q {
15        Some(q) => {
16            query.push(("q", q));
17        }
18        None => {
19            return Err("City name is required".into());
20        }
21    }
22
23    let res: Weather = client
24        .get("https://weatherapi-com.p.rapidapi.com/current.json")
25        .header("x-rapidapi-key", config.api_key)
26        .header("x-rapidapi-host", "weatherapi-com.p.rapidapi.com")
27        .query(&query)
28        .send()
29        .await?
30        .json()
31        .await?;
32
33    match args.format {
34        Format::Json => Ok(serde_json::to_string(&res)?),
35        Format::Compact => Ok(output_compact(&res)),
36    }
37}