rusty_booru/gelbooru/
mod.rs

1pub mod client;
2
3use derive_more::From;
4use serde::{Deserialize, Serialize};
5use strum::Display;
6
7use crate::generic::{BooruPost, Rating};
8
9/// Individual post from [`GelbooruResponse`]
10#[derive(Serialize, Deserialize, Debug, Clone)]
11pub struct GelbooruPost {
12    /// The ID of the post
13    pub id: u32,
14    /// Datestamp of the post's creating date
15    pub created_at: String,
16    /// Post's score
17    pub score: u32,
18    /// Post's image width
19    pub width: u32,
20    /// Post's image height
21    pub height: u32,
22    /// Post's image md5
23    pub md5: String,
24    /// Post's image file url
25    pub file_url: String,
26    /// Post's tags
27    pub tags: String,
28    /// Post's image name (with extension)
29    pub image: String,
30    /// Post's image source
31    pub source: String,
32    /// Post's rating
33    pub rating: GelbooruRating,
34}
35
36/// Gelbooru's API response with a list a posts
37#[derive(Serialize, Deserialize, Debug, Clone)]
38pub struct GelbooruResponse {
39    #[serde(rename = "post")]
40    pub posts: Vec<GelbooruPost>,
41}
42
43/// Post's rating. Check the [Gelbooru's ratings wiki](https://gelbooru.com/index.php?page=help&topic=rating)
44#[derive(Serialize, Deserialize, Debug, Clone, Display, From)]
45#[strum(serialize_all = "lowercase")]
46#[serde(rename_all = "lowercase")]
47pub enum GelbooruRating {
48    Explicit,
49    Questionable,
50    Safe,
51    Sensitive,
52    General,
53}
54
55impl From<Rating> for GelbooruRating {
56    fn from(value: Rating) -> Self {
57        match value {
58            Rating::Explicit => Self::Explicit,
59            Rating::Questionable => Self::Questionable,
60            Rating::Safe => Self::Safe,
61            Rating::Sensitive => Self::Sensitive,
62            Rating::General => Self::General,
63        }
64    }
65}
66
67impl From<GelbooruRating> for Rating {
68    fn from(value: GelbooruRating) -> Self {
69        match value {
70            GelbooruRating::Explicit => Rating::Explicit,
71            GelbooruRating::Questionable => Rating::Questionable,
72            GelbooruRating::Safe => Rating::Safe,
73            GelbooruRating::Sensitive => Rating::Sensitive,
74            GelbooruRating::General => Rating::General,
75        }
76    }
77}
78
79impl From<GelbooruPost> for BooruPost {
80    fn from(post: GelbooruPost) -> Self {
81        BooruPost {
82            id: post.id,
83            created_at: post.created_at.into(),
84            score: post.score.into(),
85            width: post.width,
86            height: post.height,
87            md5: post.md5.into(),
88            file_url: post.file_url.into(),
89            tags: post.tags,
90            image: post.image.into(),
91            source: post.source.into(),
92            rating: post.rating.into(),
93        }
94    }
95}