libwebnovel 0.9.3

A Rust crate enabling users to get chapters of a webnovel, with multiple available backends.
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use std::cmp::Ordering;
use std::fmt::Debug;

use regex::Regex;
use reqwest::StatusCode;
use strum::{EnumCount, EnumIter, IntoEnumIterator};

#[cfg(feature = "freewebnovel")]
pub use crate::backends::freewebnovel::FreeWebNovel;
#[cfg(feature = "libread")]
pub use crate::backends::libread::LibRead;
#[cfg(feature = "lightnovelworld")]
pub use crate::backends::lightnovelworld::LightNovelWorld;
#[cfg(feature = "royalroad")]
pub use crate::backends::royalroad::RoyalRoad;
use crate::utils::get;
use crate::Chapter;

#[cfg(feature = "libread")]
mod libread;
#[cfg(feature = "royalroad")]
mod royalroad;

#[cfg(feature = "freewebnovel")]
mod freewebnovel;

#[cfg(feature = "lightnovelworld")]
mod lightnovelworld;

/// An error that may be returned when the backend encounters an error
#[derive(thiserror::Error, Debug)]
pub enum BackendError {
    /// No backend capable of handling the given url has been found
    #[error("No backend has been found capable of handling the url {0}.")]
    NoMatchingBackendFound(String),
    /// Error while trying to access the fiction page
    #[error("An error has been encountered while trying to access fiction page: {0}")]
    NetError(#[from] reqwest::Error),
    /// We got an HTTP404 on the given URL :p
    #[error("the given url could not be found")]
    UrlNotFound,
    /// Used when [`reqwest::Response::status()`] returns something else than
    /// success
    #[error("We could not access the fiction page: {message}: {status}: {content}")]
    RequestFailed {
        /// A message describing the error
        message: String,
        /// Status code of the response to the failed request
        status: StatusCode,
        /// Content of the response's body as text
        content: String,
    },
    /// An error while parsing the fiction
    #[error("An error occured while parsing the fiction page: {0}")]
    ParseError(String),
    /// We could not parse a valid date
    #[error("An error occured while trying to make sense of a date: {0}")]
    DateParseError(#[from] chrono::format::ParseError),
    /// returned when we could not find the given index for a chapter
    #[error("Could not find chapter {0}")]
    UnknownChapter(usize),
    /// We have attempted something on a chapter which required some information
    /// that the chapter doesn't have. Most likely, this is something to report.
    #[error("{msg} on Chapter {chapter_url}:", chapter_url=chapter.chapter_url())]
    MissingChapterInformation {
        /// A message identifying why this error has been returned
        msg: String,
        /// The [`Chapter`] the issue originated from
        chapter: Box<Chapter>,
    },
}

type ChapterOrderingFn = Box<dyn Fn(&Chapter, &Chapter) -> Ordering>;
pub(crate) type ChapterListElem = (usize, String);
impl TryFrom<&Chapter> for ChapterListElem {
    type Error = BackendError;

    fn try_from(value: &Chapter) -> Result<Self, Self::Error> {
        Ok((
            value.index,
            value
                .title()
                .as_ref()
                .ok_or(BackendError::MissingChapterInformation {
                    msg: "Could not find a valid title".to_string(),
                    chapter: Box::new(value.clone()),
                })?
                .to_string(),
        ))
    }
}

/// Must be implemented by each backend.
///
/// ## How to implement a new backend ?
///
/// First, you need to implement the [`Backend`] trait for a struct capable of
/// handling your favorite novelreading website. Have a look at the
/// implementation of [`RoyalRoad`] (if the `royalroad` feature is active) for
/// an example.
///
/// Second, you need to add a feature in [Cargo.toml](/Cargo.toml) with your
/// backend name & what it requires.
///
/// Third, you need to add your new backend to the variants of [`Backends`].
/// Don't forget to tag it with `#[cfg(feature = "my_backend_feature_name")]`.
///
/// Fourth, you need to change all the methods of [`Backends`] to accept your
/// new variant. Don't forget to tag the new variant behaviour with
/// `#[cfg(feature = "my_backend_feature_name")]`.
pub trait Backend: Default + Debug
where
    Self: Sized,
{
    /// Gets the list of url regexps supported by this backend.
    fn get_backend_regexps() -> Vec<Regex>;
    /// An identifier for the backend. It is static and can be used for
    /// long-term storage.
    fn get_backend_name() -> &'static str;
    /// Returns a function enabling chapter ordering. This is important to
    /// ensure that chapters may still be correctly sorted when the source
    /// chapters have been removed.
    fn get_ordering_function() -> ChapterOrderingFn {
        Box::new(|c1: &Chapter, c2: &Chapter| c1.published_at().cmp(c2.published_at()))
    }
    /// Creates a new instance of itself
    fn new(url: &str) -> Result<Self, BackendError>;
    /// Returns the title of the fiction
    fn title(&self) -> Result<String, BackendError>;
    /// Returns _something_ that can be used to identify this novel, and won't
    /// change if (for instance) the title changes.
    fn immutable_identifier(&self) -> Result<String, BackendError>;
    /// Returns the url of the fiction
    fn url(&self) -> String;
    /// Returns the fictions' cover URL, if any
    fn cover_url(&self) -> Result<String, BackendError>;

    /// Returns a list of authors, if any
    fn get_authors(&self) -> Result<Vec<String>, BackendError>;

    /// Returns a vector of available chapters _without requesting the chapters
    /// themselves_. The goal is to be able to detect collisions between
    /// something stored locally and a distant source.
    ///
    /// All vector elements must be a tuple `(chapter_index: usize,
    /// chapter_title: String)`.
    fn get_chapter_list(&self) -> Result<Vec<ChapterListElem>, BackendError>;

    /// Returns a single chapter. The chapter number need to be _unique_, as
    /// some webnovel platforms allow truncating the chapter list.
    fn get_chapter(&self, chapter_number: usize) -> Result<Chapter, BackendError>;

    /// Must return the total chapter count. Default implementation calls
    /// [`self.get_chapter_list().len()`][Backend::get_chapter_list()].
    fn get_chapter_count(&self) -> Result<usize, BackendError> {
        Ok(self.get_chapter_list()?.len())
    }

    /// Returns all chapters for this fiction. The default implementation simply
    /// calls [`Self::get_chapter`] repeatedly
    fn get_chapters(&self) -> Result<Vec<Chapter>, BackendError> {
        let mut chapters = Vec::new();
        for i in 1..self.get_chapter_count()? {
            let chapter = self.get_chapter(i)?;
            chapters.push(chapter);
        }
        Ok(chapters)
    }

    /// Returns the fictions' cover as a byte array, if any.
    fn cover(&self) -> Result<Vec<u8>, BackendError> {
        let resp = get(self.cover_url()?)?;
        if !resp.status().is_success() {
            return Err(BackendError::RequestFailed {
                message: "Could not download cover image".to_string(),
                status: resp.status(),
                content: resp.text()?,
            });
        }
        let image_bytes = resp.bytes()?;
        Ok(image_bytes.to_vec())
    }
}

/// Enum listing all available backends. A new backend may be constructed using
/// [`Backends::new`].
#[derive(EnumCount, EnumIter, Debug, Default)]
pub enum Backends {
    /// A dumb backend that should never be constructed, but is necessary for
    /// iteration (with [`strum::EnumIter`] & other features.
    #[default]
    Dumb,
    #[cfg(feature = "royalroad")]
    /// A RoyalRoad backend
    RoyalRoad(RoyalRoad),
    #[cfg(feature = "libread")]
    /// A LibRead backend
    LibRead(LibRead),
    #[cfg(feature = "freewebnovel")]
    /// A FreeWebNovel backend
    FreeWebNovel(FreeWebNovel),
    /// A LightNovelWorld backend
    #[cfg(feature = "lightnovelworld")]
    LightNovelWorld(LightNovelWorld),
}

impl Backends {
    /// Returns the ordering function specific to the underlying backend.
    ///
    /// # Panics
    ///
    /// Panics when `self` is [`Backends::Dumb`].
    pub fn get_ordering_function(&self) -> ChapterOrderingFn {
        match self {
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(_) => RoyalRoad::get_ordering_function(),
            #[cfg(feature = "libread")]
            Backends::LibRead(_) => LibRead::get_ordering_function(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(_) => FreeWebNovel::get_ordering_function(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(_) => LightNovelWorld::get_ordering_function(),
        }
    }

    /// Creates a new [`Backends`] variant from the given URL.
    pub(crate) fn new_from_url(&self, url: &str) -> Result<Backends, BackendError> {
        match self {
            Backends::Dumb => Ok(Self::Dumb),
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(_) => Ok(Self::RoyalRoad(RoyalRoad::new(url)?)),
            #[cfg(feature = "libread")]
            Backends::LibRead(_) => Ok(Self::LibRead(LibRead::new(url)?)),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(_) => Ok(Self::FreeWebNovel(FreeWebNovel::new(url)?)),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(_) => Ok(Self::LightNovelWorld(LightNovelWorld::new(url)?)),
        }
    }

    /// Returns the regexps used by the underlying backend. [`Backends::Dumb`]
    /// returns an empty [`Vec`].
    pub fn get_backend_regexps(&self) -> Vec<Regex> {
        match self {
            Backends::Dumb => Vec::new(),
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(_) => RoyalRoad::get_backend_regexps(),
            #[cfg(feature = "libread")]
            Backends::LibRead(_) => LibRead::get_backend_regexps(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(_) => FreeWebNovel::get_backend_regexps(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(_) => LightNovelWorld::get_backend_regexps(),
        }
    }

    /// Returns the underlying backend name.
    pub fn get_backend_name(&self) -> &'static str {
        match self {
            Backends::Dumb => "dummy",
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(_) => RoyalRoad::get_backend_name(),
            #[cfg(feature = "libread")]
            Backends::LibRead(_) => LibRead::get_backend_name(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(_) => FreeWebNovel::get_backend_name(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(_) => LightNovelWorld::get_backend_name(),
        }
    }
}

/// All possible backend variants are contained within [`Backends`]. This
/// implementation tries to dispatch method calls to their appropriate
/// implementors.
///
/// ```rust
/// use libwebnovel::{Backend, Backends};
/// let backend =
///     Backends::new("https://www.royalroad.com/fiction/21220/mother-of-learning").unwrap();
/// assert_eq!(backend.title().unwrap(), "Mother of Learning");
/// ```
impl Backend for Backends {
    /// Not implemented for [`Backends`]. Use
    /// [`Backends::get_backend_regexps(&self)`][a] instead.
    ///
    /// [a]: Backends#method.get_backend_regexps
    fn get_backend_regexps() -> Vec<Regex> {
        unimplemented!()
    }

    /// Not implemented for [`Backends`]. Use
    /// [`Backends::get_backend_name(&self)`][a] instead.
    ///
    /// [a]: Backends#method.get_backend_name
    fn get_backend_name() -> &'static str {
        unimplemented!()
    }

    /// Can't implement this function for backends without reference to `self`.
    /// use [`Backends::get_ordering_function(&self)`][a] instead.
    ///
    /// [a]: Backends#method.get_ordering_function
    fn get_ordering_function() -> ChapterOrderingFn {
        unimplemented!()
    }

    /// Builds a new backend for a given URL. Auto-detects the backend to use
    /// from the given URL, returning [`BackendError::NoMatchingBackendFound`]
    /// if none could be found.
    ///
    /// ```rust
    /// use libwebnovel::{Backend, Backends};
    /// let backend =
    ///     Backends::new("https://www.royalroad.com/fiction/21220/mother-of-learning").unwrap();
    /// assert_eq!(backend.title().unwrap(), "Mother of Learning");
    /// ```
    fn new(url: &str) -> Result<Self, BackendError> {
        for backend_variant in Backends::iter() {
            for regex in backend_variant.get_backend_regexps() {
                if regex.is_match(url) {
                    return backend_variant.new_from_url(url);
                }
            }
        }
        Err(BackendError::NoMatchingBackendFound(url.to_string()))
    }

    /// Returns the title of the webnovel. See [`Backends::new`] for an example.
    fn title(&self) -> Result<String, BackendError> {
        match self {
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(b) => b.title(),
            #[cfg(feature = "libread")]
            Backends::LibRead(b) => b.title(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(b) => b.title(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(b) => b.title(),
        }
    }

    fn immutable_identifier(&self) -> Result<String, BackendError> {
        match self {
            // implement this on the model of self.title() please
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(b) => b.immutable_identifier(),
            #[cfg(feature = "libread")]
            Backends::LibRead(b) => b.immutable_identifier(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(b) => b.immutable_identifier(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(b) => b.immutable_identifier(),
        }
    }

    /// Returns the URL of the webnovel.
    /// ```
    /// use libwebnovel::{Backend, Backends};
    /// let backend =
    ///     Backends::new("https://www.royalroad.com/fiction/21220/mother-of-learning").unwrap();
    /// assert_eq!(
    ///     backend.url(),
    ///     "https://www.royalroad.com/fiction/21220/mother-of-learning"
    /// );
    /// ```
    fn url(&self) -> String {
        match self {
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(b) => b.url(),
            #[cfg(feature = "libread")]
            Backends::LibRead(b) => b.url(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(b) => b.url(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(b) => b.url(),
        }
    }

    fn cover_url(&self) -> Result<String, BackendError> {
        // Write this function, on the model of the other functions in Backends
        match self {
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(backend) => backend.cover_url(),
            #[cfg(feature = "libread")]
            Backends::LibRead(backend) => backend.cover_url(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(backend) => backend.cover_url(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(b) => b.cover_url(),
        }
    }

    /// Returns the author(s) of the webnovel
    /// ```
    /// use libwebnovel::{Backend, Backends};
    /// let backend =
    ///     Backends::new("https://www.royalroad.com/fiction/21220/mother-of-learning").unwrap();
    /// assert_eq!(
    ///     backend.get_authors().unwrap(),
    ///     vec!["nobody103".to_string()]
    /// );
    /// ```
    fn get_authors(&self) -> Result<Vec<String>, BackendError> {
        match self {
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(b) => b.get_authors(),
            #[cfg(feature = "libread")]
            Backends::LibRead(b) => b.get_authors(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(b) => b.get_authors(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(b) => b.get_authors(),
        }
    }

    fn get_chapter_list(&self) -> Result<Vec<ChapterListElem>, BackendError> {
        match self {
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(b) => b.get_chapter_list(),
            #[cfg(feature = "libread")]
            Backends::LibRead(b) => b.get_chapter_list(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(b) => b.get_chapter_list(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(b) => b.get_chapter_list(),
        }
    }

    /// Returns a chapter of the webnovel, given its chapter number
    /// ```
    /// use libwebnovel::{Backend, Backends};
    /// let backend =
    ///     Backends::new("https://www.royalroad.com/fiction/21220/mother-of-learning").unwrap();
    /// let chapter = backend.get_chapter(1).unwrap();
    /// assert_eq!(
    ///     chapter.title(),
    ///     &Some("1. Good Morning Brother".to_string())
    /// );
    /// assert_eq!(*chapter.index(), 1);
    /// ```
    fn get_chapter(&self, chapter_number: usize) -> Result<Chapter, BackendError> {
        match self {
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(b) => b.get_chapter(chapter_number),
            #[cfg(feature = "libread")]
            Backends::LibRead(b) => b.get_chapter(chapter_number),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(b) => b.get_chapter(chapter_number),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(b) => b.get_chapter(chapter_number),
        }
    }

    /// Returns the total count of the webnovel's chapters.
    ///
    /// # Example
    /// ```
    /// use libwebnovel::{Backend, Backends};
    /// let backend =
    ///     Backends::new("https://www.royalroad.com/fiction/21220/mother-of-learning").unwrap();
    /// assert_eq!(backend.get_chapter_count().unwrap(), 109);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics when used on the [`Backends::Dumb`] backend.
    fn get_chapter_count(&self) -> Result<usize, BackendError> {
        match self {
            Backends::Dumb => {
                unimplemented!()
            }
            #[cfg(feature = "royalroad")]
            Backends::RoyalRoad(b) => b.get_chapter_count(),
            #[cfg(feature = "libread")]
            Backends::LibRead(b) => b.get_chapter_count(),
            #[cfg(feature = "freewebnovel")]
            Backends::FreeWebNovel(b) => b.get_chapter_count(),
            #[cfg(feature = "lightnovelworld")]
            Backends::LightNovelWorld(b) => b.get_chapter_count(),
        }
    }
}