markdown-frontmatter 0.5.1

A type-safe markdown frontmatter parser
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]

/// The format of the frontmatter.
#[derive(Debug, Clone, Copy, PartialEq)]
enum FrontmatterFormat {
    /// JSON frontmatter, denoted by `{...}`.
    Json,
    /// TOML frontmatter, denoted by `+++...+++`.
    Toml,
    /// YAML frontmatter, denoted by `---...---`.
    Yaml,
}

impl From<FrontmatterFormat> for &'static str {
    fn from(format: FrontmatterFormat) -> Self {
        match format {
            FrontmatterFormat::Json => "JSON",
            FrontmatterFormat::Toml => "TOML",
            FrontmatterFormat::Yaml => "YAML",
        }
    }
}

/// The crate's error type
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Frontmatter format is disabled.
    #[error("disabled format {0}, enable corresponding cargo feature")]
    DisabledFormat(&'static str),
    /// Closing delimiter is absent.
    #[error("absent closing {0} delimiter")]
    AbsentClosingDelimiter(&'static str),

    #[cfg(feature = "json")]
    /// Invalid JSON syntax.
    #[error("invalid JSON syntax")]
    InvalidJson(#[source] serde_json::Error),
    #[cfg(feature = "toml")]
    /// Invalid TOML syntax.
    #[error("invalid TOML syntax")]
    InvalidToml(#[source] toml::de::Error),
    #[cfg(feature = "yaml")]
    /// Invalid YAML syntax.
    #[error("invalid YAML syntax")]
    InvalidYaml(#[source] serde_yaml::Error),

    #[cfg(feature = "json")]
    /// Couldn't deserialize JSON into the target type.
    #[error("couldn't deserialize JSON")]
    DeserializeJson(#[source] serde_json::Error),
    #[cfg(feature = "toml")]
    /// Couldn't deserialize TOML into the target type.
    #[error("couldn't deserialize TOML")]
    DeserializeToml(#[source] toml::de::Error),
    #[cfg(feature = "yaml")]
    /// Couldn't deserialize YAML into the target type.
    #[error("couldn't deserialize YAML")]
    DeserializeYaml(#[source] serde_yaml::Error),
}

#[cfg(any(feature = "json", feature = "toml", feature = "yaml"))]
/// Parses frontmatter from a markdown string, deserializing it into a given
/// type and returning the parsed frontmatter and the body of the document.
///
/// # Arguments
///
/// * `content` - The content of the document to parse.
///
/// # Examples
///
/// ```
/// use markdown_frontmatter::parse;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct MyFrontmatter {
///     title: String,
/// }
///
/// let doc = r#"---
/// title: Hello
/// ---
/// World
/// "#;
///
/// let (frontmatter, body) = parse::<MyFrontmatter>(doc).unwrap();
/// assert_eq!(frontmatter.title, "Hello");
/// assert_eq!(body, "World\n");
/// ```
pub fn parse<T: serde::de::DeserializeOwned>(content: &str) -> Result<(T, &str), Error> {
    let (maybe_frontmatter, body) = split(content)?;
    let SplitFrontmatter(format, matter_str) = maybe_frontmatter.unwrap_or_default();
    let frontmatter = format.parse(matter_str)?;
    Ok((frontmatter, body))
}

#[derive(Debug, Clone, Copy)]
struct SplitFrontmatter<'a>(FrontmatterFormat, &'a str);

#[cfg(any(feature = "json", feature = "toml", feature = "yaml"))]
impl Default for SplitFrontmatter<'_> {
    fn default() -> Self {
        #[cfg(feature = "json")]
        {
            Self(FrontmatterFormat::Json, "{}")
        }
        #[cfg(all(not(feature = "json"), feature = "toml"))]
        {
            Self(FrontmatterFormat::Toml, "")
        }
        #[cfg(all(not(any(feature = "json", feature = "toml")), feature = "yaml"))]
        {
            Self(FrontmatterFormat::Yaml, "{}")
        }
    }
}

/// Splits a document into frontmatter and body, returning the raw frontmatter
/// string and the body of the document.
fn split(content: &str) -> Result<(Option<SplitFrontmatter<'_>>, &str), Error> {
    let content = content.trim_start();
    let mut lines = LineSpan::new(content);

    let Some(span) = lines.next() else {
        // Empty document
        return Ok((None, content));
    };

    let Some(format) = FrontmatterFormat::detect(span.line) else {
        // No frontmatter
        return Ok((None, content));
    };

    let matter_start = match format {
        FrontmatterFormat::Json => span.start, // include opening curly bracket,
        FrontmatterFormat::Toml | FrontmatterFormat::Yaml => span.next_start,
    };

    let closing_delimiter = format.delimiter().1;
    for span in lines {
        if span.line != closing_delimiter {
            continue;
        }
        let (matter, body) = match format {
            FrontmatterFormat::Json => (
                &content[matter_start..span.next_start], // include closing curly bracket
                &content[span.next_start..],
            ),
            FrontmatterFormat::Toml | FrontmatterFormat::Yaml => (
                &content[matter_start..span.start], // exclude closing delimiter
                &content[span.next_start..],
            ),
        };
        return Ok((Some(SplitFrontmatter(format, matter)), body));
    }
    Err(Error::AbsentClosingDelimiter(format.into()))
}

impl FrontmatterFormat {
    const VARIANTS: [Self; 3] = [Self::Json, Self::Toml, Self::Yaml];

    /// Detects the frontmatter format from the first line of a document.
    fn detect(first_line: &str) -> Option<Self> {
        Self::VARIANTS
            .into_iter()
            .find(|&variant| first_line == variant.delimiter().0)
    }

    #[cfg(any(feature = "json", feature = "toml", feature = "yaml"))]
    fn parse<T: serde::de::DeserializeOwned>(&self, matter_str: &str) -> Result<T, Error> {
        match self {
            #[cfg(feature = "json")]
            Self::Json => {
                let json: serde_json::Value =
                    serde_json::from_str(matter_str).map_err(Error::InvalidJson)?;
                serde_json::from_value(json).map_err(Error::DeserializeJson)
            }
            #[cfg(not(feature = "json"))]
            Self::Json => Err(Error::DisabledFormat(Self::Json.into())),

            #[cfg(feature = "toml")]
            Self::Toml => {
                let toml: toml::Value = toml::from_str(matter_str).map_err(Error::InvalidToml)?;
                toml.try_into().map_err(Error::DeserializeToml)
            }
            #[cfg(not(feature = "toml"))]
            Self::Toml => Err(Error::DisabledFormat(Self::Toml.into())),

            #[cfg(feature = "yaml")]
            Self::Yaml => {
                let yaml: serde_yaml::Value =
                    serde_yaml::from_str(matter_str).map_err(Error::InvalidYaml)?;
                serde_yaml::from_value(yaml).map_err(Error::DeserializeYaml)
            }
            #[cfg(not(feature = "yaml"))]
            Self::Yaml => Err(Error::DisabledFormat(Self::Yaml.into())),
        }
    }

    fn delimiter(&self) -> (&'static str, &'static str) {
        match self {
            Self::Json => ("{", "}"),
            Self::Toml => ("+++", "+++"),
            Self::Yaml => ("---", "---"),
        }
    }
}

struct LineSpan<'a> {
    pub start: usize,
    pub next_start: usize,
    pub line: &'a str,
}

impl<'a> LineSpan<'a> {
    fn new(s: &'a str) -> impl Iterator<Item = LineSpan<'a>> + 'a {
        let bytes = s.as_bytes();
        let mut pos = 0;
        std::iter::from_fn(move || {
            if pos >= bytes.len() {
                return None;
            }
            let start = pos;
            let mut i = start;
            while i < bytes.len() && bytes[i] != b'\n' && bytes[i] != b'\r' {
                i += 1;
            }
            let line_end = i;
            if i < bytes.len() && bytes[i] == b'\r' {
                i += 1;
                if i < bytes.len() && bytes[i] == b'\n' {
                    i += 1;
                }
            } else if i < bytes.len() && bytes[i] == b'\n' {
                i += 1;
            }
            let line = &s[start..line_end];
            let next_start = i;
            pos = i;
            Some(LineSpan {
                start,
                next_start,
                line,
            })
        })
    }
}

#[cfg(test)]
mod test_line_span {
    use super::*;

    #[test]
    fn line_span() {
        let input = "line 1\r\nline 2\nline 3";
        let mut lines = LineSpan::new(input);

        let line1 = lines.next().unwrap();
        assert_eq!(line1.line, "line 1");
        assert_eq!(line1.start, 0);
        assert_eq!(line1.next_start, 8);

        let line2 = lines.next().unwrap();
        assert_eq!(line2.line, "line 2");
        assert_eq!(line2.start, 8);
        assert_eq!(line2.next_start, 15);

        let line3 = lines.next().unwrap();
        assert_eq!(line3.line, "line 3");
        assert_eq!(line3.start, 15);
        assert_eq!(line3.next_start, 21);

        assert!(lines.next().is_none());
    }
}

#[cfg(test)]
mod test_split {
    use super::*;

    #[test]
    fn empty_document() {
        let input = "";
        let (frontmatter, body) = split(input).unwrap();
        assert!(frontmatter.is_none());
        assert_eq!(body, "");
    }

    #[test]
    fn no_frontmatter() {
        let input = "hello world";
        let (frontmatter, body) = split(input).unwrap();
        assert!(frontmatter.is_none());
        assert_eq!(body, "hello world");
    }

    #[test]
    fn unclosed_json() {
        let input = "{\n\t\"foo\": \"bar\"\n";
        let result = split(input);
        assert!(matches!(
            result.unwrap_err(),
            Error::AbsentClosingDelimiter("JSON")
        ));
    }

    #[test]
    fn unclosed_toml() {
        let input = "+++\nfoo = \"bar\"";
        let result = split(input);
        assert!(matches!(
            result.unwrap_err(),
            Error::AbsentClosingDelimiter("TOML")
        ));
    }

    #[test]
    fn unclosed_yaml() {
        let input = "---\nfoo: bar";
        let result = split(input);
        assert!(matches!(
            result.unwrap_err(),
            Error::AbsentClosingDelimiter("YAML")
        ));
    }

    #[test]
    fn json_singleline() {
        let input = "{\n\t\"foo\": \"bar\"\n}\nhello world";
        let (frontmatter, body) = split(input).unwrap();
        assert_eq!(frontmatter.unwrap().1, "{\n\t\"foo\": \"bar\"\n}\n");
        assert_eq!(frontmatter.unwrap().0, FrontmatterFormat::Json);
        assert_eq!(body, "hello world");
    }

    #[test]
    fn json_multiline() {
        let input = "{\n\t\"foo\": \"bar\",\n\t\"baz\": 1\n}\nhello world";
        let (frontmatter, body) = split(input).unwrap();
        assert_eq!(
            frontmatter.unwrap().1,
            "{\n\t\"foo\": \"bar\",\n\t\"baz\": 1\n}\n"
        );
        assert_eq!(frontmatter.unwrap().0, FrontmatterFormat::Json);
        assert_eq!(body, "hello world");
    }

    #[test]
    fn toml_singleline() {
        let input = "+++\nfoo = \"bar\"\n+++\nhello world";
        let (frontmatter, body) = split(input).unwrap();
        assert_eq!(frontmatter.unwrap().1, "foo = \"bar\"\n");
        assert_eq!(frontmatter.unwrap().0, FrontmatterFormat::Toml);
        assert_eq!(body, "hello world");
    }

    #[test]
    fn toml_multiline() {
        let input = "+++\nfoo = \"bar\"\nbaz = 1\n+++\nhello world";
        let (frontmatter, body) = split(input).unwrap();
        assert_eq!(frontmatter.unwrap().1, "foo = \"bar\"\nbaz = 1\n");
        assert_eq!(frontmatter.unwrap().0, FrontmatterFormat::Toml);
        assert_eq!(body, "hello world");
    }

    #[test]
    fn yaml_singleline() {
        let input = "---\nfoo: bar\n---\nhello world";
        let (frontmatter, body) = split(input).unwrap();
        assert_eq!(frontmatter.unwrap().1, "foo: bar\n");
        assert_eq!(frontmatter.unwrap().0, FrontmatterFormat::Yaml);
        assert_eq!(body, "hello world");
    }

    #[test]
    fn yaml_multiline() {
        let input = "---\nfoo: bar\nbaz: 1\n---\nhello world";
        let (frontmatter, body) = split(input).unwrap();
        assert_eq!(frontmatter.unwrap().1, "foo: bar\nbaz: 1\n");
        assert_eq!(frontmatter.unwrap().0, FrontmatterFormat::Yaml);
        assert_eq!(body, "hello world");
    }
}

#[cfg(all(test, any(feature = "json", feature = "toml", feature = "yaml")))]
mod test_parse {
    use serde::Deserialize;

    use super::*;

    #[derive(Debug, PartialEq, Deserialize)]
    struct OptionalFrontmatter {
        foo: Option<bool>,
    }

    #[derive(Debug, PartialEq, Deserialize)]
    struct RequiredFrontmatter {
        foo: bool,
    }

    #[derive(Debug, PartialEq, Deserialize)]
    struct EmptyFrontmatter {}

    const EMPTY_DOCUMENT: &str = "";
    const DOCUMENT_WITHOUT_FRONTMATTER: &str = "hello world";

    const EMPTY_FRONTMATTER: EmptyFrontmatter = EmptyFrontmatter {};
    const OPTIONAL_FRONTMATTER_SOME: OptionalFrontmatter = OptionalFrontmatter { foo: Some(true) };
    const OPTIONAL_FRONTMATTER_NONE: OptionalFrontmatter = OptionalFrontmatter { foo: None };
    const REQUIRED_FRONTMATTER: RequiredFrontmatter = RequiredFrontmatter { foo: true };

    #[cfg(feature = "json")]
    mod json {
        use super::*;

        const VALID_DOCUMENT: &str = "{\n\t\"foo\": true\n}\nhello world";
        const INVALID_SYNTAX: &str = "{\n1\n}";
        const INVALID_TYPE: &str = "{\n\t\"foo\": 0\n}";

        #[test]
        fn empty_frontmatter_in_empty_document() {
            let (frontmatter, body) = parse::<EmptyFrontmatter>(EMPTY_DOCUMENT).unwrap();
            assert_eq!(frontmatter, EmptyFrontmatter {});
            assert_eq!(body, "");
        }

        #[test]
        fn optional_frontmatter_in_empty_document() {
            let (frontmatter, body) = parse::<OptionalFrontmatter>(EMPTY_DOCUMENT).unwrap();
            assert_eq!(frontmatter.foo, None);
            assert_eq!(body, "");
        }

        #[test]
        fn required_frontmatter_in_empty_document() {
            let result = parse::<RequiredFrontmatter>(EMPTY_DOCUMENT);
            assert!(matches!(result.unwrap_err(), Error::DeserializeJson(..)));
        }

        #[test]
        fn empty_frontmatter_in_document_without_frontmatter() {
            let (frontmatter, body) =
                parse::<EmptyFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER).unwrap();
            assert_eq!(frontmatter, EMPTY_FRONTMATTER);
            assert_eq!(body, DOCUMENT_WITHOUT_FRONTMATTER);
        }

        #[test]
        fn optional_frontmatter_in_document_without_frontmatter() {
            let (frontmatter, body) =
                parse::<OptionalFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER).unwrap();
            assert_eq!(frontmatter, OPTIONAL_FRONTMATTER_NONE);
            assert_eq!(body, DOCUMENT_WITHOUT_FRONTMATTER);
        }

        #[test]
        fn required_frontmatter_in_document_without_frontmatter() {
            let result = parse::<RequiredFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER);
            assert!(matches!(result.unwrap_err(), Error::DeserializeJson(..)));
        }

        #[test]
        fn optional_frontmatter_in_valid_document() {
            let (frontmatter, body) = parse::<OptionalFrontmatter>(VALID_DOCUMENT).unwrap();
            assert_eq!(frontmatter, OPTIONAL_FRONTMATTER_SOME);
            assert_eq!(body, "hello world");
        }

        #[test]
        fn required_frontmatter_in_valid_document() {
            let (frontmatter, body) = parse::<RequiredFrontmatter>(VALID_DOCUMENT).unwrap();
            assert_eq!(frontmatter, REQUIRED_FRONTMATTER);
            assert_eq!(body, "hello world");
        }

        #[test]
        fn optional_frontmatter_invalid_syntax() {
            let result = parse::<OptionalFrontmatter>(INVALID_SYNTAX);
            assert!(matches!(result.unwrap_err(), Error::InvalidJson(..)));
        }

        #[test]
        fn required_frontmatter_invalid_syntax() {
            let result = parse::<RequiredFrontmatter>(INVALID_SYNTAX);
            assert!(matches!(result.unwrap_err(), Error::InvalidJson(..)));
        }

        #[test]
        fn optional_frontmatter_invalid_type() {
            let result = parse::<OptionalFrontmatter>(INVALID_TYPE);
            assert!(matches!(result.unwrap_err(), Error::DeserializeJson(..)));
        }

        #[test]
        fn required_frontmatter_invalid_type() {
            let result = parse::<RequiredFrontmatter>(INVALID_TYPE);
            assert!(matches!(result.unwrap_err(), Error::DeserializeJson(..)));
        }
    }

    #[cfg(feature = "toml")]
    mod toml {
        use super::*;

        const VALID_DOCUMENT: &str = "+++\nfoo = true\n+++\nhello world";
        const INVALID_SYNTAX: &str = "+++\nfoobar\n+++\n";
        const INVALID_TYPE: &str = "+++\nfoo = 123\n+++\n";

        #[cfg(not(any(feature = "json", feature = "yaml")))]
        mod only {
            use super::*;

            #[test]
            fn empty_frontmatter_in_empty_document() {
                let (frontmatter, body) = parse::<EmptyFrontmatter>(EMPTY_DOCUMENT).unwrap();
                assert_eq!(frontmatter, EmptyFrontmatter {});
                assert_eq!(body, "");
            }

            #[test]
            fn optional_frontmatter_in_empty_document() {
                let (frontmatter, body) = parse::<OptionalFrontmatter>(EMPTY_DOCUMENT).unwrap();
                assert_eq!(frontmatter.foo, None);
                assert_eq!(body, "");
            }

            #[test]
            fn required_frontmatter_in_empty_document() {
                let result = parse::<RequiredFrontmatter>(EMPTY_DOCUMENT);
                assert!(matches!(result.unwrap_err(), Error::DeserializeToml(..)));
            }

            #[test]
            fn empty_frontmatter_in_document_without_frontmatter() {
                let (frontmatter, body) =
                    parse::<EmptyFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER).unwrap();
                assert_eq!(frontmatter, EMPTY_FRONTMATTER);
                assert_eq!(body, DOCUMENT_WITHOUT_FRONTMATTER);
            }

            #[test]
            fn optional_frontmatter_in_document_without_frontmatter() {
                let (frontmatter, body) =
                    parse::<OptionalFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER).unwrap();
                assert_eq!(frontmatter, OPTIONAL_FRONTMATTER_NONE);
                assert_eq!(body, DOCUMENT_WITHOUT_FRONTMATTER);
            }

            #[test]
            fn required_frontmatter_in_document_without_frontmatter() {
                let result = parse::<RequiredFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER);
                assert!(matches!(result.unwrap_err(), Error::DeserializeToml(..)));
            }
        }

        #[test]
        fn optional_frontmatter_in_valid_document() {
            let (frontmatter, body) = parse::<OptionalFrontmatter>(VALID_DOCUMENT).unwrap();
            assert_eq!(frontmatter, OPTIONAL_FRONTMATTER_SOME);
            assert_eq!(body, "hello world");
        }

        #[test]
        fn required_frontmatter_in_valid_document() {
            let (frontmatter, body) = parse::<RequiredFrontmatter>(VALID_DOCUMENT).unwrap();
            assert_eq!(frontmatter, REQUIRED_FRONTMATTER);
            assert_eq!(body, "hello world");
        }

        #[test]
        fn optional_frontmatter_invalid_syntax() {
            let result = parse::<OptionalFrontmatter>(INVALID_SYNTAX);
            assert!(matches!(result.unwrap_err(), Error::InvalidToml(..)));
        }

        #[test]
        fn required_frontmatter_invalid_syntax() {
            let result = parse::<RequiredFrontmatter>(INVALID_SYNTAX);
            assert!(matches!(result.unwrap_err(), Error::InvalidToml(..)));
        }

        #[test]
        fn optional_frontmatter_invalid_type() {
            let result = parse::<OptionalFrontmatter>(INVALID_TYPE);
            assert!(matches!(result.unwrap_err(), Error::DeserializeToml(..)));
        }

        #[test]
        fn required_frontmatter_invalid_type() {
            let result = parse::<RequiredFrontmatter>(INVALID_TYPE);
            assert!(matches!(result.unwrap_err(), Error::DeserializeToml(..)));
        }
    }

    #[cfg(feature = "yaml")]
    mod yaml {
        use super::*;

        const VALID_DOCUMENT: &str = "---\nfoo: true\n---\nhello world";
        const INVALID_SYNTAX: &str = "---\n:\n---\n";
        const INVALID_TYPE: &str = "---\nfoo: 123\n---\n";

        #[cfg(not(any(feature = "json", feature = "toml")))]
        mod only {
            use super::*;

            #[test]
            fn empty_frontmatter_in_empty_document() {
                let (frontmatter, body) = parse::<EmptyFrontmatter>(EMPTY_DOCUMENT).unwrap();
                assert_eq!(frontmatter, EmptyFrontmatter {});
                assert_eq!(body, "");
            }

            #[test]
            fn optional_frontmatter_in_empty_document() {
                let (frontmatter, body) = parse::<OptionalFrontmatter>(EMPTY_DOCUMENT).unwrap();
                assert_eq!(frontmatter.foo, None);
                assert_eq!(body, "");
            }

            #[test]
            fn required_frontmatter_in_empty_document() {
                let result = parse::<RequiredFrontmatter>(EMPTY_DOCUMENT);
                assert!(matches!(result.unwrap_err(), Error::DeserializeYaml(..)));
            }

            #[test]
            fn empty_frontmatter_in_document_without_frontmatter() {
                let (frontmatter, body) =
                    parse::<EmptyFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER).unwrap();
                assert_eq!(frontmatter, EMPTY_FRONTMATTER);
                assert_eq!(body, DOCUMENT_WITHOUT_FRONTMATTER);
            }

            #[test]
            fn optional_frontmatter_in_document_without_frontmatter() {
                let (frontmatter, body) =
                    parse::<OptionalFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER).unwrap();
                assert_eq!(frontmatter, OPTIONAL_FRONTMATTER_NONE);
                assert_eq!(body, DOCUMENT_WITHOUT_FRONTMATTER);
            }

            #[test]
            fn required_frontmatter_in_document_without_frontmatter() {
                let result = parse::<RequiredFrontmatter>(DOCUMENT_WITHOUT_FRONTMATTER);
                assert!(matches!(result.unwrap_err(), Error::DeserializeYaml(..)));
            }
        }

        #[test]
        fn optional_frontmatter_in_valid_document() {
            let (frontmatter, body) = parse::<OptionalFrontmatter>(VALID_DOCUMENT).unwrap();
            assert_eq!(frontmatter, OPTIONAL_FRONTMATTER_SOME);
            assert_eq!(body, "hello world");
        }

        #[test]
        fn required_frontmatter_in_valid_document() {
            let (frontmatter, body) = parse::<RequiredFrontmatter>(VALID_DOCUMENT).unwrap();
            assert_eq!(frontmatter, REQUIRED_FRONTMATTER);
            assert_eq!(body, "hello world");
        }

        #[test]
        fn optional_frontmatter_invalid_syntax() {
            let result = parse::<OptionalFrontmatter>(INVALID_SYNTAX);
            assert!(matches!(result.unwrap_err(), Error::InvalidYaml(..)));
        }

        #[test]
        fn required_frontmatter_invalid_syntax() {
            let result = parse::<RequiredFrontmatter>(INVALID_SYNTAX);
            assert!(matches!(result.unwrap_err(), Error::InvalidYaml(..)));
        }

        #[test]
        fn optional_frontmatter_invalid_type() {
            let result = parse::<OptionalFrontmatter>(INVALID_TYPE);
            assert!(matches!(result.unwrap_err(), Error::DeserializeYaml(..)));
        }

        #[test]
        fn required_frontmatter_invalid_type() {
            let result = parse::<RequiredFrontmatter>(INVALID_TYPE);
            assert!(matches!(result.unwrap_err(), Error::DeserializeYaml(..)));
        }
    }
}