Add simple formatting

This commit is contained in:
Stan Drozd 2019-02-15 23:44:43 +01:00
parent 584d38761a
commit 43132a745d
No known key found for this signature in database
GPG key ID: EB6D6A3C40C7521D
3 changed files with 1410 additions and 2 deletions

1336
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -5,3 +5,9 @@ authors = ["Stan Drozd <drozdziak1@gmail.com>"]
edition = "2018"
[dependencies]
reqwest = "0.9.9"
failure = "0.1.5"
serde_derive = "1.0.87"
serde = "1.0.87"
serde_json = "1.0.38"
colored = "1.7.0"

View file

@ -1,3 +1,69 @@
fn main() {
println!("Hello, world!");
#[macro_use]
extern crate failure;
#[macro_use]
extern crate serde_derive;
use colored::*;
use failure::Error;
use serde::{Deserialize, Deserializer};
fn f64_to_u64_helper<'de, D: Deserializer<'de>>(d: D) -> Result<u64, D::Error> {
Ok(f64::deserialize(d)? as u64)
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct HsMember {
#[serde(deserialize_with = "f64_to_u64_helper")]
timestamp: u64,
login: String,
pretty_time: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Response {
pub kektops: u16,
pub unknown: u16,
pub esps: u16,
pub users: Vec<HsMember>,
pub vms: u16,
}
fn main() -> Result<(), Error> {
let res = reqwest::get("https://at.hackerspace.pl/api")?.text()?;
let parsed: Response = serde_json::from_str(&res)?;
let logins: Vec<&str> = parsed
.users
.iter()
.map(|member| member.login.as_str())
.collect();
let members_pretty = if logins.len() > 0 {
logins.as_slice().join(" | ")
} else {
"no-one".to_string()
};
if parsed.users.len() > 0 {
println!("{}: {}", "Members".green(), members_pretty);
}
if parsed.unknown > 0 {
println!("{}: {}", "Anons".yellow(), parsed.unknown);
}
if parsed.kektops > 0 {
println!("{}: {}", "Kektops".blue(), parsed.kektops);
}
if parsed.esps > 0 {
println!("{}: {}", "ESPs".blue(), parsed.esps);
}
if parsed.vms > 0 {
println!("{}: {}", "VMs".blue(), parsed.vms);
}
Ok(())
}