52 lines
1.1 KiB
Rust
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(())
|
|
}
|