mbook 0.3.0

Read, edit and create mbook files: Markdown chapters and AVIF pictures in one indexed container
Documentation
//! Loading a whole book into memory.

use super::{Book, Chapter, Diagram, Formula, Landmark, Picture, Toc, TocItem};
use crate::container;
use crate::error::{Error, Result};
use crate::format::{CONTAINER_MAGIC, FORMAT, SERVICE_COUNT};
use crate::index::Index;
use crate::options::ReadOptions;
use mopenzl::TypedOutput;
use std::io::{Cursor, Read};
use std::path::Path;

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

    /// Loads a book from a reader, decoding everything in one pass. The
    /// reader does not have to seek.
    ///
    /// Loading is lenient where the format tells readers to be: a chapter
    /// with a broken link loads, and the problem is reported when the book is
    /// written. Resources of a kind this crate does not know are left behind.
    ///
    /// # Errors
    ///
    /// The source fails, the data is not a valid mbook file, or a default
    /// limit is exceeded.
    pub fn read(reader: impl Read) -> Result<Self> {
        Self::read_with(reader, &ReadOptions::default())
    }

    /// Loads a book from a reader with the given options.
    ///
    /// The file limit of `options` also bounds the decoded size of the book,
    /// which is what a loaded book takes in memory.
    ///
    /// # Errors
    ///
    /// As [`Book::read`], with the limits of `options`.
    pub fn read_with(mut reader: impl Read, options: &ReadOptions) -> Result<Self> {
        let limits = options.limits;
        // Tell a file that is not a framed container from a damaged one.
        let mut signature = Vec::with_capacity(CONTAINER_MAGIC.len());
        let wanted = u64::try_from(CONTAINER_MAGIC.len()).unwrap_or(u64::MAX);
        reader.by_ref().take(wanted).read_to_end(&mut signature)?;
        if signature != CONTAINER_MAGIC {
            return Err(Error::NotMbook);
        }
        let mut decoder = container::decoder(&limits, limits.max_file_bytes)?;
        let mut resources = decoder.framed_reader(Cursor::new(signature).chain(reader));

        let mut version = None;
        let mut service = Vec::with_capacity(SERVICE_COUNT);
        let mut contents = Vec::new();
        let mut decoded_lens = Vec::new();
        while let Some(resource) = resources.read_resource().map_err(container::error)? {
            decoded_lens.push(resource.info.decoded_size);
            if service.len() < SERVICE_COUNT {
                if service.is_empty() {
                    // Fail before the rest of an unsupported file is decoded.
                    version = Some(container::header(&resource.outputs)?);
                }
                service.push(resource.outputs);
                contents.push(Vec::new());
            } else {
                contents.push(resource.outputs);
            }
        }
        let version = version.ok_or(Error::NotMbook)?;
        let [_, meta, table, toc, anchors] = service.as_slice() else {
            return Err(Error::Malformed {
                resource: FORMAT,
                reason: "the file has fewer than five resources",
            });
        };
        let index = Index::from_outputs(version, [meta, table, toc, anchors], &decoded_lens)?;
        Self::assemble(&index, |number| {
            // Every resource is asked for once, so its outputs can be moved.
            let outputs = usize::try_from(number)
                .ok()
                .and_then(|at| contents.get_mut(at))
                .map(std::mem::take);
            Ok(outputs.unwrap_or_default())
        })
    }

    /// Builds the model from checked tables. `outputs` gives the decoded
    /// content of a resource by its number.
    pub(crate) fn assemble(
        index: &Index,
        mut outputs: impl FnMut(u32) -> Result<Vec<TypedOutput>>,
    ) -> Result<Self> {
        let mut book = Self::empty(index.meta().clone());
        for entry in index.chapters() {
            let number = entry.id.number();
            // The table of a chapter is the resource before it.
            let table = index.anchor_table(number);
            let extra_anchors = match table {
                Some(table) => index.read_extra_anchors(number, table, &outputs(table)?)?,
                None => Vec::new(),
            };
            let text = index.text(number, container::serial(number, outputs(number)?)?)?;
            if let Some(table) = table {
                Index::check_extra_positions(table, &extra_anchors, &text)?;
            }
            let chapter = Chapter {
                path: entry.path.to_owned(),
                text,
                // The anchors stored in the file are kept until the text of
                // the chapter is replaced.
                anchors: index.anchors(number).to_vec(),
                extra_anchors,
            };
            book.place_chapter(book.chapters.len(), chapter);
        }
        for entry in index.pictures() {
            let number = entry.id.number();
            book.place_picture(Picture {
                path: entry.path.to_owned(),
                size: entry.size,
                data: container::serial(number, outputs(number)?)?,
            });
        }
        for entry in index.diagrams() {
            let number = entry.id.number();
            let source = Index::source(number, container::serial(number, outputs(number)?)?)?;
            book.place_diagram(Diagram {
                path: entry.path.to_owned(),
                source,
            });
        }
        for entry in index.formulas() {
            let number = entry.id.number();
            let source = Index::source(number, container::serial(number, outputs(number)?)?)?;
            book.place_formula(Formula {
                path: entry.path.to_owned(),
                source,
            });
        }
        book.toc = Toc::Explicit(
            index
                .toc()
                .iter()
                .map(|entry| toc_item(index, entry))
                .collect(),
        );
        if let Some(table) = index.landmarks_table() {
            for entry in index.read_landmarks(table, &outputs(table)?)? {
                let position = entry.position;
                let chapter = index
                    .chapter(position.chapter)
                    .and_then(|chapter| book.chapter(chapter.path));
                // A landmark inside a chapter is named by the anchor at its
                // offset, of a heading or of the chapter's own table.
                let anchor = chapter.and_then(|chapter| {
                    let anchors = chapter.anchors.iter().chain(&chapter.extra_anchors);
                    anchors
                        .clone()
                        .find(|anchor| anchor.offset == position.offset)
                        .map(|anchor| anchor.name.clone())
                });
                if position.offset != 0 && anchor.is_none() {
                    return Err(Error::Malformed {
                        resource: table,
                        reason: "a row inside a chapter has no anchor",
                    });
                }
                book.landmarks.push(Landmark {
                    kind: entry.kind,
                    title: entry.title,
                    chapter: chapter.map_or_else(String::new, |chapter| chapter.path.clone()),
                    anchor: anchor.filter(|_| position.offset != 0),
                });
            }
        }
        Ok(book)
    }
}

/// A stored contents row, named the way a link names its target.
fn toc_item(index: &Index, entry: &crate::types::TocEntry) -> TocItem {
    let position = entry.position;
    let chapter = index.chapter(position.chapter);
    // A row inside a chapter always has an anchor at its offset.
    let anchor = (position.offset != 0)
        .then(|| index.anchors(position.chapter.number()))
        .and_then(|anchors| {
            anchors
                .iter()
                .find(|anchor| anchor.offset == position.offset)
        })
        .map(|anchor| anchor.name.clone());
    TocItem {
        level: entry.level,
        title: entry.title.clone(),
        chapter: chapter.map_or_else(String::new, |chapter| chapter.path.to_owned()),
        anchor,
    }
}