mbook 0.3.6

Read, edit and create mbook files: Markdown chapters and AVIF pictures in one indexed container
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Lazy reading of an mbook file.

use crate::book::Book;
use crate::container;
use crate::error::{Error, ResolveError, Result};
use crate::format::{ANCHORS, CONTAINER_MAGIC, FORMAT, META, RESOURCES, SERVICE_COUNT, TOC};
use crate::id::{ChapterId, DiagramId, FormulaId, PictureId};
use crate::index::Index;
use crate::index::Resolved;
use crate::meta::Meta;
use crate::options::{Limits, ReadOptions};
use crate::types::{
    Anchor, ChapterEntry, Destination, DiagramEntry, FormulaEntry, LandmarkEntry, PictureEntry,
    Position, Resource, TocEntry, Version,
};
use mopenzl::TypedOutput;
use mopenzl::framing::{FramedDecompressor, FramedIndex};
use std::fs::File;
use std::io::{Cursor, Read, Seek, SeekFrom};
use std::path::Path;

/// Reads a book lazily from anything that can seek.
///
/// Opening a reader reads the directory of the file and its five small
/// tables: enough for the title, the cover and the contents. Each chapter,
/// picture, diagram and formula, and the anchor table of each chapter, is
/// read and decoded when it is asked for.
///
/// Methods that only look into the tables take `&self` and do no I/O. Methods
/// that touch the source start with `read_` and take `&mut self`.
///
/// A `BookReader` is `Send` when its source is. To decode on several threads,
/// open one reader per thread.
///
/// # Examples
///
/// ```no_run
/// use mbook::BookReader;
///
/// let mut book = BookReader::open("example.mbook")?;
/// println!("{}", book.meta().title());
/// for entry in book.toc() {
///     println!("{}{}", "  ".repeat(usize::from(entry.level) - 1), entry.title);
/// }
///
/// let first = book.chapters().next().map(|chapter| chapter.id);
/// if let Some(id) = first {
///     let text = book.read_chapter(id)?;
///     println!("{text}");
/// }
/// # Ok::<(), mbook::Error>(())
/// ```
#[derive(Debug)]
pub struct BookReader<R> {
    source: R,
    decoder: FramedDecompressor,
    /// Where every resource is in the source.
    directory: FramedIndex,
    /// The checked tables.
    index: Index,
    limits: Limits,
}

impl BookReader<File> {
    /// Opens the book stored in a file.
    ///
    /// # Errors
    ///
    /// The file cannot be opened, or as [`BookReader::new`].
    pub fn open(path: impl AsRef<Path>) -> Result<Self> {
        Self::new(File::open(path)?)
    }
}

impl BookReader<Cursor<Vec<u8>>> {
    /// Reads a whole stream into memory and opens the book in that buffer.
    ///
    /// Use it for a source that cannot seek. The buffer holds the compressed
    /// file; chapters and pictures are still decoded on demand. Nothing can
    /// be shown before the stream ends, because the directory is at the end
    /// of a file.
    ///
    /// # Errors
    ///
    /// The stream fails, or as [`BookReader::new`].
    pub fn buffered(mut reader: impl Read) -> Result<Self> {
        let mut bytes = Vec::new();
        reader.read_to_end(&mut bytes)?;
        Self::new(Cursor::new(bytes))
    }
}

impl<R: Read + Seek> BookReader<R> {
    /// Opens the book that runs from the current position of `source` to its
    /// end, with the default options.
    ///
    /// # Errors
    ///
    /// The source fails; the data is not an mbook file, declares a newer
    /// major version, or has a damaged or malformed table; a default limit is
    /// exceeded.
    pub fn new(source: R) -> Result<Self> {
        Self::with_options(source, &ReadOptions::default())
    }

