Respect --json in mensa canteens

See #4.
This commit is contained in:
Malte Tammena 2021-10-20 20:45:07 +02:00
parent ab9bd9801c
commit 9158875169
3 changed files with 16 additions and 11 deletions

View file

@ -1,6 +1,6 @@
use chrono::Duration;
use reqwest::blocking::Client;
use serde::{de::DeserializeOwned, Deserialize};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tracing::info;
use std::marker::PhantomData;
@ -9,18 +9,18 @@ use crate::{
cache,
config::CanteensState,
error::{Error, Result},
geoip, get_sane_terminal_dimensions, ENDPOINT, TTL_CANTEENS,
geoip, get_sane_terminal_dimensions, print_json, ENDPOINT, TTL_CANTEENS,
};
const ADRESS_INDENT: &str = " ";
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Canteen {
id: usize,
name: String,
//city: String,
city: String,
address: String,
//oordinates: [f32; 2],
coordinates: [f32; 2],
}
impl Canteen {
@ -58,10 +58,15 @@ impl Canteen {
PaginatedList::from(&state.client, url, *TTL_CANTEENS)?.try_flatten_and_collect()
}
pub fn print_all(state: &CanteensState, canteens: &[Self]) {
for canteen in canteens {
println!();
canteen.print(state);
pub fn print_all(state: &CanteensState, canteens: &[Self]) -> Result<()> {
if state.args.json {
print_json(&canteens)
} else {
for canteen in canteens {
println!();
canteen.print(state);
}
Ok(())
}
}
}

View file

@ -40,7 +40,7 @@ pub struct Args {
)]
pub color: ColorWhen,
/// Print plain json. Useful for shell scripts.
/// Output json. Useful for shell scripts.
#[structopt(long, global = true, takes_value = false)]
pub json: bool,

View file

@ -167,7 +167,7 @@ fn real_main() -> Result<()> {
Some(Command::Canteens(cmd)) => {
let state = State::from(state, cmd);
let canteens = Canteen::fetch(&state)?;
Canteen::print_all(&state, &canteens);
Canteen::print_all(&state, &canteens)?;
}
Some(Command::Tags) => {
Tag::print_all(&state)?;