rbook 0.7.1

A fast, format-agnostic, ergonomic ebook library for reading, building, and editing EPUB 2 and 3.
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
//! Sequential + random‐access [`Ebook`](super::Ebook) [`Reader`] module.
//!
//! # See Also
//! - [`epub::reader`][crate::epub::reader] for the epub-specific reader module.

pub mod errors;

use crate::ebook::manifest::ManifestEntry;
use crate::ebook::spine::SpineEntry;
use crate::reader::errors::ReaderResult;
use crate::util::Sealed;

/// A sequential + random-access [`Ebook`](super::reader) reader.
///
/// # Lifetime
/// All returned [`ReaderContent<'ebook>`](ReaderContent) are tied to the lifetime of the
/// underlying [`Ebook`](super::Ebook).
///
/// # See Also
/// - [`EpubReader`](crate::epub::reader::EpubReader) for epub-specific reader information.
///
/// # Examples
/// - Streaming over a reader's contents:
/// ```
/// # use rbook::Epub;
/// # fn main() -> rbook::ebook::errors::EbookResult<()> {
/// let epub = Epub::open("tests/ebooks/example_epub")?;
/// let mut reader = epub.reader();
/// # let mut count = 0;
///
/// // Stream over all content
/// while let Some(Ok(content)) = reader.read_next() {
/// #    count += 1;
///     // process content //
/// }
/// # assert_eq!(5, count);
/// # assert_eq!(count, reader.len());
/// # Ok(())
/// # }
/// ```
/// - Random access:
/// ```
/// # use rbook::Epub;
/// # fn main() -> rbook::ebook::errors::EbookResult<()> {
/// let epub = Epub::open("tests/ebooks/example_epub")?;
/// let mut reader = epub.reader();
///
/// let content_a = reader.get(2)?;
/// let content_b = reader.get("c1")?; // by idref
///
/// assert_eq!(2, content_a.position());
/// assert_eq!(2, content_b.position());
/// # assert_eq!(content_a.content(), content_b.content());
/// # Ok(())
/// # }
/// ```
pub trait Reader<'ebook>: Sealed {
    /// Resets the reader's cursor to its initial state; **before** the first entry.
    ///
    /// After calling this method:
    /// - [`Self::current_position`] = [`None`]
    /// - [`Self::remaining`] = The total number of entries ([`Self::len`])
    ///
    /// By default, a newly created [`Reader`] starts in this state.
    ///
    /// # Examples
    /// - Assessing the current cursor position state:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut reader = epub.reader();
    ///
    /// // Cursor is before the first entry
    /// assert_eq!(None, reader.current_position());
    /// assert_eq!(5, reader.remaining());
    ///
    /// // Iterate over all content
    /// for result in &mut reader {
    ///     // process content //
    /// }
    ///
    /// assert_eq!(0, reader.remaining());
    ///
    /// // Resetting the cursor, so it is **before** the first element
    /// reader.reset();
    /// assert_eq!(None, reader.current_position());
    /// assert_eq!(5, reader.remaining());
    ///
    /// // Setting the cursor **at** the first element.
    /// reader.read(0)?;
    /// assert_eq!(Some(0), reader.current_position());
    /// assert_eq!(4, reader.remaining());
    /// # Ok(())
    /// # }
    /// ```
    fn reset(&mut self);

    /// Returns the next [`ReaderContent`] and increments the reader's cursor by one.
    ///
    /// # Cases
    /// - `Some(Ok(content))`: Entry exists and reading it succeeded.  
    /// - `Some(Err(e))`: Entry exists yet reading it failed
    ///   (see [`ReaderError`](errors::ReaderError)).
    /// - `None`: No next entries; ***in this case, the cursor is not incremented.***
    ///
    /// # Examples
    /// - Observing how `read_next` affects the cursor position:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut reader = epub.reader();
    ///
    /// // Current cursor position
    /// assert_eq!(None, reader.current_position());
    /// // Iterate to the end
    /// while let Some(Ok(content)) = reader.read_next() {
    ///     // process content //
    /// }
    /// // The current cursor position is now at the end
    /// assert_eq!(Some(4), reader.current_position());
    ///
    /// // No next content
    /// assert!(reader.read_next().is_none());
    /// // The cursor is not updated
    /// assert_eq!(Some(4), reader.current_position());
    ///
    /// # Ok(())
    /// # }
    /// ```
    fn read_next(&mut self) -> Option<ReaderResult<impl ReaderContent<'ebook> + 'ebook>>;

    /// Returns the previous [`ReaderContent`] and decrements the reader's cursor by one.
    ///
    /// # Cases
    /// - `Some(Ok(content))`: Entry exists and reading it succeeded.  
    /// - `Some(Err(e))`: Entry exists yet reading it failed
    ///   (see [`ReaderError`](errors::ReaderError)).
    /// - `None`: No previous entries; ***in this case, the cursor is not decremented.***
    ///
    /// # Examples
    /// - Observing how `read_prev` affects the cursor position:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut reader = epub.reader();
    ///
    /// // Jump to the end
    /// reader.seek(reader.len() - 1)?;
    /// assert_eq!(Some(4), reader.current_position());
    ///
    /// // Iterate to the start
    /// while let Some(Ok(content)) = reader.read_prev() {
    ///     // ... //
    /// }
    /// // Current cursor position at the start
    /// assert_eq!(Some(0), reader.current_position());
    ///
    /// // No previous content
    /// assert!(reader.read_prev().is_none());
    /// // The cursor is not updated
    /// assert_eq!(Some(0), reader.current_position());
    ///
    /// # Ok(())
    /// # }
    /// ```
    fn read_prev(&mut self) -> Option<ReaderResult<impl ReaderContent<'ebook> + 'ebook>>;

    /// Returns the [`ReaderContent`] that the reader's cursor is currently positioned at.
    ///
    /// # Cases
    /// - `Some(Ok(content))`: Entry exists and reading it succeeded.  
    /// - `Some(Err(e))`: Entry exists yet reading it failed
    ///   (see [`ReaderError`](errors::ReaderError)).
    /// - `None`: No current entry ([`Self::current_position`] is [`None`]).
    fn read_current(&self) -> Option<ReaderResult<impl ReaderContent<'ebook> + 'ebook>>;

    /// Returns the [`ReaderContent`] at the given [`ReaderKey`]
    /// and moves the reader’s cursor to that position.
    ///
    /// Equivalent to [`Self::get`], except that this method updates the cursor.
    ///
    /// To re-iterate from the start, prefer [`Self::reset`]
    /// over `read(0)`, as `reset` puts the cursor **before** the first entry.
    ///
    /// # Errors
    /// See [`ReaderError`](errors::ReaderError).
    fn read<'a>(
        &mut self,
        key: impl Into<ReaderKey<'a>>,
    ) -> ReaderResult<impl ReaderContent<'ebook> + 'ebook>;

    /// Moves the reader’s cursor **to** the given [`ReaderKey`]
    /// and returns the resulting cursor position.
    ///
    /// To iterate from the start again, prefer [`Self::reset`]
    /// over `seek(0)`, as `reset` positions the cursor **before** the first entry.
    ///
    /// # Errors
    /// See [`ReaderError`](errors::ReaderError).
    fn seek<'a>(&mut self, key: impl Into<ReaderKey<'a>>) -> ReaderResult<usize>;

    /// Returns the [`ReaderContent`] at the given [`ReaderKey`]
    /// without moving the reader's cursor.
    ///
    /// # Errors
    /// See [`ReaderError`](errors::ReaderError).
    fn get<'a>(
        &self,
        key: impl Into<ReaderKey<'a>>,
    ) -> ReaderResult<impl ReaderContent<'ebook> + 'ebook>;

    /// The total number of traversable [`ReaderContent`] entries.
    ///
    /// This method returns the same value regardless of calls to methods that mutate
    /// a reader's cursor such as [`Self::read`].
    ///
    /// # See Also
    /// - [`Self::remaining`] to find out how many entries are left relative to a cursor.
    fn len(&self) -> usize;

    /// The position of the reader’s cursor (current entry).
    ///
    /// Returns [`None`] if the cursor is **before** the first entry
    /// (such as on a newly created reader or after invoking [`Self::reset`].
    /// Otherwise, `Some(i)` where `0 <= i < len`.
    ///
    /// # Examples
    /// - Retrieving the position upon navigating:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut reader = epub.reader();
    ///
    /// assert_eq!(None, reader.current_position());
    ///
    /// // Set position to `0`
    /// reader.seek(0)?;
    /// assert_eq!(Some(0), reader.current_position());
    ///
    /// reader.read_next();
    /// assert_eq!(Some(1), reader.current_position());
    ///
    /// // Set position to the end
    /// reader.seek(reader.len() - 1)?;
    /// assert_eq!(Some(4), reader.current_position());
    ///
    /// // The position remains the same since the end is reached (len - 1)
    /// assert!(reader.read_next().is_none());
    /// assert_eq!(Some(4), reader.current_position());
    ///
    /// reader.reset();
    /// assert_eq!(None, reader.current_position());
    /// # Ok(())
    /// # }
    /// ```
    fn current_position(&self) -> Option<usize>;

    /// The total number of remaining traversable [`ReaderContent`]
    /// until the reader's cursor reaches the end.
    ///
    /// # Examples
    /// - Observing the number of contents remaining:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut reader = epub.reader();
    ///
    /// assert_eq!(5, reader.len());
    /// assert_eq!(5, reader.remaining());
    /// assert_eq!(None, reader.current_position());
    ///
    /// // `len` remains fixed while `remaining` changes:
    /// reader.seek(3)?;
    /// assert_eq!(5, reader.len());
    /// assert_eq!(1, reader.remaining());
    /// assert_eq!(Some(3), reader.current_position());
    /// # Ok(())
    /// # }
    /// ```
    fn remaining(&self) -> usize {
        match self.current_position() {
            Some(position) => self.len().saturating_sub(position + 1),
            None => self.len(),
        }
    }

    /// Returns `true` if the reader has no [`ReaderContent`] to provide;
    /// a [length](`Self::len`) of `0`.
    ///
    /// # Examples
    /// - Assessing if a reader has content:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut reader = epub.reader();
    ///
    /// assert_eq!(5, reader.len());
    /// // The reader has 5 entries, so it is not empty:
    /// assert!(!reader.is_empty());
    /// # Ok(())
    /// # }
    /// ```
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Content provided by a [`Reader`], encompassing associated data.
///
/// # Examples
/// - Retrieving the content of the same entry by different [`keys`](ReaderKey):
/// ```
/// # use rbook::Epub;
/// # fn main() -> rbook::ebook::errors::EbookResult<()> {
/// let epub = Epub::open("tests/ebooks/example_epub")?;
/// let mut reader = epub.reader();
///
/// let entry_by_idref = reader.get("cover")?;
/// let entry_by_position = reader.get(0)?;
/// let kind =  entry_by_idref.manifest_entry().kind();
///
/// assert_eq!(0, entry_by_idref.position());
/// assert_eq!(0, entry_by_position.position());
/// assert_eq!("application/xhtml+xml", kind.as_str());
///
/// // Retrieving the main content
/// let string_ref: &str = entry_by_idref.content();
///
/// assert_eq!(string_ref, entry_by_position.content());
///
/// let string_content: String = entry_by_idref.into_string(); // or .into()
/// let bytes_content: Vec<u8> = entry_by_position.into(); // or .into_bytes()
///
/// assert_eq!(bytes_content, string_content.into_bytes());
/// # Ok(())
/// # }
/// ```
pub trait ReaderContent<'ebook>: Into<String> + Into<Vec<u8>> + Sealed {
    /// The position of reader content within a [`Reader`] (0-index-based).
    ///
    /// This value may not equal [`SpineEntry::order`] depending
    /// on how a reader is configured.
    ///
    /// # Examples
    /// - Showcasing different positioning regarding EPUB:
    /// ```
    /// # use rbook::Epub;
    /// # use rbook::epub::reader::LinearBehavior;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    ///
    /// // Reader with non-linear spine entries prepended at the start of its internal buffer.
    /// let mut reader_a = epub.reader_builder()
    ///     .linear_behavior(LinearBehavior::PrependNonLinear)
    ///     .create();
    /// let content_a = reader_a.read_next().unwrap()?;
    ///
    /// assert_eq!(0, content_a.position());
    /// assert_eq!(0, content_a.spine_entry().order());
    /// assert_eq!("cover", content_a.spine_entry().idref());
    ///
    /// // Reader with non-linear spine entries appended at the end of its internal buffer.
    /// let mut reader_b = epub.reader_builder()
    ///     .linear_behavior(LinearBehavior::AppendNonLinear)
    ///     .create();
    /// let content_b = reader_b.read_next().unwrap()?;
    ///
    /// assert_eq!(0, content_b.position());
    /// assert_eq!(1, content_b.spine_entry().order());
    /// assert_eq!("toc", content_b.spine_entry().idref());
    /// # Ok(())
    /// # }
    /// ```
    fn position(&self) -> usize;

    /// The readable content (e.g., `XHTML`, `HTML`, etc.).
    fn content(&self) -> &str;

    /// The associated [`SpineEntry`] containing reading order details.
    fn spine_entry(&self) -> impl SpineEntry<'ebook> + 'ebook;

    /// The associated [`ManifestEntry`] containing resource details.
    fn manifest_entry(&self) -> impl ManifestEntry<'ebook> + 'ebook;

    /// Takes the contained readable content string.
    ///
    /// This method is equivalent to calling `into::<String>()`.
    ///
    /// See [`Self::content`] to retrieve a reference without taking ownership.
    ///
    /// # Examples:
    /// - Extracting the contained content in the form of a [`String`]:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut reader = epub.reader();
    ///
    /// let content_a: String = reader.get(2)?.into();
    /// let content_b = reader.get(2)?.into_string();
    ///
    /// assert_eq!(content_a, content_b);
    /// # Ok(())
    /// # }
    /// ```
    fn into_string(self) -> String {
        self.into()
    }

    /// Takes the contained readable content bytes.
    ///
    /// This method is equivalent to calling `into::<Vec<u8>()`.
    ///
    /// # Examples:
    /// - Extracting the contained content in the form of bytes:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut reader = epub.reader();
    ///
    /// let content_a: Vec<u8> = reader.get(2)?.into();
    /// let content_b = reader.get(2)?.into_bytes();
    ///
    /// assert_eq!(content_a, content_b);
    /// # Ok(())
    /// # }
    /// ```
    fn into_bytes(self) -> Vec<u8> {
        self.into()
    }
}

/// A key to access content within a [`Reader`].
#[non_exhaustive]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum ReaderKey<'a> {
    /// A string value, intended for lookup within a [`Reader`].
    ///
    /// For an [`Epub`](crate::Epub), this value corresponds to the `idref` of a spine entry.
    Value(&'a str),

    /// An absolute position within the internal buffer of a [`Reader`].
    ///
    /// When passed as an argument to a reader,
    /// it must be less than [`Reader::len`] or
    /// [`ReaderError::OutOfBounds`](errors::ReaderError::OutOfBounds) will be returned.
    Position(usize),
}

impl<'a> From<&'a str> for ReaderKey<'a> {
    fn from(value: &'a str) -> Self {
        Self::Value(value)
    }
}

impl From<usize> for ReaderKey<'_> {
    fn from(index: usize) -> Self {
        Self::Position(index)
    }
}