    /// Opens a book with the given options.
    ///
    /// # Errors
    ///
    /// As [`BookReader::new`], with the limits of `options`.
    pub fn with_options(mut source: R, options: &ReadOptions) -> Result<Self> {
        let limits = options.limits;
        check_signature(&mut source)?;
        // One resource is decoded at a time, so nothing bounds their sum.
        let mut decoder = container::decoder(&limits, u64::MAX)?;
        let directory = decoder
            .framed_index(&mut source)
            .map_err(container::error)?;
        let mut read = |number: u32| {
            let at = usize::try_from(number).unwrap_or(usize::MAX);
            decoder
                .read_indexed_resource(&mut source, &directory, at)
                .map_err(container::error)
        };
        if directory.is_empty() {
            return Err(Error::NotMbook);
        }
        let version = container::header(&read(FORMAT)?.outputs)?;
        if directory.len() < SERVICE_COUNT {
            return Err(Error::Malformed {
                resource: FORMAT,
                reason: "the file has fewer than five resources",
            });
        }
        let meta = read(META)?;
        let resources = read(RESOURCES)?;
        let toc = read(TOC)?;
        let anchors = read(ANCHORS)?;
        let decoded_lens: Vec<u64> = directory.iter().map(|info| info.decoded_size).collect();
        let index = Index::from_outputs(
            version,
            [
                &meta.outputs,
                &resources.outputs,
                &toc.outputs,
                &anchors.outputs,
            ],
            &decoded_lens,
        )?;
        Ok(Self {
            source,
            decoder,
            directory,
            index,
            limits,
        })
    }

    /// The format version the file declares.
    #[must_use]
    pub fn version(&self) -> Version {
        self.index.version()
    }

    /// The metadata.
    #[must_use]
    pub fn meta(&self) -> &Meta {
        self.index.meta()
    }

    /// The table of contents: a tree listed in pre-order.
    #[must_use]
    pub fn toc(&self) -> &[TocEntry] {
        self.index.toc()
    }

