jensmemesclient/cli/src/api.rs

93 lines
2.1 KiB
Rust
Raw Normal View History

use crate::util::IntoTableRow;
2020-12-05 22:20:08 +01:00
use anyhow::{anyhow, Result};
2020-12-05 12:08:24 +01:00
use serde::Deserialize;
2020-12-05 22:20:08 +01:00
use term_table::{row::Row, table_cell::TableCell};
2020-12-05 12:08:24 +01:00
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
2020-12-05 12:08:24 +01:00
pub struct UpResp {
pub files: Vec<String>,
}
2020-12-05 12:32:01 +01:00
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
2020-12-05 12:32:01 +01:00
pub struct CatsResp {
pub categories: Vec<Category>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
2020-12-19 19:43:23 +01:00
pub struct UsersResp {
pub users: Vec<User>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
2020-12-19 19:43:23 +01:00
pub struct MemesResp {
pub memes: Vec<Meme>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
2020-12-05 12:32:01 +01:00
pub struct Category {
pub id: String,
pub name: String,
}
2020-12-05 22:20:08 +01:00
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)]
2020-12-05 22:20:08 +01:00
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<'_> {
2020-12-05 22:20:08 +01:00
Row::new(vec![
TableCell::new(&self.link),
TableCell::new(&self.category),
TableCell::new(&self.user),
])
}
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
2020-12-19 19:43:23 +01:00
pub struct User {
pub name: String,
pub id: Option<String>,
pub tokenhash: Option<String>,
pub userdir: Option<String>,
pub dayuploads: String,
2020-12-05 22:20:08 +01:00
}
2020-12-19 19:43:23 +01:00
impl User {
pub fn get_id(&self) -> Option<&String> {
2020-12-20 20:52:22 +01:00
self.id
.as_ref()
2020-12-19 19:43:23 +01:00
.or(self.tokenhash.as_ref())
.or(self.userdir.as_ref())
}
}
impl IntoTableRow for User {
fn into_table_row(&self) -> Row<'_> {
2020-12-19 19:43:23 +01:00
Row::new(vec![
TableCell::new(&self.name),
TableCell::new(&self.get_id().map(String::as_ref).unwrap_or("[No ID]")),
TableCell::new(&self.dayuploads),
])
}
}