jensmemesclient/cli/src/commands/list.rs

52 lines
1.1 KiB
Rust
Raw Normal View History

2020-12-05 22:20:08 +01:00
use anyhow::Result;
2020-12-05 22:21:00 +01:00
use reqwest::Client;
2020-12-05 22:20:08 +01:00
2020-12-20 20:52:22 +01:00
use crate::util::{self, api, MemeSorting};
2020-12-05 22:20:08 +01:00
2020-12-20 20:52:22 +01:00
pub async fn run(
http: &Client,
cat: Option<String>,
user: Option<String>,
sorting: Option<MemeSorting>,
) -> Result<()> {
2020-12-21 23:06:50 +01:00
// This needs to be done so both users, memes and categories will be requested
// at once
let (memes, ..) = tokio::try_join!(
2020-12-29 14:19:51 +01:00
api::memes(
http,
cat.as_ref().map(String::as_ref),
user.as_ref().map(String::as_ref)
),
async {
2020-12-21 23:06:50 +01:00
if let Some(c) = cat.as_ref() {
util::assert_category_exists(http, c).await
} else {
Ok(())
}
},
async {
2020-12-21 23:06:50 +01:00
if let Some(u) = user.as_ref() {
util::assert_user_exists(http, u).await
} else {
Ok(())
}
},
)?;
2020-12-05 22:20:08 +01:00
2020-12-29 14:19:51 +01:00
let mut memes = memes.iter().collect::<Vec<_>>();
2020-12-20 20:52:22 +01:00
if let Some(s) = sorting {
s.sort_with(&mut memes);
}
2020-12-05 22:20:08 +01:00
let mut table = util::list_table();
2020-12-18 16:58:53 +01:00
2020-12-05 22:20:08 +01:00
for m in memes {
table.add_row(m.into());
}
println!("{}", table.render());
Ok(())
2020-12-05 22:21:00 +01:00
}