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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use core::fmt;

use chrono::NaiveDate;
use serde::Deserialize;
use serde_json::Value;

#[derive(Debug, Deserialize, Clone)]
pub struct AnimeList {
    id: i64,
    title: String,
    status: String,
    finish_date: String,
}

impl AnimeList {
    pub fn new(value: &Value) -> Self {
        // parse node
        let (id, title) = Self::parse_node(value);
        let (status, finish_date) = Self::parse_status(value);
        Self {
            id: id.unwrap(),
            title: title.unwrap().to_string(),
            status: status.unwrap().to_string(),
            finish_date: finish_date.unwrap().to_string(),
        }
        // todo!()
    }

    fn parse_node(value: &Value) -> (Option<i64>, Option<&str>) {
        let node = value.get("node");
        match node {
            Some(anime) => (
                anime.get("id").unwrap().as_i64(),
                anime.get("title").unwrap().as_str(),
            ),
            _ => (None, None),
        }
    }

    fn parse_status(value: &Value) -> (Option<&str>, Option<&str>) {
        let status = value.get("list_status");
        match status {
            Some(anime_status) => (
                anime_status.get("status").unwrap().as_str(),
                if let Some(date) = anime_status.get("finish_date") {
                    date.as_str()
                } else {
                    Some("-")
                },
            ),
            _ => (None, None),
        }
    }

    pub fn get_title(self) -> String {
        self.title
    }

    pub fn get_id(&self) -> i64 {
        self.id
    }

    pub fn get_status(self) -> String {
        self.status
    }
}

impl fmt::Display for AnimeList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut finish_date = "-".to_string();
        if self.finish_date != "-" {
            finish_date = NaiveDate::parse_from_str(self.finish_date.as_str(), "%Y-%m-%d")
                .unwrap()
                .format("%b %d, %Y")
                .to_string()
        }

        // 9 because 2023-07-06 has 10 characters
        write!(f, "{}\t{:>9}\t{}", self.status, finish_date, self.title)
    }
}

#[derive(Debug, Deserialize)]
pub struct Anime {
    id: i64,
    title: String,
    start_date: String,
    end_date: Option<String>,
    synopsis: String,
    mean: f64,
    rank: i64,
}

impl Anime {
    pub fn get_id(self) -> i64 {
        self.id
    }

    pub fn get_title(self) -> String {
        self.title
    }

    pub fn get_synopsis(self) -> String {
        self.synopsis
    }

    pub fn get_rank(self) -> i64 {
        self.rank
    }
}

impl fmt::Display for Anime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut start_date = "-".to_string();
        if self.start_date != "-" {
            start_date = NaiveDate::parse_from_str(self.start_date.as_str(), "%Y-%m-%d")
                .unwrap()
                .format("%b %d, %Y")
                .to_string()
        }

        let end_date = match &self.end_date {
            Some(date) => {
                NaiveDate::parse_from_str(&date.as_str(), "%Y-%m-%d")
                .unwrap()
                .format("%b %d, %Y")
                .to_string()
            },
            _ => "-".to_string(),
        };

        // 9 because 2023-07-06 has 10 characters
        write!(
            f,
            "Start\t{}\nEnd\t{}\nScore\t{}\n",
            start_date, end_date, self.mean
        )
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct SeasonalAnime {
    id: i64,
    title: String,
}

impl SeasonalAnime {
    pub fn get_id(self) -> i64 {
        self.id
    }

    pub fn get_title(self) -> String {
        self.title
    }
}

impl fmt::Display for SeasonalAnime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}\t{}", self.id, self.title)
    }
}