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
//! Format-agnostic table-of-contents; [`Toc`]-related content.
//!
//! # See Also
//! - [`epub::toc`][crate::epub::toc] for the epub-specific ToC module.

use crate::ebook::manifest::ManifestEntry;
use crate::ebook::resource::Resource;
use crate::ebook::toc::macros::toc_entry_kind;
use crate::util::Sealed;
use std::fmt::Display;

/// The table of contents, aiding navigation throughout an ebook [`Ebook`](super::Ebook).
///
/// Each [`TocEntry`] returned by [`Toc`] is a top-level root containing
/// [children](TocEntry::iter).
///
/// The methods [`Self::by_kind`] and [`Self::iter`] can be used to retrieve TOC variants,
/// such as [`landmarks`](TocEntryKind::Landmarks), [`page-list`](TocEntryKind::PageList), etc.
///
/// # See Also
/// - [`EpubToc`](crate::epub::toc::EpubToc) for epub-specific table of contents information.
///
/// # Examples
/// - Iterating over the table of contents:
/// ```
/// # use rbook::Epub;
/// # fn main() -> rbook::ebook::errors::EbookResult<()> {
/// let epub = Epub::open("tests/ebooks/example_epub")?;
/// let root = epub.toc().contents().unwrap();
/// let mut children = root.iter();
///
/// // A for loop may also be used alternatively
/// assert_eq!("The Cover", children.next().unwrap().label());
/// assert_eq!("rbook Chapter 1", children.next().unwrap().label());
/// assert_eq!("rbook Chapter 2", children.next().unwrap().label());
/// assert_eq!(None, children.next());
/// # Ok(())
/// # }
/// ```
pub trait Toc<'ebook>: Sealed {
    /// Returns the **root** [`TocEntry`] of the primary TOC, or [`None`] if it does not exist.
    ///
    /// See the [trait-level example](Toc) for how to traverse the hierarchy.
    fn contents(&self) -> Option<impl TocEntry<'ebook> + 'ebook>;

    /// Returns the **root** [`TocEntry`] for the given [`TocEntryKind`],
    /// or [`None`] if it does not exist.
    ///
    /// # Examples
    /// - Retrieving different table of contents by kind:
    /// ```
    /// # use rbook::ebook::toc::TocEntryKind;
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let toc = epub.toc();
    ///
    /// // Providing a string as input:
    /// let contents = toc.by_kind("toc");
    /// let pagelist = toc.by_kind("page-list");
    /// // Providing an enum as input:
    /// let landmarks = toc.by_kind(TocEntryKind::Landmarks);
    ///
    /// assert_eq!(contents, toc.by_kind(TocEntryKind::Toc));
    /// assert_eq!(pagelist, toc.by_kind(TocEntryKind::PageList));
    /// assert_eq!(landmarks, toc.by_kind("landmarks"));
    /// assert_eq!(None, toc.by_kind(TocEntryKind::ListOfIllustrations));
    /// # Ok(())
    /// # }
    /// ```
    fn by_kind(
        &self,
        kind: impl Into<TocEntryKind<'ebook>>,
    ) -> Option<impl TocEntry<'ebook> + 'ebook>;

    /// Returns an iterator over all **root** [entries](TocEntry).
    ///
    /// # See Also
    /// - [`TocEntry::kind`] to retrieve the [`TocEntryKind`] of each root.
    ///
    /// # Examples
    /// - Iterating over roots and observing their kind:
    /// ```
    /// # use rbook::ebook::toc::TocEntryKind;
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let mut roots = epub.toc().iter();
    ///
    /// let contents = roots.next().unwrap();
    /// assert_eq!(TocEntryKind::Toc, contents.kind());
    ///
    /// let landmarks = roots.next().unwrap();
    /// assert_eq!(TocEntryKind::Landmarks, landmarks.kind());
    ///
    /// let pagelist = roots.next().unwrap();
    /// assert_eq!(TocEntryKind::PageList, pagelist.kind());
    ///
    /// // No remaining roots
    /// assert_eq!(None, roots.next());
    /// # Ok(())
    /// # }
    /// ```
    fn iter(&self) -> impl Iterator<Item = impl TocEntry<'ebook>> + 'ebook;
}

