jensmemesclient/cli/src/api.rs
LordMZTE bb33d3b015
All checks were successful
continuous-integration/drone/push Build is passing
category list improvements and cleanups
2021-01-05 15:13:19 +01:00

92 lines
2.1 KiB
Rust

use crate::util::IntoTableRow;
use anyhow::{anyhow, Result};
use serde::Deserialize;
use term_table::{row::Row, table_cell::TableCell};
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct UpResp {
pub files: Vec<String>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct CatsResp {
pub categories: Vec<Category>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct UsersResp {
pub users: Vec<User>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct MemesResp {
pub memes: Vec<Meme>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Category {
pub id: String,
pub name: String,
}
impl IntoTableRow for Category {
fn into_table_row(&self) -> Row<'_> {
Row::new(vec![TableCell::new(&self.id), TableCell::new(&self.name)])
}
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Meme {
pub id: String,
pub link: String,
pub path: String,
pub category: String,
pub user: String,
}
impl Meme {
pub fn file_name(&self) -> Result<&str> {
self.path
.split('/')
.last()
.ok_or_else(|| anyhow!("failed to get file name. server response invalid"))
}
}
impl IntoTableRow for Meme {
fn into_table_row(&self) -> Row<'_> {
Row::new(vec![
TableCell::new(&self.link),
TableCell::new(&self.category),
TableCell::new(&self.user),
])
}
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct User {
pub name: String,
pub id: Option<String>,
pub tokenhash: Option<String>,
pub userdir: Option<String>,
pub dayuploads: String,
}
impl User {
pub fn get_id(&self) -> Option<&String> {
self.id
.as_ref()
.or(self.tokenhash.as_ref())
.or(self.userdir.as_ref())
}
}
impl IntoTableRow for User {
fn into_table_row(&self) -> Row<'_> {
Row::new(vec![
TableCell::new(&self.name),
TableCell::new(&self.get_id().map(String::as_ref).unwrap_or("[No ID]")),
TableCell::new(&self.dayuploads),
])
}
}