    /// The chapters, in reading order.
    pub fn chapters(&self) -> impl ExactSizeIterator<Item = ChapterEntry<'_>> {
        self.index.chapters()
    }

    /// The pictures, in file order.
    pub fn pictures(&self) -> impl ExactSizeIterator<Item = PictureEntry<'_>> {
        self.index.pictures()
    }

    /// The diagrams, in file order.
    pub fn diagrams(&self) -> impl ExactSizeIterator<Item = DiagramEntry<'_>> {
        self.index.diagrams()
    }

    /// The formulas, in file order.
    pub fn formulas(&self) -> impl ExactSizeIterator<Item = FormulaEntry<'_>> {
        self.index.formulas()
    }

    /// What the tables say about a chapter. `None` for an id that came from
    /// another book.
    #[must_use]
    pub fn chapter(&self, id: ChapterId) -> Option<ChapterEntry<'_>> {
        self.index.chapter(id)
    }

    /// What the tables say about a picture. `None` for an id that came from
    /// another book.
    #[must_use]
    pub fn picture(&self, id: PictureId) -> Option<PictureEntry<'_>> {
        self.index.picture(id)
    }

    /// What the tables say about a diagram. `None` if this book has no diagram
    /// at the id's resource number.
    #[must_use]
    pub fn diagram(&self, id: DiagramId) -> Option<DiagramEntry<'_>> {
        self.index.diagram(id)
    }

    /// What the tables say about a formula. `None` if this book has no formula
    /// at the id's resource number.
    #[must_use]
    pub fn formula(&self, id: FormulaId) -> Option<FormulaEntry<'_>> {
        self.index.formula(id)
    }

    /// The chapter with this path.
    #[must_use]
    pub fn find_chapter(&self, path: &str) -> Option<ChapterEntry<'_>> {
        self.index.find_chapter(path)
    }

    /// The picture with this path.
    #[must_use]
    pub fn find_picture(&self, path: &str) -> Option<PictureEntry<'_>> {
        self.index.find_picture(path)
    }

    /// The diagram with this path.
    #[must_use]
    pub fn find_diagram(&self, path: &str) -> Option<DiagramEntry<'_>> {
        self.index.find_diagram(path)
    }

    /// The formula with this path.
    #[must_use]
    pub fn find_formula(&self, path: &str) -> Option<FormulaEntry<'_>> {
        self.index.find_formula(path)
    }

    /// The cover named by the metadata, when it is a picture of the book.
    #[must_use]
    pub fn cover(&self) -> Option<PictureEntry<'_>> {
        self.meta().cover().and_then(|path| self.find_picture(path))
    }

    /// The id of the chapter with this resource number, when that resource
    /// is a chapter. Use it to restore an id stored outside the program.
    #[must_use]
    pub fn chapter_id(&self, number: u32) -> Option<ChapterId> {
        self.index.chapter_id(number)
    }

    /// The id of the picture with this resource number, when that resource
    /// is a picture.
    #[must_use]
    pub fn picture_id(&self, number: u32) -> Option<PictureId> {
        self.index.picture_id(number)
    }

    /// The id of the diagram with this resource number, when that resource
    /// is a diagram.
    #[must_use]
    pub fn diagram_id(&self, number: u32) -> Option<DiagramId> {
        self.index.diagram_id(number)
    }

    /// The id of the formula with this resource number, when that resource
    /// is a formula.
    #[must_use]
    pub fn formula_id(&self, number: u32) -> Option<FormulaId> {
        self.index.formula_id(number)
    }

    /// The anchors of the headings of a chapter, sorted by offset. Empty for
    /// an id that came from another book.
    ///
    /// Anchors of other places are in the chapter's own table; see
    /// [`read_extra_anchors`](Self::read_extra_anchors).
    #[must_use]
    pub fn anchors(&self, chapter: ChapterId) -> &[Anchor] {
        self.index.anchors(chapter.number())
    }

    /// Tells what a link or picture destination found in a chapter points
    /// to.
    ///
    /// `destination` is taken after the Markdown parser has processed escapes
    /// and entities. The call does not know whether it came from a link or
    /// from a picture: a link that resolves to [`Destination::Picture`],
    /// [`Destination::Diagram`] or [`Destination::Formula`], or a picture
    /// that resolves to anything else, is unresolved.
    ///
    /// Nothing is read. A fragment that is not the anchor of a heading may
    /// be in the anchor table of its chapter: the result is then
    /// [`Destination::ExtraAnchor`], and
    /// [`read_resolve`](Self::read_resolve) finds the position.
    ///
    /// # Errors
    ///
    /// The destination leads nowhere; the caller shows the link text or the
    /// alternative text instead.
    pub fn resolve(&self, from: ChapterId, destination: &str) -> Result<Destination, ResolveError> {
        self.index.resolve(from, destination)
    }

    /// Tells what a destination points to, reading the anchor table of the
    /// target chapter when the fragment is not the anchor of a heading.
    ///
    /// It is [`resolve`](Self::resolve) for the moment a link is followed:
    /// the result is never [`Destination::ExtraAnchor`].
    ///
    /// The table cannot show whether an offset is at the start of a line;
    /// [`load`](Self::load), which has the text as well, checks that.
    ///
    /// # Errors
    ///
    /// [`Error::Unresolved`] when the destination leads nowhere; otherwise
    /// as [`read_extra_anchors`](Self::read_extra_anchors).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use mbook::{BookReader, Destination};
    ///
    /// let mut book = BookReader::open("example.mbook")?;
    /// let Some(from) = book.chapters().next().map(|chapter| chapter.id) else {
    ///     return Ok(());
    /// };
    /// // The user followed [the figure](ch01.md#fig-1).
    /// if let Destination::Chapter(position) = book.read_resolve(from, "ch01.md#fig-1")? {
    ///     println!("byte {} of chapter {}", position.offset, position.chapter.number());
    /// }
    /// # Ok::<(), mbook::Error>(())
    /// ```
    pub fn read_resolve(&mut self, from: ChapterId, destination: &str) -> Result<Destination> {
        match self.index.resolve_named(from, destination)? {
            Resolved::Known(destination) => Ok(destination),
            Resolved::Extra { chapter, name } => {
                let anchors = self.read_extra_anchors(chapter)?;
                let anchor = anchors.iter().find(|anchor| anchor.name == name);
                let offset = anchor.ok_or(ResolveError::UnknownAnchor)?.offset;
                Ok(Destination::Chapter(Position { chapter, offset }))
            }
        }
    }

    /// Every row of the resource table, including service rows and kinds
    /// this crate does not know. For tools that list a file.
    pub fn resources(&self) -> impl ExactSizeIterator<Item = Resource<'_>> {
        self.index.resources()
    }

    /// Reads and decodes one chapter.
    ///
    /// An error here does not affect other resources.
    ///
    /// # Errors
    ///
    /// The source fails; the record is damaged or larger than the limit; the
    /// text is not UTF-8, or a stored position in it is not at the start of
    /// a line; the id came from another book.
    pub fn read_chapter(&mut self, id: ChapterId) -> Result<String> {
        let number = id.number();
        if self.index.chapter_id(number).is_none() {
            return Err(Error::NoSuchResource(number));
        }
        let bytes = self.read_serial(number)?;
        self.index.text(number, bytes)
    }

    /// Reads the anchors a chapter has besides those of its headings, sorted
    /// by offset. Empty for a chapter without an anchor table.
    ///
    /// An error here does not affect other resources.
    ///
    /// # Errors
    ///
    /// The source fails; the record is damaged or larger than the limit; the
    /// table breaks a rule of the format; the id came from another book.
    pub fn read_extra_anchors(&mut self, chapter: ChapterId) -> Result<Vec<Anchor>> {
        let number = chapter.number();
        if self.index.chapter_id(number).is_none() {
            return Err(Error::NoSuchResource(number));
        }
        let Some(table) = self.index.anchor_table(number) else {
            return Ok(Vec::new());
        };
        let outputs = read_outputs(&mut self.decoder, &mut self.source, &self.directory, table)?;
        self.index.read_extra_anchors(number, table, &outputs)
    }

    /// Reads the landmarks of the book: its notable places, such as the
    /// start of the text or the index, in the order the book lists them.
    /// Empty for a book without landmarks.
    ///
    /// An error here does not affect the rest of the book.
    ///
    /// # Errors
    ///
    /// The source fails; the record is damaged or larger than the limit; the
    /// table breaks a rule of the format.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use mbook::BookReader;
    ///
    /// let mut book = BookReader::open("example.mbook")?;
    /// for landmark in book.read_landmarks()? {
    ///     println!("{}: resource {}", landmark.kind, landmark.position.chapter.number());
    /// }
    /// # Ok::<(), mbook::Error>(())
    /// ```
    pub fn read_landmarks(&mut self) -> Result<Vec<LandmarkEntry>> {
        let Some(table) = self.index.landmarks_table() else {
            return Ok(Vec::new());
        };
        let outputs = read_outputs(&mut self.decoder, &mut self.source, &self.directory, table)?;
        self.index.read_landmarks(table, &outputs)
    }

    /// Reads one picture as the bytes of an AVIF file.
    ///
    /// An error here does not affect other resources.
    ///
    /// # Errors
    ///
    /// The source fails, the record is damaged or larger than the limit, or
    /// the id came from another book.
    pub fn read_picture(&mut self, id: PictureId) -> Result<Vec<u8>> {
        let number = id.number();
        if self.index.picture_id(number).is_none() {
            return Err(Error::NoSuchResource(number));
        }
        self.read_serial(number)
    }

    /// Reads one diagram as its description in Mermaid notation.
    ///
    /// An error here does not affect other resources.
    ///
    /// # Errors
    ///
    /// The source fails; the record is damaged or larger than the limit; the
    /// description is not UTF-8; the number does not identify a diagram.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use mbook::BookReader;
    ///
    /// let mut book = BookReader::open("example.mbook")?;
    /// let ids: Vec<_> = book.diagrams().map(|diagram| diagram.id).collect();
    /// for id in ids {
    ///     println!("{}", book.read_diagram(id)?);
    /// }
    /// # Ok::<(), mbook::Error>(())
    /// ```
    pub fn read_diagram(&mut self, id: DiagramId) -> Result<String> {
        let number = id.number();
        if self.index.diagram_id(number).is_none() {
            return Err(Error::NoSuchResource(number));
        }
        let bytes = self.read_serial(number)?;
        Index::source(number, bytes)
    }

    /// Reads one formula in the notation of `KaTeX`.
    ///
    /// An error here does not affect other resources.
    ///
    /// # Errors
    ///
    /// The source fails; the record is damaged or larger than the limit; the
    /// formula is not UTF-8; the number does not identify a formula.
    pub fn read_formula(&mut self, id: FormulaId) -> Result<String> {
        let number = id.number();
        if self.index.formula_id(number).is_none() {
            return Err(Error::NoSuchResource(number));
        }
        let bytes = self.read_serial(number)?;
        Index::source(number, bytes)
    }

    /// Reads the cover as the bytes of an AVIF file; `None` when the book
    /// has no cover.
    ///
    /// # Errors
    ///
    /// As [`read_picture`](Self::read_picture).
    pub fn read_cover(&mut self) -> Result<Option<Vec<u8>>> {
        match self.cover().map(|cover| cover.id) {
            Some(id) => self.read_picture(id).map(Some),
            None => Ok(None),
        }
    }

    /// Decodes every chapter, picture, diagram and formula into a [`Book`]
    /// that can be changed.
    ///
    /// Resources of a kind this crate does not know are left behind.
    ///
    /// # Errors
    ///
    /// As [`read_chapter`](Self::read_chapter),
    /// [`read_picture`](Self::read_picture), [`read_diagram`](Self::read_diagram)
    /// and [`read_formula`](Self::read_formula), for any resource; or the
    /// decoded book is larger than the file limit.
    pub fn load(&mut self) -> Result<Book> {
        if self.index.content_len() > self.limits.max_file_bytes {
            return Err(Error::Limit("the decoded book exceeds the file limit"));
        }
        let Self {
            source,
            decoder,
            directory,
            index,
            ..
        } = self;
        Book::assemble(index, |number| {
            read_outputs(decoder, source, directory, number)
        })
    }

    /// Gives the source back.
    #[must_use]
    pub fn into_inner(self) -> R {
        self.source
    }

    /// Reads one resource that must decode to a single serial output.
    fn read_serial(&mut self, number: u32) -> Result<Vec<u8>> {
        let outputs = read_outputs(&mut self.decoder, &mut self.source, &self.directory, number)?;
        container::serial(number, outputs)
    }
}

/// Reads and decodes one resource.
fn read_outputs<R: Read + Seek>(
    decoder: &mut FramedDecompressor,
    source: &mut R,
    directory: &FramedIndex,
    number: u32,
) -> Result<Vec<TypedOutput>> {
    let at = usize::try_from(number).unwrap_or(usize::MAX);
    decoder
        .read_indexed_resource(source, directory, at)
        .map(|resource| resource.outputs)
        .map_err(container::error)
}

/// Tells a file that is not a framed container from a damaged one, leaving
/// the source where it was.
fn check_signature<R: Read + Seek>(source: &mut R) -> Result<()> {
    let start = source.stream_position()?;
    let mut signature = [0; CONTAINER_MAGIC.len()];
    let complete = match source.read_exact(&mut signature) {
        Ok(()) => true,
        Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => false,
        Err(error) => return Err(error.into()),
    };
    source.seek(SeekFrom::Start(start))?;
    if complete && signature == CONTAINER_MAGIC {
        Ok(())
    } else {
        Err(Error::NotMbook)
    }
}