/// An entry contained within a [`Toc`], encompassing associated metadata.
///
/// Provides two forms of iterators:
/// - [`TocEntry::iter`]: Direct children (nested form).
/// - [`TocEntry::flatten`]: **All** children recursively.
///
/// # See Also
/// - [`EpubTocEntry`](crate::epub::toc::EpubTocEntry) for epub-specific entry information.
pub trait TocEntry<'ebook>: Sealed {
    /// The depth of an entry relative to the root ([`0 = root`](Self::is_root)).
    fn depth(&self) -> usize;

    /// The human-readable label.
    ///
    /// The label is the text displayed to the user in a reading system's navigation menu.
    fn label(&self) -> &'ebook str;

    /// The semantic kind of content associated with an entry.
    ///
    /// For example, an entry may point to the
    /// [`appendix`](TocEntryKind::Appendix) or [`cover page`](TocEntryKind::Cover).
    fn kind(&self) -> TocEntryKind<'ebook>;

    /// The [`ManifestEntry`] associated with a [`TocEntry`].
    ///
    /// Returns [`None`] if the toc entry references a non-existent
    /// [`ManifestEntry`] within the [`Manifest`](super::Manifest).
    fn manifest_entry(&self) -> Option<impl ManifestEntry<'ebook> + 'ebook>;

    /// The [`Resource`] intended to navigate to from an entry.
    ///
    /// # Examples
    /// - Retrieving the resource associated with an entry:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let main_toc_root = epub.toc().contents().unwrap();
    ///
    /// // Root has no associated resource
    /// assert_eq!(None, main_toc_root.resource());
    ///
    /// for child in main_toc_root {
    ///     let resource = child.resource().unwrap();
    ///     assert_eq!("application/xhtml+xml", resource.kind().as_str());
    ///     
    ///     let content = epub.read_resource_str(resource)?;
    ///     // process content //
    /// }
    /// # Ok(())
    /// # }
    /// ```
    fn resource(&self) -> Option<Resource<'ebook>> {
        self.manifest_entry().map(|entry| entry.resource())
    }

    /// Returns the associated direct child [`TocEntry`] if the given `index` is less than
    /// [`Self::len`], otherwise [`None`].
    fn get(&self, index: usize) -> Option<impl TocEntry<'ebook> + 'ebook>;

    /// Returns an iterator over direct child entries
    /// (whose [`depth`](TocEntry::depth) is one greater than the parent).
    ///
    /// # See Also
    /// - [`Self::flatten`] for ***all*** children recursively.
    fn iter(&self) -> impl Iterator<Item = impl TocEntry<'ebook> + 'ebook> + 'ebook;

    /// Returns a recursive iterator over **all** children.
    fn flatten(&self) -> impl Iterator<Item = impl TocEntry<'ebook> + 'ebook> + 'ebook;

    /// The total number of direct [`children`](Self::iter) a toc entry has.
    fn len(&self) -> usize;

    /// Returns `true` if there are no children.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns `true` if the depth of a toc entry is `0`, indicating the root.
    ///
    /// # Examples
    /// - Assessing if an entry is a root:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let main_toc_root = epub.toc().contents().unwrap();
    ///
    /// assert!(main_toc_root.is_root());
    ///
    /// for child in main_toc_root {
    ///     // Immediate children are never roots:
    ///     assert!(!child.is_root());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    fn is_root(&self) -> bool {
        self.depth() == 0
    }

    /// Calculates and returns the **maximum** depth relative to an entry.
    /// In other words, how many levels deep is the most-nested child?
    ///
    /// Child [entries](TocEntry) have a maximum depth less than the parent.
    /// For example, if an entry has a maximum depth of `5`,
    /// then its direct children will have a maximum depth of **at most** `4`.
    ///
    /// # Scenarios
    /// The maximum depth indicates the following:
    ///
    /// | Max Depth | Indication                                                      |
    /// |-----------|-----------------------------------------------------------------|
    /// | 0         | No direct children (Equivalent to [`TocEntry::is_empty`]).      |
    /// | 1         | Only direct children (Children do not contain nested children). |
    /// | \>1       | At least one direct child contains nested children.             |
    ///
    /// # See Also
    /// - [`Self::depth`] for the pre-computed depth relative to the root.
    ///
    /// # Examples
    /// - Comparing the calculated maximum depth with [`Self::depth`]:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let main_toc_root = epub.toc().contents().unwrap();
    ///
    /// // Current depth relative to the root
    /// assert_eq!(0, main_toc_root.depth());
    /// // Calculated maximum depth - deepest child entry within the hierarchy
    /// assert_eq!(2, main_toc_root.max_depth());
    ///
    /// let child = main_toc_root.get(0).unwrap();
    ///
    /// // Current depth relative to the root
    /// assert_eq!(1, child.depth());
    /// // Calculated maximum depth - `child` entry has no children
    /// assert_eq!(0, child.max_depth());
    /// # Ok(())
    /// # }
    /// ```
    fn max_depth(&self) -> usize {
        self.iter()
            .fold(0, |depth, child| depth.max(1 + child.max_depth()))
    }

    /// Calculates and returns the **total** number of all (direct and nested)
    /// children relative to an entry.
    ///
    /// # Scenarios
    /// The total number of children indicates the following:
    ///
    /// | Total Children  | Indication                                                      |
    /// |-----------------|-----------------------------------------------------------------|
    /// | 0               | No direct children (Equivalent to [`Self::is_empty`]).          |
    /// | [`Self::len`]   | Only direct children (Children do not contain nested children). |
    /// | \>[`Self::len`] | At least one direct child contains nested children.             |
    ///
    /// # Examples
    /// - Comparing the calculated total length with [`Self::len`]:
    /// ```
    /// # use rbook::Epub;
    /// # fn main() -> rbook::ebook::errors::EbookResult<()> {
    /// let epub = Epub::open("tests/ebooks/example_epub")?;
    /// let main_toc_root = epub.toc().contents().unwrap();
    ///
    /// assert_eq!(3, main_toc_root.len());
    /// // The `4` indicates that there is a single nested
    /// // child that's not a direct child of the root.
    /// assert_eq!(4, main_toc_root.total_len());
    ///
    /// let child = main_toc_root.get(1).unwrap();
    ///
    /// assert_eq!(1, child.len());
    /// assert_eq!(1, child.total_len());
    /// # Ok(())
    /// # }
    /// ```
    fn total_len(&self) -> usize {
        self.iter()
            .fold(0, |total, child| total + child.total_len() + 1)
    }
}

