jensmemesclient/cli/src/commands/list.rs
LordMZTE 8f429357d8
All checks were successful
continuous-integration/drone/push Build is passing
optimize listing (v0.1.2)
2020-12-29 14:19:51 +01:00

52 lines
1.1 KiB
Rust

use anyhow::Result;
use reqwest::Client;
use crate::util::{self, api, MemeSorting};
pub async fn run(
http: &Client,
cat: Option<String>,
user: Option<String>,
sorting: Option<MemeSorting>,
) -> Result<()> {
// This needs to be done so both users, memes and categories will be requested
// at once
let (memes, ..) = tokio::try_join!(
api::memes(
http,
cat.as_ref().map(String::as_ref),
user.as_ref().map(String::as_ref)
),
async {
if let Some(c) = cat.as_ref() {
util::assert_category_exists(http, c).await
} else {
Ok(())
}
},
async {
if let Some(u) = user.as_ref() {
util::assert_user_exists(http, u).await
} else {
Ok(())
}
},
)?;
let mut memes = memes.iter().collect::<Vec<_>>();
if let Some(s) = sorting {
s.sort_with(&mut memes);
}
let mut table = util::list_table();
for m in memes {
table.add_row(m.into());
}
println!("{}", table.render());
Ok(())
}