diff --git a/src/main.rs b/src/main.rs index ff83161..faa5075 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,49 +1,86 @@ use serde::Deserialize; -use std::{thread, time}; +use serde_json::Value; +use std::collections::HashMap; +use std::env; extern crate reqwest; -const API_URL: &str = "https://status.robertsspaceindustries.com/"; - -const STATUS_URL: &str = "https://status.robertsspaceindustries.com/static/content/api/v0/systems.en.json"; -const INCIDENTS_URL: &str = "https://status.robertsspaceindustries.com/static/content/api/v0/incidents/timeline.en.json"; - +const STATUS_URL: &str = + "https://status.robertsspaceindustries.com/static/content/api/v0/systems.en.json"; +const INCIDENTS_URL: &str = + "https://status.robertsspaceindustries.com/static/content/api/v0/incidents/timeline.en.json"; #[derive(Deserialize, Debug)] struct Status { name: String, status: String, order: u8, +} +#[derive(Deserialize, Debug)] +struct Incidents { + count: u16, + days: Vec>, } -fn get_incidents() -> Result<(),reqwest::Error> { - // TODO add parsing for the data this returns +fn get_incidents() -> Result<(), reqwest::Error> { let res = reqwest::blocking::get(INCIDENTS_URL)?; - let data = res.text()?; - // let pu = &data[1]; - // let ea = &data[2]; - //println!("Persistent Universe: {}\nEA: {}", pu.status, ea.status); - dbg!(data); + let data: Value = res.json()?; + let mut day; + for i in 0..5 { + day = &data["days"][i]; + let date = day["date"] + .as_str() + .unwrap() + .split("T") + .collect::>()[0]; + let incidents = day["incidents"].as_array(); + println!("Date: {}", date); + println!("Number of incidents: {}", day["count"]); + for incident in incidents { + for inc in incident { + println!("Title: {}", inc["title"]); + println!("Severity: {}", inc["severity"]); + println!("Resolved: {}", inc["resolved"]); + println!("Content: {}\n", inc["content"]); + } + } + println!("\n"); + } + Ok(()) } -fn get_status() -> Result<(),reqwest::Error> { +fn get_status() -> Result<(), reqwest::Error> { let res = reqwest::blocking::get(STATUS_URL)?; let data = res.json::>()?; let pu = &data[1]; let ea = &data[2]; - println!("Persistent Universe: {}\nEA: {}", pu.status, ea.status); + println!("Persistent Universe: {}\nEA: {}", pu.status, ea.status); Ok(()) } +fn help() { + println!( + "sc_status conveniently checks the Star Citizen Status API.\n\ + Call with '-i' to get detailed information about incidents" + ); +} + fn main() { - match get_status(){ - Err(e) => println!("{:?}", e), - _ => () + let args: Vec = env::args().collect(); + match args.len() { + 1 => match get_status() { + Err(e) => println!("{:?}", e), + _ => (), + }, + 2 => match args[1].as_ref() { + "-i" => match get_incidents() { + Err(e) => println!("{:?}", e), + _ => (), + }, + _ => help(), + }, + _ => help(), } - // match get_incidents(){ - // Err(e) => println!("{:?}", e), - // _ => () - // } }