toc_entry_kind! {
    Acknowledgments => "acknowledgments",
    Afterword => "afterword",
    Appendix => "appendix",
    BackMatter => "backmatter",
    Bibliography => "bibliography",
    // https://idpf.org/epub/20/spec/OPF_2.0_final_spec.html#Section2.6
    // specifies "text" as **First "real" page of content (e.g. "Chapter 1")**.
    BodyMatter => "bodymatter" | "text",
    Chapter => "chapter",
    Colophon => "colophon",
    Conclusion => "conclusion",
    Contributors => "contributors",
    CopyrightPage => "copyright-page" | "copyright",
    Cover => "cover",
    Dedication => "dedication",
    Endnotes => "endnotes",
    Epigraph => "epigraph",
    Epilogue => "epilogue",
    Errata => "errata",
    Footnotes => "footnotes",
    Foreword => "foreword",
    FrontMatter => "frontmatter",
    Glossary => "glossary",
    Imprint => "imprint",
    Index => "index",
    Introduction => "introduction",
    Landmarks => "landmarks",
    ListOfIllustrations => "loi",
    ListOfAudio => "loa",
    ListOfTables => "lot",
    ListOfVideos => "lov",
    PageList => "page-list",
    Part => "part",
    Preamble => "preamble",
    Preface => "preface",
    Prologue => "prologue",
    Qna => "qna",
    TitlePage => "titlepage" | "title-page",
    Toc => "toc",
    Volume => "volume",
}

