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, } #[derive(Deserialize, Debug, PartialEq, Eq, Clone)] pub struct CatsResp { pub categories: Vec, } #[derive(Deserialize, Debug, PartialEq, Eq, Clone)] pub struct UsersResp { pub users: Vec, } #[derive(Deserialize, Debug, PartialEq, Eq, Clone)] pub struct MemesResp { pub memes: Vec, } #[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, pub tokenhash: Option, pub userdir: Option, 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), ]) } }