rusty_booru/shared/
mod.rs1use crate::danbooru::client::DanbooruError;
2
3use self::client::{ClientInformation, ClientTypes};
4use derive_is_enum_variant::is_enum_variant;
5use itertools::Itertools;
6use std::fmt::Display;
7use strum::Display;
8
9pub mod client;
10
11#[derive(derive_more::From, Debug, thiserror::Error, Display)]
12pub enum Error {
13 #[error(transparent)]
14 Reqwest(reqwest::Error),
15
16 #[error(transparent)]
17 Danbooru(DanbooruError),
18
19 Unexpected,
20}
21
22#[derive(Debug, Clone, Display)]
23#[strum(serialize_all = "lowercase")]
24pub enum Sort {
25 Id,
26 Score,
27 Rating,
28 User,
29 Height,
30 Width,
31 Source,
32 Updated,
33 Random,
34}
35
36#[derive(is_enum_variant, Debug, Clone)]
37pub enum Tag<T: ClientTypes> {
38 Plain(String),
39 Blacklist(String),
40 Rating(T::Rating),
41 Sort(Sort),
42}
43
44impl<T: ClientInformation + ClientTypes> Display for Tag<T> {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 match self {
47 Tag::Plain(tag) => write!(f, "{}", tag),
48 Tag::Blacklist(tag) => write!(f, "-{}", tag),
49 Tag::Rating(tag) => write!(f, "rating:{}", tag),
50 Tag::Sort(by) => write!(f, "{}:{}", T::SORT, by),
51 }
52 }
53}
54
55#[derive(Debug, Clone)]
56pub struct Tags<T: ClientTypes>(pub Vec<Tag<T>>);
57
58impl<T: ClientTypes + ClientInformation> Tags<T> {
59 pub fn unpack(&self) -> String {
60 self.0
61 .iter()
62 .map(ToString::to_string)
63 .collect_vec()
64 .join(" ")
65 }
66}