carta-core 0.0.8

Shared conversion options, error types, and text/attribute helpers.
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
//! Format extensions: the set of optional syntax features a reader or writer may honor.
//!
//! [`Extension`] is one named feature; [`Extensions`] is a deterministic, allocation-free set of them
//! backed by a fixed array of 64-bit words. [`presets`] holds the per-flavor sets; strict
//! `CommonMark` is the empty set.

/// Generates the [`Extension`] enum together with the `ALL`/`COUNT`/`name` metadata, keeping the
/// variant list as the single source of truth for the bitset sizing in [`Extensions`].
macro_rules! define_extensions {
    ($($(#[$attribute:meta])* $variant:ident => $name:literal),+ $(,)?) => {
        /// A single format extension. Each variant's position in [`Extension::ALL`] is its bit
        /// index in [`Extensions`].
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[non_exhaustive]
        pub enum Extension { $($(#[$attribute])* $variant),+ }

        impl Extension {
            /// Every extension, in declaration order.
            pub const ALL: &'static [Extension] = &[$(Extension::$variant),+];
            /// The number of distinct extensions.
            pub const COUNT: usize = Self::ALL.len();

            /// The extension's identifier (e.g. `"footnotes"`).
            #[must_use]
            pub const fn name(self) -> &'static str {
                match self { $(Extension::$variant => $name),+ }
            }

            /// The extension named `name`, or `None` if no extension uses that identifier.
            #[must_use]
            pub fn from_name(name: &str) -> Option<Extension> {
                match name { $($name => Some(Extension::$variant),)+ _ => None }
            }
        }
    };
}

define_extensions! {
    /// Straight quotes, `...`, `--`, and `---` become curly quotes, an ellipsis, and en/em dashes.
    Smart => "smart",
    /// `~~text~~` strikeout spans.
    Strikeout => "strikeout",
    /// `^text^` superscript spans.
    Superscript => "superscript",
    /// `~text~` subscript spans.
    Subscript => "subscript",
    /// Pipe tables: `|`-separated cells with a delimiter row carrying the column alignments.
    PipeTables => "pipe_tables",
    /// `[^label]` footnote references with separately defined note bodies.
    Footnotes => "footnotes",
    /// `- [ ]` / `- [x]` task-list items.
    TaskLists => "task_lists",
    /// A bare absolute URI or `www.` address in running text becomes a link.
    Autolink => "autolink_bare_uris",
    /// `$…$` inline and `$$…$$` display math.
    TexMathDollars => "tex_math_dollars",
    /// `:::`-fenced divs carrying an attribute block or a bare class name.
    FencedDivs => "fenced_divs",
    /// `[text]{.class}` spans: bracketed text followed by an attribute block.
    BracketedSpans => "bracketed_spans",
    /// Every newline within a paragraph is a hard line break.
    HardLineBreaks => "hard_line_breaks",
    /// Raw HTML tags and blocks are carried through rather than treated as text.
    RawHtml => "raw_html",
    /// A `{#id .class key=val}` attribute block on a header line.
    HeaderAttributes => "header_attributes",
    /// An attribute block on a fenced code block's opening line.
    FencedCodeAttributes => "fenced_code_attributes",
    /// An attribute block after an inline code span.
    InlineCodeAttributes => "inline_code_attributes",
    /// An attribute block after a link or image.
    LinkAttributes => "link_attributes",
    /// The combined attribute toggle: the attribute syntaxes enabled as a group.
    Attributes => "attributes",
    /// Definition lists: a term line followed by `:`-marked definition blocks.
    DefinitionLists => "definition_lists",
    /// Grid tables drawn with `+---+` cell borders.
    GridTables => "grid_tables",
    /// Multiline tables, whose cells may continue across several source lines.
    MultilineTables => "multiline_tables",
    /// Simple tables: columns aligned under a dashed header line.
    SimpleTables => "simple_tables",
    /// A `Table:` (or bare `:`) caption line attached to a table.
    TableCaptions => "table_captions",
    /// `|`-prefixed line blocks, preserving the source's line divisions.
    LineBlocks => "line_blocks",
    /// Ordered-list markers beyond decimal numbers: letters, roman numerals, and `)` delimiters.
    FancyLists => "fancy_lists",
    /// `(@label)` example lists, numbered sequentially across the whole document.
    ExampleLists => "example_lists",
    /// An ordered list starts at the number its first marker carries rather than 1.
    Startnum => "startnum",
    /// A `---`-delimited YAML metadata block.
    YamlMetadataBlock => "yaml_metadata_block",
    /// A `%`-prefixed title/author/date block at the top of the document.
    PandocTitleBlock => "pandoc_title_block",
    /// A header without an explicit identifier gets one derived from its text.
    AutoIdentifiers => "auto_identifiers",
    /// Derived header identifiers use the `GitHub` slug form: lowercased, punctuation dropped,
    /// spaces to hyphens.
    GfmAutoIdentifiers => "gfm_auto_identifiers",
    /// Fold a derived identifier down to ASCII, dropping diacritics before the slug is formed.
    AsciiIdentifiers => "ascii_identifiers",
    /// A header's explicit identifier is written in `MultiMarkdown`'s trailing `[id]` form rather
    /// than the `{#id}` attribute block.
    MmdHeaderIdentifiers => "mmd_header_identifiers",
    /// A header's own text works as a reference-link label for that header.
    ImplicitHeaderReferences => "implicit_header_references",
    /// A bare image with a caption becomes a figure.
    ImplicitFigures => "implicit_figures",
    /// Raw passthrough: `` `code`{=fmt} `` inline and ```` ```{=fmt} ```` fenced blocks.
    RawAttribute => "raw_attribute",
    /// A `^[…]` inline note expands to a footnote in place.
    InlineNotes => "inline_notes",
    /// A block-level `<div>` becomes a `Div`, with Markdown parsed inside.
    NativeDivs => "native_divs",
    /// An inline `<span>` becomes a `Span`, with Markdown parsed inside.
    NativeSpans => "native_spans",
    /// Markdown is parsed inside block-level HTML, which is otherwise split tag-by-tag.
    MarkdownInHtmlBlocks => "markdown_in_html_blocks",
    /// A `<div>`/`<span>` emitted for a div/span carries a `data-markdown="1"` marker so its
    /// contents are still parsed as Markdown; this also forces a div with no native syntax into an
    /// HTML wrap.
    MarkdownAttribute => "markdown_attribute",
    /// Inline raw `TeX` (`\command{…}`, `\begin{env}…\end{env}`) passes through verbatim.
    RawTex => "raw_tex",
    /// `[@key]` / `@key` citation references.
    Citations => "citations",
    /// An attribute block on a table's caption line attaches to the table.
    TableAttributes => "table_attributes",
    /// A blank line is required before a blockquote, so one never interrupts a paragraph.
    BlankBeforeBlockquote => "blank_before_blockquote",
    /// A blank line is required before a header, so one never interrupts a paragraph.
    BlankBeforeHeader => "blank_before_header",
    /// `==text==` highlight spans.
    Mark => "mark",
    /// `:name:` emoji shortcodes.
    Emoji => "emoji",
    /// `> [!NOTE]`-style admonition blockquotes become classed divs.
    Alerts => "alerts",
    /// `\(…\)` inline and `\[…\]` display math delimiters.
    TexMathSingleBackslash => "tex_math_single_backslash",
    /// `\\(…\\)` inline and `\\[…\\]` display math delimiters.
    TexMathDoubleBackslash => "tex_math_double_backslash",
    /// Tilde-fenced (`~~~`) code blocks; with no fence form available, code is written in the
    /// four-space indented form.
    FencedCodeBlocks => "fenced_code_blocks",
    /// Backtick-fenced code blocks.
    BacktickCodeBlocks => "backtick_code_blocks",
    /// The `GitHub` math surface: inline `` $`…`$ `` and a ```` ```math ```` display block, as
    /// opposed to the `$…$`/`$$…$$` dollar form.
    TexMathGfm => "tex_math_gfm",
    /// A backslash at a line's end is a hard line break, written as a trailing `\`; without it the
    /// writer falls back to two trailing spaces.
    EscapedLineBreaks => "escaped_line_breaks",
    /// An underscore inside a word opens no emphasis, so the writer leaves intra-word `_` literal;
    /// without it every `_` is escaped so a strict reader cannot start emphasis mid-word.
    IntrawordUnderscores => "intraword_underscores",
    /// A list may begin directly after a paragraph line with no intervening blank line,
    /// interrupting it; without it a list marker on the line after a paragraph folds into that
    /// paragraph.
    ListsWithoutPrecedingBlankline => "lists_without_preceding_blankline",
    /// `*[SHY]: Soft hyphen` abbreviation definitions, applied to later occurrences of the term.
    Abbreviations => "abbreviations",
    /// A backslash escapes any symbol, not only the ASCII-punctuation subset.
    AllSymbolsEscapable => "all_symbols_escapable",
    /// A backslash before `<` or `>` escapes the angle bracket.
    AngleBracketsEscapable => "angle_brackets_escapable",
    /// Line breaks between East Asian wide characters carry no width and are dropped.
    EastAsianLineBreaks => "east_asian_line_breaks",
    /// An indented code block requires four spaces of indentation rather than one tab stop.
    FourSpaceRule => "four_space_rule",
    /// Typographic conventions of the Project Gutenberg style for plain-text output.
    Gutenberg => "gutenberg",
    /// Soft line breaks within a paragraph are discarded rather than kept as spaces.
    IgnoreLineBreaks => "ignore_line_breaks",
    /// User-defined `LaTeX` macros are expanded in math and raw `TeX`.
    LatexMacros => "latex_macros",
    /// Bird-track (`> `) literate-program code sections.
    LiterateHaskell => "literate_haskell",
    /// An attribute block following a link or image in the `MultiMarkdown` position.
    MmdLinkAttributes => "mmd_link_attributes",
    /// A `MultiMarkdown` metadata block at the top of the document.
    MmdTitleBlock => "mmd_title_block",
    /// `-` and `--` map to en/em dashes under the older dash convention.
    OldDashes => "old_dashes",
    /// A raw block or inline may be written directly as Markdown for round-tripping.
    RawMarkdown => "raw_markdown",
    /// Relative paths in links and images are rebased onto the source file's location.
    RebaseRelativePaths => "rebase_relative_paths",
    /// `~x` / `^x` subscript and superscript bind only the single following character.
    ShortSubsuperscripts => "short_subsuperscripts",
    /// A defined label may be referenced by `[label]` alone, with no following `[]` or `(…)`.
    ShortcutReferenceLinks => "shortcut_reference_links",
    /// An ATX header requires a space between the opening `#` run and the heading text.
    SpaceInAtxHeader => "space_in_atx_header",
    /// A reference link's label and its following `[id]` may be separated by whitespace.
    SpacedReferenceLinks => "spaced_reference_links",
    /// `[[target|title]]` wiki links, with the title following the pipe.
    WikilinksTitleAfterPipe => "wikilinks_title_after_pipe",
    /// `[[title|target]]` wiki links, with the title preceding the pipe.
    WikilinksTitleBeforePipe => "wikilinks_title_before_pipe",
    /// A `Div`/`Span`/`CodeBlock` carrying a `custom-style` attribute renders with that named
    /// paragraph or character style rather than the built-in body style.
    Styles => "styles",
    /// Figures and tables are auto-numbered by the target's own field mechanism, with a caption
    /// label prefix, instead of carrying a number baked into the caption text.
    NativeNumbering => "native_numbering",
    /// An empty paragraph in the document model is preserved in the output rather than dropped.
    EmptyParagraphs => "empty_paragraphs",
}

const WORD_BITS: usize = u64::BITS as usize;
const WORDS: usize = Extension::COUNT.div_ceil(WORD_BITS);

// `from_list` indexing is sound only while each discriminant equals its `ALL` position; the
// assertion turns a contiguity break into a build failure, not an out-of-bounds index.
#[allow(clippy::indexing_slicing)]
const _: () = {
    let mut i = 0;
    while i < Extension::ALL.len() {
        assert!(Extension::ALL[i] as usize == i);
        i += 1;
    }
};

/// A deterministic, allocation-free set of [`Extension`]s, backed by a fixed array of 64-bit words
/// indexed by each variant's position in [`Extension::ALL`].
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Extensions([u64; WORDS]);

impl Default for Extensions {
    fn default() -> Self {
        Self::empty()
    }
}

impl Extensions {
    /// The empty set (strict `CommonMark`).
    #[must_use]
    pub const fn empty() -> Self {
        Self([0; WORDS])
    }

    /// The set containing exactly `list`. Const so presets are `const` values.
    #[must_use]
    // In bounds by the contiguity assertion; slice `get` is not const-usable on the pinned toolchain.
    #[allow(clippy::indexing_slicing)]
    pub const fn from_list(list: &[Extension]) -> Self {
        let mut words = [0u64; WORDS];
        let mut i = 0;
        while i < list.len() {
            let bit = list[i] as usize;
            words[bit / WORD_BITS] |= 1u64 << (bit % WORD_BITS);
            i += 1;
        }
        Self(words)
    }

    /// Whether `ext` is in the set.
    #[must_use]
    pub fn contains(self, ext: Extension) -> bool {
        let bit = ext as usize;
        self.0
            .get(bit / WORD_BITS)
            .is_some_and(|word| (word >> (bit % WORD_BITS)) & 1 == 1)
    }

    /// Adds `ext` to the set.
    pub fn insert(&mut self, ext: Extension) {
        let bit = ext as usize;
        if let Some(word) = self.0.get_mut(bit / WORD_BITS) {
            *word |= 1u64 << (bit % WORD_BITS);
        }
    }

    /// Removes `ext` from the set.
    pub fn remove(&mut self, ext: Extension) {
        let bit = ext as usize;
        if let Some(word) = self.0.get_mut(bit / WORD_BITS) {
            *word &= !(1u64 << (bit % WORD_BITS));
        }
    }

    /// The union of this set and `other`.
    #[must_use]
    pub fn union(self, other: Extensions) -> Extensions {
        let mut words = self.0;
        for (word, &add) in words.iter_mut().zip(other.0.iter()) {
            *word |= add;
        }
        Extensions(words)
    }

    /// Whether the set is empty.
    #[must_use]
    pub fn is_empty(self) -> bool {
        self.0.iter().all(|&word| word == 0)
    }

    /// The set's extensions in [`Extension::ALL`] (deterministic) order.
    pub fn iter(self) -> impl Iterator<Item = Extension> {
        Extension::ALL
            .iter()
            .copied()
            .filter(move |&ext| self.contains(ext))
    }
}

impl core::fmt::Debug for Extensions {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_set()
            .entries(self.iter().map(Extension::name))
            .finish()
    }
}

/// Per-flavor extension sets.
pub mod presets {
    use super::{Extension, Extensions};

    /// Strict `CommonMark`: no extensions.
    pub const COMMONMARK: Extensions = Extensions::empty();

    /// `GitHub`-Flavored Markdown.
    pub const GFM: Extensions = Extensions::from_list(&[
        Extension::Strikeout,
        Extension::PipeTables,
        Extension::BacktickCodeBlocks,
        Extension::TaskLists,
        Extension::Autolink,
        Extension::Footnotes,
        Extension::TexMathDollars,
        Extension::TexMathGfm,
        Extension::GfmAutoIdentifiers,
        Extension::RawHtml,
        Extension::Emoji,
        Extension::Alerts,
    ]);

    /// The `commonmark_x` dialect: `CommonMark` with a broad set of inline and block extensions
    /// enabled. `backtick_code_blocks` is additionally carried because the shared Markdown engine
    /// fences code on that flag, which `CommonMark` does natively.
    pub const COMMONMARK_X: Extensions = Extensions::from_list(&[
        Extension::Smart,
        Extension::Strikeout,
        Extension::Superscript,
        Extension::Subscript,
        Extension::PipeTables,
        Extension::Footnotes,
        Extension::TaskLists,
        Extension::TexMathDollars,
        Extension::FencedDivs,
        Extension::BracketedSpans,
        Extension::BacktickCodeBlocks,
        Extension::RawHtml,
        Extension::RawAttribute,
        Extension::Attributes,
        Extension::HeaderAttributes,
        Extension::FencedCodeAttributes,
        Extension::InlineCodeAttributes,
        Extension::LinkAttributes,
        Extension::DefinitionLists,
        Extension::FancyLists,
        Extension::GfmAutoIdentifiers,
        Extension::ImplicitHeaderReferences,
        Extension::Emoji,
        Extension::Alerts,
    ]);

    /// The extended Markdown dialect: the broad default extension set.
    pub const MARKDOWN: Extensions = Extensions::from_list(&[
        Extension::AllSymbolsEscapable,
        Extension::Smart,
        Extension::Strikeout,
        Extension::Superscript,
        Extension::Subscript,
        Extension::PipeTables,
        Extension::Footnotes,
        Extension::TaskLists,
        Extension::TexMathDollars,
        Extension::FencedDivs,
        Extension::BracketedSpans,
        Extension::RawHtml,
        Extension::HeaderAttributes,
        Extension::FencedCodeAttributes,
        Extension::FencedCodeBlocks,
        Extension::BacktickCodeBlocks,
        Extension::InlineCodeAttributes,
        Extension::LinkAttributes,
        Extension::DefinitionLists,
        Extension::GridTables,
        Extension::MultilineTables,
        Extension::SimpleTables,
        Extension::TableCaptions,
        Extension::LineBlocks,
        Extension::FancyLists,
        Extension::ExampleLists,
        Extension::Startnum,
        Extension::YamlMetadataBlock,
        Extension::PandocTitleBlock,
        Extension::AutoIdentifiers,
        Extension::ImplicitHeaderReferences,
        Extension::ImplicitFigures,
        Extension::RawAttribute,
        Extension::InlineNotes,
        Extension::NativeDivs,
        Extension::NativeSpans,
        Extension::MarkdownInHtmlBlocks,
        Extension::RawTex,
        Extension::Citations,
        Extension::TableAttributes,
        Extension::BlankBeforeBlockquote,
        Extension::BlankBeforeHeader,
        Extension::EscapedLineBreaks,
        Extension::IntrawordUnderscores,
        Extension::SpaceInAtxHeader,
    ]);

    /// The legacy GitHub Markdown dialect (`markdown_github`): the variants that exist and affect
    /// writer output. No smart typography, math, spans, or fenced divs.
    pub const MARKDOWN_GITHUB: Extensions = Extensions::from_list(&[
        Extension::Strikeout,
        Extension::PipeTables,
        Extension::Footnotes,
        Extension::TaskLists,
        Extension::Autolink,
        Extension::RawHtml,
        Extension::FencedCodeBlocks,
        Extension::BacktickCodeBlocks,
        Extension::AutoIdentifiers,
        Extension::GfmAutoIdentifiers,
        Extension::Emoji,
        Extension::Alerts,
        Extension::IntrawordUnderscores,
    ]);

    /// The PHP Markdown Extra dialect (`markdown_phpextra`): the variants that exist and affect
    /// writer output. It has no backtick code fences, so code fences are written with tildes, and
    /// no smart typography, math, strikeout, spans, or fenced divs.
    pub const MARKDOWN_PHPEXTRA: Extensions = Extensions::from_list(&[
        Extension::DefinitionLists,
        Extension::FencedCodeBlocks,
        Extension::Footnotes,
        Extension::HeaderAttributes,
        Extension::IntrawordUnderscores,
        Extension::LinkAttributes,
        Extension::MarkdownAttribute,
        Extension::PipeTables,
        Extension::RawHtml,
    ]);

    /// The `MultiMarkdown` dialect (`markdown_mmd`): the variants that exist and affect writer
    /// output. It has no header attribute blocks, strikeout, task lists, smart typography, spans,
    /// or fenced divs. With `tex_math_dollars` on and taking precedence, a `tex_math_double_backslash`
    /// surface would not change this dialect's writer output, so it is left out of the preset and math
    /// is emitted as `$…$`.
    pub const MARKDOWN_MMD: Extensions = Extensions::from_list(&[
        Extension::AutoIdentifiers,
        Extension::BacktickCodeBlocks,
        Extension::DefinitionLists,
        Extension::Footnotes,
        Extension::ImplicitFigures,
        Extension::ImplicitHeaderReferences,
        Extension::IntrawordUnderscores,
        Extension::MarkdownAttribute,
        Extension::MmdHeaderIdentifiers,
        Extension::PipeTables,
        Extension::RawAttribute,
        Extension::RawHtml,
        Extension::Subscript,
        Extension::Superscript,
        Extension::TexMathDollars,
    ]);

    /// The original Markdown dialect (`markdown_strict`): only raw HTML. With no fenced or backtick
    /// code, tables, definition lists, footnotes, task lists, math, or any attribute syntax, every
    /// richer construct falls back to indented code, an HTML block, or a raw glyph. Lacking
    /// `intraword_underscores`, every `_` is escaped; lacking `pipe_tables`, a literal `|` is left
    /// unescaped.
    pub const MARKDOWN_STRICT: Extensions = Extensions::from_list(&[Extension::RawHtml]);

    // Reader defaults are broader than writer presets: readers enable every parseable construct.
    // Some entries are not yet branched on by the engine; recorded so they take effect once modeled.

    /// Reader defaults for the extended Markdown dialect (`markdown`): the broad writer set plus the
    /// two constructs the writer preset omits, user-defined `LaTeX` macro expansion and the shortcut
    /// reference-link form.
    pub const MARKDOWN_READ: Extensions = Extensions::from_list(&[
        Extension::AllSymbolsEscapable,
        Extension::AutoIdentifiers,
        Extension::BacktickCodeBlocks,
        Extension::BlankBeforeBlockquote,
        Extension::BlankBeforeHeader,
        Extension::BracketedSpans,
        Extension::Citations,
        Extension::DefinitionLists,
        Extension::EscapedLineBreaks,
        Extension::ExampleLists,
        Extension::FancyLists,
        Extension::FencedCodeAttributes,
        Extension::FencedCodeBlocks,
        Extension::FencedDivs,
        Extension::Footnotes,
        Extension::GridTables,
        Extension::HeaderAttributes,
        Extension::ImplicitFigures,
        Extension::ImplicitHeaderReferences,
        Extension::InlineCodeAttributes,
        Extension::InlineNotes,
        Extension::IntrawordUnderscores,
        Extension::LatexMacros,
        Extension::LineBlocks,
        Extension::LinkAttributes,
        Extension::MarkdownInHtmlBlocks,
        Extension::MultilineTables,
        Extension::NativeDivs,
        Extension::NativeSpans,
        Extension::PandocTitleBlock,
        Extension::PipeTables,
        Extension::RawAttribute,
        Extension::RawHtml,
        Extension::RawTex,
        Extension::ShortcutReferenceLinks,
        Extension::SimpleTables,
        Extension::Smart,
        Extension::SpaceInAtxHeader,
        Extension::Startnum,
        Extension::Strikeout,
        Extension::Subscript,
        Extension::Superscript,
        Extension::TableAttributes,
        Extension::TableCaptions,
        Extension::TaskLists,
        Extension::TexMathDollars,
        Extension::YamlMetadataBlock,
    ]);

    /// Reader defaults for the original Markdown dialect (`markdown_strict`): only raw HTML, plus the
    /// shortcut and spaced reference-link forms.
    pub const MARKDOWN_STRICT_READ: Extensions = Extensions::from_list(&[
        Extension::RawHtml,
        Extension::ShortcutReferenceLinks,
        Extension::SpacedReferenceLinks,
    ]);

    /// Reader defaults for the GitHub Markdown dialect (`markdown_github`): the GitHub construct
    /// set, including lists that open without a preceding blank line and the escaping and
    /// heading-spacing leniencies.
    pub const MARKDOWN_GITHUB_READ: Extensions = Extensions::from_list(&[
        Extension::Alerts,
        Extension::AllSymbolsEscapable,
        Extension::AutoIdentifiers,
        Extension::Autolink,
        Extension::BacktickCodeBlocks,
        Extension::Emoji,
        Extension::FencedCodeBlocks,
        Extension::Footnotes,
        Extension::GfmAutoIdentifiers,
        Extension::IntrawordUnderscores,
        Extension::ListsWithoutPrecedingBlankline,
        Extension::PipeTables,
        Extension::RawHtml,
        Extension::ShortcutReferenceLinks,
        Extension::SpaceInAtxHeader,
        Extension::Strikeout,
        Extension::TaskLists,
    ]);

    /// Reader defaults for the PHP Markdown Extra dialect (`markdown_phpextra`): the writer set
    /// plus abbreviations and the reference-link forms.
    pub const MARKDOWN_PHPEXTRA_READ: Extensions = Extensions::from_list(&[
        Extension::Abbreviations,
        Extension::DefinitionLists,
        Extension::FencedCodeBlocks,
        Extension::Footnotes,
        Extension::HeaderAttributes,
        Extension::IntrawordUnderscores,
        Extension::LinkAttributes,
        Extension::MarkdownAttribute,
        Extension::PipeTables,
        Extension::RawHtml,
        Extension::ShortcutReferenceLinks,
        Extension::SpacedReferenceLinks,
    ]);

    /// Reader defaults for the `MultiMarkdown` dialect (`markdown_mmd`): the writer set plus
    /// `MultiMarkdown`'s link-attribute and title-block forms, single-character sub/superscripts,
    /// the reference-link forms, the all-symbols escaping leniency, and the double-backslash math
    /// delimiters.
    pub const MARKDOWN_MMD_READ: Extensions = Extensions::from_list(&[
        Extension::AllSymbolsEscapable,
        Extension::AutoIdentifiers,
        Extension::BacktickCodeBlocks,
        Extension::DefinitionLists,
        Extension::Footnotes,
        Extension::ImplicitFigures,
        Extension::ImplicitHeaderReferences,
        Extension::IntrawordUnderscores,
        Extension::MarkdownAttribute,
        Extension::MmdHeaderIdentifiers,
        Extension::MmdLinkAttributes,
        Extension::MmdTitleBlock,
        Extension::PipeTables,
        Extension::RawAttribute,
        Extension::RawHtml,
        Extension::ShortSubsuperscripts,
        Extension::ShortcutReferenceLinks,
        Extension::SpacedReferenceLinks,
        Extension::Subscript,
        Extension::Superscript,
        Extension::TexMathDollars,
        Extension::TexMathDoubleBackslash,
    ]);

    /// Reader defaults for `GitHub`-Flavored Markdown (`gfm`): the writer set plus recognition of a
    /// `---`-delimited YAML metadata block, which the writer preset omits.
    pub const GFM_READ: Extensions = Extensions::from_list(&[
        Extension::Alerts,
        Extension::Autolink,
        Extension::BacktickCodeBlocks,
        Extension::Emoji,
        Extension::Footnotes,
        Extension::GfmAutoIdentifiers,
        Extension::PipeTables,
        Extension::RawHtml,
        Extension::Strikeout,
        Extension::TaskLists,
        Extension::TexMathDollars,
        Extension::TexMathGfm,
        Extension::YamlMetadataBlock,
    ]);

    /// Reader defaults for the `commonmark_x` dialect: the writer set plus recognition of a
    /// `---`-delimited YAML metadata block, which the writer preset omits.
    pub const COMMONMARK_X_READ: Extensions = Extensions::from_list(&[
        Extension::Alerts,
        Extension::Attributes,
        Extension::BacktickCodeBlocks,
        Extension::BracketedSpans,
        Extension::DefinitionLists,
        Extension::Emoji,
        Extension::FancyLists,
        Extension::FencedCodeAttributes,
        Extension::FencedDivs,
        Extension::Footnotes,
        Extension::GfmAutoIdentifiers,
        Extension::HeaderAttributes,
        Extension::ImplicitHeaderReferences,
        Extension::InlineCodeAttributes,
        Extension::LinkAttributes,
        Extension::PipeTables,
        Extension::RawAttribute,
        Extension::RawHtml,
        Extension::Smart,
        Extension::Strikeout,
        Extension::Subscript,
        Extension::Superscript,
        Extension::TaskLists,
        Extension::TexMathDollars,
        Extension::YamlMetadataBlock,
    ]);
}

#[cfg(test)]
mod tests {
    use super::{Extension, Extensions, presets};

    #[test]
    fn words_cover_every_variant() {
        // Every variant's bit index must land inside the backing array.
        for ext in Extension::ALL {
            assert!((*ext as usize) / super::WORD_BITS < super::WORDS);
        }
    }

    #[test]
    fn insert_remove_contains_round_trip() {
        let mut set = Extensions::empty();
        assert!(set.is_empty());
        assert!(!set.contains(Extension::Footnotes));
        set.insert(Extension::Footnotes);
        assert!(set.contains(Extension::Footnotes));
        assert!(!set.is_empty());
        set.remove(Extension::Footnotes);
        assert!(!set.contains(Extension::Footnotes));
        assert!(set.is_empty());
    }

    #[test]
    fn from_list_and_iter_follow_declaration_order() {
        let set = Extensions::from_list(&[Extension::PipeTables, Extension::Smart]);
        let collected: Vec<Extension> = set.iter().collect();
        // `iter` yields in `ALL` order, regardless of `from_list` argument order.
        assert_eq!(collected, vec![Extension::Smart, Extension::PipeTables]);
    }

    #[test]
    fn commonmark_preset_is_empty_gfm_is_not() {
        assert!(presets::COMMONMARK.is_empty());
        assert!(presets::GFM.contains(Extension::Strikeout));
        assert!(presets::GFM.contains(Extension::TaskLists));
        assert!(presets::GFM.contains(Extension::PipeTables));
        // GFM has no subscript/superscript; those belong to the broader Markdown dialects.
        assert!(!presets::GFM.contains(Extension::Subscript));
        assert!(!presets::GFM.contains(Extension::Superscript));
    }

    #[test]
    fn markdown_and_commonmark_x_presets_are_broad() {
        assert!(presets::MARKDOWN.contains(Extension::DefinitionLists));
        assert!(presets::MARKDOWN.contains(Extension::YamlMetadataBlock));
        assert!(presets::MARKDOWN.contains(Extension::Smart));
        assert!(presets::COMMONMARK_X.contains(Extension::FencedDivs));
        assert!(presets::COMMONMARK_X.contains(Extension::Attributes));
        // The strict CommonMark dialect keeps none of these.
        assert!(presets::COMMONMARK.is_empty());
    }

    #[test]
    fn code_and_math_surface_variants_round_trip_and_seed_presets() {
        for token in ["fenced_code_blocks", "backtick_code_blocks", "tex_math_gfm"] {
            let ext = Extension::from_name(token).expect("a declared variant");
            assert_eq!(ext.name(), token);
        }
        // The Markdown dialect fences code with both backtick and tilde forms.
        assert!(presets::MARKDOWN.contains(Extension::FencedCodeBlocks));
        assert!(presets::MARKDOWN.contains(Extension::BacktickCodeBlocks));
        // GFM fences with backticks and renders math in its own surface; it has no tilde-fence form.
        assert!(presets::GFM.contains(Extension::BacktickCodeBlocks));
        assert!(presets::GFM.contains(Extension::TexMathGfm));
        assert!(!presets::GFM.contains(Extension::FencedCodeBlocks));
    }

    #[test]
    fn names_are_stable() {
        assert_eq!(Extension::Footnotes.name(), "footnotes");
        assert_eq!(Extension::Autolink.name(), "autolink_bare_uris");
        assert_eq!(Extension::HardLineBreaks.name(), "hard_line_breaks");
        assert_eq!(Extension::RawHtml.name(), "raw_html");
    }

    #[test]
    fn from_name_round_trips_every_variant() {
        for ext in Extension::ALL {
            assert_eq!(Extension::from_name(ext.name()), Some(*ext));
        }
        assert_eq!(Extension::from_name("not_an_extension"), None);
        assert_eq!(Extension::from_name(""), None);
    }

    #[test]
    fn union_combines_both_sides() {
        let a = Extensions::from_list(&[Extension::Strikeout]);
        let b = Extensions::from_list(&[Extension::Subscript]);
        let combined = a.union(b);
        assert!(combined.contains(Extension::Strikeout));
        assert!(combined.contains(Extension::Subscript));
        assert!(!combined.contains(Extension::Superscript));
        assert_eq!(a.union(Extensions::empty()), a);
    }
}