1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use serde::{
    Deserialize,
    Serialize,
};
use std::collections::HashMap;
use url::Url;

/// A list of neko images
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ImageList {
    /// Images list
    pub images: Vec<Image>,

    /// Unknown data
    #[serde(flatten)]
    pub unknown: HashMap<String, serde_json::Value>,
}

/// Neko images
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Image {
    /// Image id
    pub id: String,
    /// Artist
    pub artist: Option<String>,
    /// Whether this is nsfw
    pub nsfw: bool,
    /// Tags
    pub tags: Vec<String>,
    /// # of likes
    pub likes: u64,
    /// # of favorites
    pub favorites: u64,
    /// The uploader
    pub uploader: ShortUser,
    /// The approver
    pub approver: Option<ShortUser>,
    /// Comments
    pub comments: Vec<serde_json::Value>,

    /// unknown
    #[serde(rename = "originalHash")]
    pub original_hash: String,

    /// created date
    #[serde(rename = "createdAt")]
    pub created_at: String,
}

impl Image {
    /// Get the url
    pub fn get_url(&self) -> Result<Url, url::ParseError> {
        let base = Url::parse("https://nekos.moe/image/").unwrap();
        base.join(&self.id)
    }
}

/// A user with only small amounts of info
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShortUser {
    /// User ID
    pub id: String,

    /// User name
    pub username: String,
}

#[cfg(test)]
mod test {
    use super::*;

    const RANDOM: &str = include_str!("../test_data/random.json");

    #[test]
    fn parse_image_list() {
        let image_list: ImageList = serde_json::from_str(RANDOM).expect("failed to parse");
        dbg!(image_list);
    }
}