mbook 0.3.7

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
//! Error types.

use crate::types::Version;

/// The result type of this crate.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Why an operation on a book failed.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// The source or the sink failed.
    #[error("I/O failed")]
    Io(#[from] std::io::Error),
    /// The file has no valid format header.
    #[error("not an mbook file")]
    NotMbook,
    /// The file declares a major version newer than this crate supports.
    #[error("format version {0} is newer than this library supports")]
    UnsupportedVersion(Version),
    /// The container or one of its frames is damaged.
    #[error("the container or a frame is damaged")]
    Container(#[source] Box<dyn std::error::Error + Send + Sync>),
    /// A table breaks a rule of the format.
    #[error("resource {resource} is malformed: {reason}")]
    Malformed {
        /// The number of the offending resource.
        resource: u32,
        /// The rule that is broken.
        reason: &'static str,
    },
    /// A configured limit was exceeded.
    #[error("limit exceeded: {0}")]
    Limit(&'static str),
    /// An id that came from another book was used to read a resource.
    #[error("resource {0} is not a chapter, picture, diagram or formula of this book")]
    NoSuchResource(u32),
    /// No chapter of the book has this path.
    #[error("nothing in the book has the path {0:?}")]
    NoSuchPath(String),
    /// A position in the reading order is past its end.
    #[error("index {index} is out of range for {len} chapters")]
    IndexOutOfRange {
        /// The index that was given.
        index: usize,
        /// The number of chapters in the book.
        len: usize,
    },
    /// A path breaks the rules of the format.
    #[error("invalid path {path:?}")]
    InvalidPath {
        /// The path that was given.
        path: String,
        /// The rule that is broken.
        #[source]
        reason: PathError,
    },
    /// The path is already used by a chapter, a picture, a diagram or a
    /// formula.
    #[error("the path {0:?} is already used")]
    DuplicatePath(String),
    /// The text of a chapter cannot be stored.
    #[error("invalid chapter text")]
    InvalidText(#[from] TextError),
    /// The bytes given for a picture are not a usable AVIF file.
    #[error("invalid picture: {0}")]
    InvalidPicture(&'static str),
    /// The source given for a diagram cannot be stored.
    #[error("invalid diagram")]
    InvalidDiagram(#[source] SourceError),
    /// The source given for a formula cannot be stored.
    #[error("invalid formula")]
    InvalidFormula(#[source] SourceError),
    /// A change to the metadata breaks the rules of the format.
    #[error("invalid metadata")]
    InvalidMeta(#[from] MetaError),
    /// A link or picture destination leads nowhere.
    #[error("the destination leads nowhere")]
    Unresolved(#[from] ResolveError),
    /// The landmarks given to a book break a rule of the format.
    #[error("invalid landmarks")]
    InvalidLandmark(#[from] LandmarkError),
    /// The book as a whole is not valid and was not written.
    #[error("the book cannot be written")]
    Invalid(#[from] Problems),
}

/// Why a path was refused.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum PathError {
    /// The path is empty.
    #[error("the path is empty")]
    Empty,
    /// The path starts or ends with `/`, or contains `//`.
    #[error("a segment is empty")]
    EmptySegment,
    /// A segment is `.` or `..`.
    #[error("a segment is `.` or `..`")]
    DotSegment,
    /// The path contains a backslash.
    #[error("the path contains a backslash")]
    Backslash,
    /// The path contains a character below U+0020.
    #[error("the path contains a control character")]
    ControlCharacter,
}

/// A character that a chapter, a diagram or a formula must not contain.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[error("control character U+{:04X} at byte {offset}", u32::from(*.character))]
#[non_exhaustive]
pub struct TextError {
    /// Where the character is, in bytes from the start of the text after
    /// line endings were normalised.
    pub offset: usize,
    /// The character.
    pub character: char,
}

/// Why the source of a diagram or of a formula was refused.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum SourceError {
    /// The source is empty or holds nothing but white space.
    #[error("the source is empty")]
    Empty,
    /// The source holds a control character other than line feed and tab.
    #[error("the source holds a character that is not allowed")]
    ForbiddenCharacter(#[from] TextError),
}

/// Why a change to the metadata was refused.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum MetaError {
    /// The key does not match `[a-z0-9]+(-[a-z0-9]+)*`.
    #[error("{0:?} is not a valid key")]
    InvalidKey(String),
    /// The key must be present and cannot be removed.
    #[error("the key {0:?} is required")]
    Required(String),
    /// The key holds one value and already has it.
    #[error("the key {0:?} holds one value")]
    SingleValued(String),
    /// The title is empty.
    #[error("the title is empty")]
    EmptyTitle,
}

/// Why the landmarks of a book were refused.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum LandmarkError {
    /// The kind does not match `[a-z0-9]+(-[a-z0-9]+)*`.
    #[error("{0:?} is not a valid kind of landmark")]
    InvalidKind(String),
    /// More than one landmark has the kind `start`.
    #[error("a book has one start")]
    RepeatedStart,
}

/// Why a link or picture destination leads nowhere.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ResolveError {
    /// The destination has a URI scheme other than `http`, `https` or
    /// `mailto`.
    #[error("the scheme is not allowed")]
    ForbiddenScheme,
    /// The destination contains `?`, starts with `/`, or does not decode to
    /// UTF-8.
    #[error("the destination is malformed")]
    Malformed,
    /// A `..` segment leaves the root of the book.
    #[error("the destination leaves the book")]
    OutsideRoot,
    /// No chapter, picture, diagram or formula has the resolved path.
    #[error("nothing in the book has that path")]
    NotFound,
    /// The chapter has no anchor with the name given after `#`.
    #[error("the chapter has no such anchor")]
    UnknownAnchor,
    /// A destination that names a picture, a diagram or a formula has a
    /// fragment.
    #[error("a picture cannot have a fragment")]
    FragmentOnPicture,
}

/// Every problem that keeps a book from being written.
///
/// Returned by `Book::check`, and inside [`Error::Invalid`] by the calls that
/// write a book.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("the book has {} problem(s)", .0.len())]
pub struct Problems(Vec<Problem>);

impl Problems {
    /// `Ok` when a check found nothing.
    #[cfg(feature = "write")]
    pub(crate) fn none_or(problems: Vec<Problem>) -> Result<(), Self> {
        if problems.is_empty() {
            Ok(())
        } else {
            Err(Self(problems))
        }
    }

    /// The problems, in the order they were found.
    pub fn iter(&self) -> std::slice::Iter<'_, Problem> {
        self.0.iter()
    }

    /// The number of problems; never zero.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Always `false`: a check that finds nothing returns `Ok`.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl<'a> IntoIterator for &'a Problems {
    type Item = &'a Problem;
    type IntoIter = std::slice::Iter<'a, Problem>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// One problem found when checking a book.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{}{kind}", location(.path.as_deref(), *.offset))]
pub struct Problem {
    /// The chapter, diagram or formula the problem is in, when it is in one.
    pub path: Option<String>,
    /// Where in it, in bytes from the start of its text.
    pub offset: Option<u32>,
    /// What is wrong.
    pub kind: ProblemKind,
}

/// Where a problem is, as a prefix for its message.
fn location(path: Option<&str>, offset: Option<u32>) -> String {
    match (path, offset) {
        (Some(path), Some(offset)) => format!("{path}, byte {offset}: "),
        (Some(path), None) => format!("{path}: "),
        (None, _) => String::new(),
    }
}

/// What is wrong with a book.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ProblemKind {
    /// A link leads to no chapter or anchor of the book.
    #[error("the link to {0:?} leads nowhere")]
    UnresolvedLink(String),
    /// A picture reference names no picture, diagram or formula of the book.
    #[error("the picture {0:?} is not in the book")]
    UnresolvedPicture(String),
    /// A link or a picture uses a URI scheme the format does not allow.
    #[error("the destination {0:?} uses a scheme that is not allowed")]
    ForbiddenScheme(String),
    /// The chapter contains raw HTML.
    #[error("raw HTML is not allowed")]
    RawHtml,
    /// The `cover` key names no picture of the book.
    #[error("the cover is not a picture of the book")]
    MissingCover,
    /// A chapter, a diagram or a formula holds a character the format does
    /// not allow: a byte order mark at its start, or a control character
    /// other than line feed and tab.
    #[error("the character U+{:04X} is not allowed", u32::from(*.0))]
    ForbiddenCharacter(char),
    /// A diagram or a formula is empty or holds nothing but white space.
    #[error("the source is empty")]
    EmptySource,
    /// A contents item, named by its title, leads to a chapter or an anchor
    /// that does not exist.
    #[error("the contents item {0:?} leads nowhere")]
    DanglingTocItem(String),
    /// A landmark, named by its kind, leads to a chapter or an anchor that
    /// does not exist.
    #[error("the landmark {0:?} leads nowhere")]
    DanglingLandmark(String),
    /// A contents item has an empty title.
    #[error("a contents item has no title")]
    EmptyTocTitle,
    /// The level of a contents item, named by its title, is 0 or more than
    /// one below the item before it.
    #[error("the level of the contents item {0:?} does not continue the tree")]
    TocLevel(String),
    /// A contents item, named by its title, comes before an item that
    /// precedes it in reading order.
    #[error("the contents item {0:?} is out of reading order")]
    TocOrder(String),
}

#[cfg(test)]
mod tests {
    use super::{
        Error, LandmarkError, MetaError, PathError, Problem, ProblemKind, Problems, ResolveError,
        SourceError, TextError,
    };
    use crate::types::Version;
    use std::error::Error as _;

    fn problem(path: Option<&str>, offset: Option<u32>) -> Problem {
        Problem {
            path: path.map(str::to_owned),
            offset,
            kind: ProblemKind::RawHtml,
        }
    }

    #[test]
    fn io_error_keeps_its_source() {
        let error = Error::from(std::io::Error::other("disk on fire"));
        let source = error.source().map(ToString::to_string);
        assert_eq!(source.as_deref(), Some("disk on fire"));
    }

    #[test]
    fn unsupported_version_names_the_version() {
        let error = Error::UnsupportedVersion(Version { major: 2, minor: 1 });
        assert_eq!(
            error.to_string(),
            "format version 2.1 is newer than this library supports"
        );
    }

    #[test]
    fn container_error_keeps_its_source() {
        let error = Error::Container("bad checksum".into());
        let source = error.source().map(ToString::to_string);
        assert_eq!(source.as_deref(), Some("bad checksum"));
    }

    #[test]
    fn malformed_names_the_resource_and_the_rule() {
        let error = Error::Malformed {
            resource: 3,
            reason: "levels skip",
        };
        assert_eq!(error.to_string(), "resource 3 is malformed: levels skip");
    }

    #[test]
    fn invalid_path_quotes_the_path() {
        let error = Error::InvalidPath {
            path: "a//b".into(),
            reason: PathError::EmptySegment,
        };
        assert_eq!(error.to_string(), r#"invalid path "a//b""#);
    }

    #[test]
    fn invalid_path_keeps_the_reason_as_its_source() {
        let error = Error::InvalidPath {
            path: "a//b".into(),
            reason: PathError::EmptySegment,
        };
        let source = error.source().map(ToString::to_string);
        assert_eq!(source.as_deref(), Some("a segment is empty"));
    }

    #[test]
    fn index_out_of_range_names_both_numbers() {
        let error = Error::IndexOutOfRange { index: 4, len: 3 };
        assert_eq!(error.to_string(), "index 4 is out of range for 3 chapters");
    }

    #[test]
    fn text_error_converts_into_invalid_text() {
        let text = TextError {
            offset: 7,
            character: '\u{1b}',
        };
        assert!(matches!(Error::from(text), Error::InvalidText(inner) if inner == text));
    }

    #[test]
    fn text_error_names_the_character_and_the_offset() {
        let text = TextError {
            offset: 7,
            character: '\u{1b}',
        };
        assert_eq!(text.to_string(), "control character U+001B at byte 7");
    }

    #[test]
    fn source_error_is_the_source_of_an_invalid_diagram_and_formula() {
        let diagram = Error::InvalidDiagram(SourceError::Empty);
        let formula = Error::InvalidFormula(SourceError::Empty);
        let sources = [&diagram, &formula].map(|error| error.source().map(ToString::to_string));
        assert_eq!(
            (diagram.to_string(), formula.to_string()),
            ("invalid diagram".to_owned(), "invalid formula".to_owned())
        );
        assert_eq!(sources.map(|source| source.is_some()), [true, true]);
    }

    #[test]
    fn a_text_error_is_the_source_of_a_forbidden_character_in_a_source() {
        let text = TextError {
            offset: 3,
            character: '\u{7}',
        };
        let error = SourceError::from(text);
        let source = error.source().map(ToString::to_string);
        assert_eq!(
            (error.to_string().as_str(), source.as_deref()),
            (
                "the source holds a character that is not allowed",
                Some("control character U+0007 at byte 3")
            )
        );
        assert_eq!(SourceError::Empty.to_string(), "the source is empty");
    }

    #[test]
    fn an_id_of_another_book_is_named_by_its_number() {
        assert_eq!(
            Error::NoSuchResource(9).to_string(),
            "resource 9 is not a chapter, picture, diagram or formula of this book"
        );
    }

    #[test]
    fn an_empty_source_has_a_message() {
        assert_eq!(ProblemKind::EmptySource.to_string(), "the source is empty");
    }

    #[test]
    fn meta_error_is_the_source_of_invalid_meta() {
        let error = Error::from(MetaError::Required("id".into()));
        let source = error.source().map(ToString::to_string);
        assert_eq!(source.as_deref(), Some(r#"the key "id" is required"#));
    }

    #[test]
    fn resolve_error_is_the_source_of_unresolved() {
        let error = Error::from(ResolveError::UnknownAnchor);
        let source = error.source().map(ToString::to_string);
        assert_eq!(
            (error.to_string().as_str(), source.as_deref()),
            (
                "the destination leads nowhere",
                Some("the chapter has no such anchor")
            )
        );
    }

    #[test]
    fn landmark_error_is_the_source_of_invalid_landmark() {
        let error = Error::from(LandmarkError::InvalidKind("The Index".into()));
        let source = error.source().map(ToString::to_string);
        assert_eq!(
            (error.to_string().as_str(), source.as_deref()),
            (
                "invalid landmarks",
                Some(r#""The Index" is not a valid kind of landmark"#)
            )
        );
        assert_eq!(
            LandmarkError::RepeatedStart.to_string(),
            "a book has one start"
        );
    }

    #[test]
    fn a_dangling_landmark_names_its_kind() {
        let kind = ProblemKind::DanglingLandmark("index".into());
        assert_eq!(kind.to_string(), r#"the landmark "index" leads nowhere"#);
    }

    #[test]
    fn resolve_error_has_a_message() {
        assert_eq!(
            ResolveError::UnknownAnchor.to_string(),
            "the chapter has no such anchor"
        );
    }

    #[test]
    fn problem_with_a_place_names_the_chapter_and_the_byte() {
        let text = problem(Some("ch01.md"), Some(130)).to_string();
        assert_eq!(text, "ch01.md, byte 130: raw HTML is not allowed");
    }

    #[test]
    fn problem_in_a_chapter_names_the_chapter() {
        let text = problem(Some("ch01.md"), None).to_string();
        assert_eq!(text, "ch01.md: raw HTML is not allowed");
    }

    #[test]
    fn problem_of_the_whole_book_has_no_prefix() {
        let whole = Problem {
            path: None,
            offset: None,
            kind: ProblemKind::MissingCover,
        };
        assert_eq!(whole.to_string(), "the cover is not a picture of the book");
    }

    #[test]
    fn problem_kind_quotes_the_destination() {
        let kind = ProblemKind::UnresolvedLink("ch09.md".into());
        assert_eq!(kind.to_string(), r#"the link to "ch09.md" leads nowhere"#);
    }

    #[test]
    fn problems_count_what_they_hold() {
        let problems = Problems(vec![problem(None, None), problem(None, None)]);
        assert_eq!((problems.len(), problems.is_empty()), (2, false));
    }

    #[test]
    fn problems_iterate_in_order() {
        let problems = Problems(vec![
            problem(Some("a.md"), None),
            problem(Some("b.md"), None),
        ]);
        let paths: Vec<_> = (&problems)
            .into_iter()
            .filter_map(|p| p.path.as_deref())
            .collect();
        assert_eq!(paths, ["a.md", "b.md"]);
    }

    #[test]
    fn problems_become_an_invalid_book_error() {
        let error = Error::from(Problems(vec![problem(None, None)]));
        let source = error.source().map(ToString::to_string);
        assert_eq!(source.as_deref(), Some("the book has 1 problem(s)"));
    }
}