add list user and cat verification and optimization
This commit is contained in:
parent
76fb754e53
commit
709e10111c
|
@ -2,33 +2,33 @@ use anyhow::{anyhow, Result};
|
|||
use serde::Deserialize;
|
||||
use term_table::{row::Row, table_cell::TableCell};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||
pub struct UpResp {
|
||||
pub files: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||
pub struct CatsResp {
|
||||
pub categories: Vec<Category>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||
pub struct UsersResp {
|
||||
pub users: Vec<User>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||
pub struct MemesResp {
|
||||
pub memes: Vec<Meme>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||
pub struct Category {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||
pub struct Meme {
|
||||
pub id: String,
|
||||
pub link: String,
|
||||
|
@ -56,7 +56,7 @@ impl Into<Row<'_>> for &Meme {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Debug, PartialEq, Eq)]
|
||||
pub struct User {
|
||||
pub name: String,
|
||||
pub id: Option<String>,
|
||||
|
|
|
@ -9,7 +9,34 @@ pub async fn run(
|
|||
user: Option<String>,
|
||||
sorting: Option<MemeSorting>,
|
||||
) -> Result<()> {
|
||||
let memes = api::memes(http).await?;
|
||||
let mut cat_check_fut = None;
|
||||
let mut usr_check_fut = None;
|
||||
if let Some(c) = cat.as_ref() {
|
||||
cat_check_fut = Some(util::assert_category_exists(http, c));
|
||||
}
|
||||
|
||||
if let Some(u) = user.as_ref() {
|
||||
usr_check_fut = Some(util::assert_user_exists(http, u));
|
||||
}
|
||||
|
||||
// This needs to be done so both users, memes and categories will be requested at once
|
||||
let (_, _, memes) = tokio::try_join!(
|
||||
async {
|
||||
if let Some(f) = cat_check_fut {
|
||||
f.await
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
async {
|
||||
if let Some(f) = usr_check_fut {
|
||||
f.await
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
api::memes(http),
|
||||
)?;
|
||||
|
||||
let memes = memes
|
||||
.into_iter()
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use crate::api::Meme;
|
||||
use anyhow::bail;
|
||||
use reqwest::Client;
|
||||
use std::str::FromStr;
|
||||
use term_table::{Table, TableBuilder, TableStyle};
|
||||
use tokio::process::Command;
|
||||
|
@ -17,6 +18,11 @@ macro_rules! init_once_cell {
|
|||
};
|
||||
}
|
||||
|
||||
pub mod consts {
|
||||
pub const NO_SUCH_CATEGORY_ERROR: &str = "The given Category does not exist!";
|
||||
pub const NO_SUCH_USER_ERROR: &str = "The given User does not exist!";
|
||||
}
|
||||
|
||||
/// ways to communicyte with the JM API
|
||||
pub mod api {
|
||||
use crate::api::{CatsResp, Meme, MemesResp, User, UsersResp};
|
||||
|
@ -100,7 +106,11 @@ impl MemeSorting {
|
|||
pub fn sort_with(&self, memes: &mut [&Meme]) {
|
||||
macro_rules! sort {
|
||||
($list:ident, $field:ident) => {
|
||||
$list.sort_by(|a, b| a.$field.to_ascii_lowercase().cmp(&b.$field.to_ascii_lowercase()));
|
||||
$list.sort_by(|a, b| {
|
||||
a.$field
|
||||
.to_ascii_lowercase()
|
||||
.cmp(&b.$field.to_ascii_lowercase())
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -127,3 +137,15 @@ impl FromStr for MemeSorting {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn assert_user_exists(http: &Client, user: &str) -> anyhow::Result<()> {
|
||||
if !api::users(http).await?.iter().any(|u| u.name == user) {
|
||||
bail!(consts::NO_SUCH_USER_ERROR);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub async fn assert_category_exists(http: &Client, cat: &str) -> anyhow::Result<()> {
|
||||
if !api::cats(http).await?.iter().any(|c| c == cat) {
|
||||
bail!(consts::NO_SUCH_CATEGORY_ERROR);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue