use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::{cmp::Ordering, fmt::Debug, ops::Not};
use crate::ImageBoards;
use self::{extension::Extension, rating::Rating, tags::Tag};
pub mod error;
pub mod extension;
pub mod rating;
pub mod tags;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NameType {
ID,
MD5,
}
impl Not for NameType {
type Output = Self;
fn not(self) -> Self::Output {
match self {
Self::ID => Self::MD5,
Self::MD5 => Self::ID,
}
}
}
#[derive(Debug)]
pub struct PostQueue {
pub imageboard: ImageBoards,
pub client: Client,
pub posts: Vec<Post>,
pub tags: Vec<String>,
}
impl PostQueue {
pub fn prepare(&mut self, limit: Option<u16>) {
if let Some(max) = limit {
self.posts.truncate(max as usize);
} else {
self.posts.shrink_to_fit()
}
}
}
#[derive(Clone, Serialize, Deserialize, Eq)]
pub struct Post {
pub id: u64,
pub website: ImageBoards,
pub url: String,
pub md5: String,
pub extension: Extension,
pub rating: Rating,
pub tags: Vec<Tag>,
}
impl Debug for Post {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Post")
.field("Post ID", &self.id)
.field("Website", &self.website)
.field("Download URL", &self.url)
.field("MD5 Hash", &self.md5)
.field("File Extension", &self.extension)
.field("Rating", &self.rating)
.field("Tag List", &self.tags)
.finish()
}
}
impl Ord for Post {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
impl PartialOrd for Post {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Post {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Post {
#[inline]
pub fn file_name(&self, name_type: NameType) -> String {
let name = match name_type {
NameType::ID => self.id.to_string(),
NameType::MD5 => self.md5.to_string(),
};
format!("{}.{}", name, self.extension)
}
#[inline]
pub fn name(&self, name_type: NameType) -> String {
match name_type {
NameType::ID => self.id.to_string(),
NameType::MD5 => self.md5.to_string(),
}
}
#[inline]
pub fn seq_file_name(&self, num_digits: usize) -> String {
format!("{:0num_digits$}.{}", self.id, self.extension)
}
}