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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use failure::{Error, SyncFailure};
use MAL;
use minidom::Element;
use request::Request;
use std::fmt::{self, Debug, Display};

macro_rules! generate_response_xml {
    ($struct:ident, $($field:ident($val_name:ident): $xml_name:expr => $xml_val:expr),+) => {{
        let mut entry = Element::bare("entry");

        $(if $struct.$field.changed {
            let $val_name = &$struct.$field.value;

            let mut elem = Element::bare($xml_name);
            elem.append_text_node($xml_val);
            entry.append_child(elem);
        })+

        let mut buffer = Vec::new();
        entry.write_to(&mut buffer).map_err(SyncFailure::new)?;

        Ok(String::from_utf8(buffer)?)
    }};
}

macro_rules! reset_changed_fields {
    ($struct:ident, $($name:ident),+) => ($($struct.$name.changed = false;)+);
}

#[cfg(feature = "anime-list")]
pub mod anime;
#[cfg(feature = "manga-list")]
pub mod manga;

/// Specifies what type of list is being used.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ListType {
    Anime,
    Manga,
}

impl Display for ListType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ListType::Anime => write!(f, "anime"),
            ListType::Manga => write!(f, "manga"),
        }
    }
}

/// Contains methods that perform common operations on a user's anime / manga list.
pub trait List {
    /// Represents an entry on a user's anime / manga list.
    type Entry: ListEntry;

    /// Requests and parses all entries on a user's anime / manga list.
    /// 
    /// # Examples
    /// 
    /// ```no_run
    /// use mal::MAL;
    /// use mal::list::List;
    /// 
    /// // Create a new MAL instance
    /// let mal = MAL::new("username", "password");
    /// 
    /// // Read all list entries from the user's list
    /// let entries = mal.anime_list().read_entries().unwrap();
    /// ```
    fn read_entries(&self) -> Result<Vec<Self::Entry>, Error> {
        let resp = Request::List(&self.mal().username, Self::list_type())
            .send(self.mal())?
            .text()?;

        let root: Element = resp.parse().map_err(SyncFailure::new)?;
        let mut entries = Vec::new();

        for child in root.children().skip(1) {
            let entry = Self::Entry::parse(child)?;
            entries.push(entry);
        }

        Ok(entries)
    }

    /// Adds an entry to the user's anime / manga list.
    /// 
    /// If the entry is already on the user's list, nothing will happen.
    /// 
    /// # Examples
    /// 
    /// ```no_run
    /// use mal::{MAL, AnimeInfo};
    /// use mal::list::List;
    /// use mal::list::anime::{AnimeEntry, WatchStatus};
    /// 
    /// // Create a new MAL instance
    /// let mal = MAL::new("username", "password");
    /// 
    /// // Search for "Toradora" on MyAnimeList
    /// let mut search_results = mal.search_anime("Toradora").unwrap();
    /// 
    /// // Use the first result's info
    /// let toradora_info = search_results.swap_remove(0);
    /// 
    /// // Create a new anime list entry with Toradora's info
    /// let mut entry = AnimeEntry::new(toradora_info);
    /// 
    /// // Set the entry's watched episodes to 5 and status to watching
    /// entry.set_watched_episodes(5).set_status(WatchStatus::Watching);
    /// 
    /// // Add the entry to the user's anime list
    /// mal.anime_list().add(&mut entry).unwrap();
    /// ```
    fn add(&self, entry: &mut Self::Entry) -> Result<(), Error> {
        let body = entry.generate_xml()?;
        
        Request::Add(entry.id(), Self::list_type(), &body)
            .send(self.mal())?;

        entry.set_last_updated_time();
        entry.reset_changed_fields();

        Ok(())
    }