mod macros {
    macro_rules! toc_entry_kind {
        {
            $($map_enum:ident => $map_string:literal $(| $additional_mapping:literal)*,)*
        } => {
            /// The kinds of content that may be associated with table of content
            /// [entries](TocEntry).
            ///
            /// The variants are based on the EPUB 3 Structural Semantics Vocabulary.
            /// See more at: <https://www.w3.org/TR/epub-ssv-11>
            ///
            /// Uncommon semantics not directly included here are retrievable
            /// through [`TocEntryKind::Other`].
            #[non_exhaustive]
            #[derive(Copy, Clone, Debug, Default, Hash, PartialEq, Eq)]
            pub enum TocEntryKind<'ebook> {
                $(
                #[doc = concat!("Maps to `", $map_string, "`.")]
                $(
                #[doc = concat!("- `", $additional_mapping, "` → `", $map_string, "`")]
                )*
                ///
                /// More details at:
                #[doc = concat!("<https://www.w3.org/TR/epub-ssv-11/#", $map_string, ">.")]
                /// # Examples
                /// - Conversion from a string using [`TocEntryKind::from`]:
                /// ```
                #[doc = concat!(
                    " # use rbook::ebook::toc::TocEntryKind::{self, ",
                    stringify!($map_enum),
                    "};"
                )]
                #[doc = concat!(
                    "assert_eq!(TocEntryKind::",
                    stringify!($map_enum),
                    ", TocEntryKind::from(\"",
                    $map_string,
                    "\"))"
                )]
                /// ```
                $map_enum,
                )*
                /// An unknown entry kind.
                #[default]
                Unknown,
                /// An entry kind not mapped to any other variants.
                Other(&'ebook str),
            }

            impl TocEntryKind<'_> {
                /// Returns the string form of a [`TocEntryKind`].
                ///
                /// # Examples
                /// - Conversion from a string and comparison:
                /// ```
                /// # use rbook::ebook::toc::TocEntryKind;
                /// let title_page_kind = TocEntryKind::from("titlepage");
                /// let chapter_kind = TocEntryKind::from("chapter");
                ///
                /// assert_eq!("titlepage", title_page_kind.as_str());
                /// assert_eq!("chapter", chapter_kind.as_str());
                /// ```
                pub fn as_str(&self) -> &str {
                    match self {
                        $(Self::$map_enum => $map_string,)*
                        Self::Unknown => "unknown",
                        Self::Other(value) => value,
                    }
                }
            }

            impl<'ebook, S: AsRef<str> + ?Sized> From<&'ebook S> for TocEntryKind<'ebook> {
                fn from(value: &'ebook S) -> Self {
                    let value = value.as_ref();

                    match value {
                        $($map_string $(| $additional_mapping)* => Self::$map_enum,)*
                        "" => Self::Unknown,
                        _ => Self::Other(value)
                    }
                }
            }

            impl<'ebook> From<&'ebook Self> for TocEntryKind<'ebook> {
                fn from(value: &'ebook Self) -> Self {
                    match value {
                        $(Self::$map_enum => Self::$map_enum,)*
                        Self::Unknown => Self::Unknown,
                        Self::Other(other) => Self::Other(other)
                    }
                }
            }

            impl Display for TocEntryKind<'_> {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    f.write_str(self.as_str())
                }
            }
        };
    }

    pub(super) use toc_entry_kind;
}