    /// Updates an entry on the user's anime / manga list.
    /// 
    /// If the entry is already on the user's list, nothing will happen.
    /// 
    /// # Examples
    /// 
    /// ```no_run
    /// use mal::{MAL, AnimeInfo};
    /// use mal::list::List;
    /// use mal::list::anime::{AnimeEntry, WatchStatus};
    /// 
    /// // Create a new MAL instance
    /// let mal = MAL::new("username", "password");
    /// 
    /// // Get a handle to the user's anime list
    /// let anime_list = mal.anime_list();
    /// 
    /// // Get and parse all of the list entries
    /// let entries = anime_list.read_entries().unwrap();
    /// 
    /// // Find Toradora in the list entries
    /// let mut toradora_entry = entries.into_iter().find(|e| e.series_info.id == 4224).unwrap();
    /// 
    /// // Set new values for the list entry
    /// // In this case, the episode count will be updated to 25, the score will be set to 10, and the status will be set to completed
    /// toradora_entry.set_watched_episodes(25)
    ///               .set_score(10)
    ///               .set_status(WatchStatus::Completed);
    /// 
    /// // Update the anime on the user's list
    /// anime_list.update(&mut toradora_entry).unwrap();
    /// ```
    fn update(&self, entry: &mut Self::Entry) -> Result<(), Error> {
        let body = entry.generate_xml()?;

        Request::Update(entry.id(), Self::list_type(), &body)
            .send(self.mal())?;

        entry.set_last_updated_time();
        entry.reset_changed_fields();

        Ok(())
    }

    /// Removes an entry from the user's anime / manga list.
    /// 
    /// If the entry isn't already on the user's list, nothing will happen.
    /// 
    /// # Examples
    /// 
    /// ```no_run
    /// use mal::{MAL, AnimeInfo};
    /// use mal::list::List;
    /// use mal::list::anime::{AnimeEntry, WatchStatus};
    /// 
    /// // Create a new MAL instance
    /// let mal = MAL::new("username", "password");
    /// 
    /// // Search for "Toradora" on MyAnimeList
    /// let mut search_results = mal.search_anime("Toradora").unwrap();
    /// 
    /// // Use the first result's info
    /// let toradora_info = search_results.swap_remove(0);
    /// 
    /// // Get a handle to the user's anime list
    /// let anime_list = mal.anime_list();
    /// 
    /// // Get and parse all of the list entries
    /// let entries = anime_list.read_entries().unwrap();
    /// 
    /// // Find Toradora in the list entries
    /// let toradora_entry = entries.into_iter().find(|e| e.series_info.id == 4224).unwrap();
    /// 
    /// // Delete Toradora from the user's anime list
    /// anime_list.delete(&toradora_entry).unwrap();
    /// ```
    fn delete(&self, entry: &Self::Entry) -> Result<(), Error> {
        Request::Delete(entry.id(), Self::list_type())
            .send(self.mal())?;

        Ok(())
    }

    /// Removes an entry from the user's anime / manga list by its id.
    /// 
    /// If the entry isn't already on the user's list, nothing will happen.
    /// 
    /// # Examples
    /// 
    /// ```no_run
    /// use mal::{MAL, AnimeInfo};
    /// use mal::list::List;
    /// use mal::list::anime::{AnimeEntry, WatchStatus};
    /// 
    /// // Create a new MAL instance
    /// let mal = MAL::new("username", "password");
    /// 
    /// // Delete the anime with the id of 4224 (Toradora) from the user's anime list
    /// mal.anime_list().delete_id(4224).unwrap();
    /// ```
    fn delete_id(&self, id: u32) -> Result<(), Error> {
        Request::Delete(id, Self::list_type())
            .send(self.mal())?;
        
        Ok(())
    }

    /// Returns what type of list this is.
    fn list_type() -> ListType;
    /// Returns a reference to the [MAL] client used to send requests to the API.
    /// 
    /// [MAL]: ../struct.MAL.html
    fn mal(&self) -> &MAL;
}

/// Contains required methods to generate and parse list entries from the API.
pub trait ListEntry where Self: Sized {
    /// Used to construct a new version of `Self` from response XML.
    fn parse(xml_elem: &Element) -> Result<Self, Error>;

    /// Used to generate XML to send to the API.
    fn generate_xml(&self) -> Result<String, Error>;
    /// Used to reset the status of any fields that have been modified
    /// since last updating the entry on MyAnimeList.
    fn reset_changed_fields(&mut self);

    /// Used to update the last updated time.
    fn set_last_updated_time(&mut self);

    /// Used to get the MyAnimeList ID of the list entry.
    fn id(&self) -> u32;
}

#[derive(Debug, Clone)]
struct ChangeTracker<T: Debug + Clone> {
    value: T,
    changed: bool,
}

impl<T: Debug + Clone> ChangeTracker<T> {
    fn new(value: T) -> ChangeTracker<T> {
        ChangeTracker {
            value,
            changed: false,
        }
    }

    fn set(&mut self, value: T) {
        self.value = value;
        self.changed = true;
    }
}

impl<T: Debug + Clone> From<T> for ChangeTracker<T> {
    fn from(value: T) -> Self {
        ChangeTracker::new(value)
    }
}