common/parser_tools/djot_options.rs
1//! Per-feature selection for djot import/export.
2//!
3//! The djot importer and exporter round-trip a set of *optional* block-level
4//! attributes — paragraph alignment, line height, text direction, non-breakable
5//! lines and background color — through djot's native `{key=value}` block
6//! attribute syntax (e.g. `{alignment=center}` on the line before a paragraph).
7//! These two option structs let a caller choose which of those attributes are
8//! carried. Everything else (headings, lists, tables, blockquotes, code blocks
9//! and all inline formatting) is always imported/exported and is not gated.
10//!
11//! Both structs default to **all enabled** — the fully lossless round-trip. Use
12//! [`DjotImportOptions::none`] / [`DjotExportOptions::none`] to restrict the
13//! round-trip to the core structural and inline feature set only.
14//!
15//! The attribute keys used on the wire are the model field names:
16//! `alignment`, `line_height`, `direction`, `non_breakable_lines`,
17//! `background_color`. Block attributes are only emitted/read for standalone
18//! paragraphs and headings; list items, code blocks and table cells normalise
19//! their block styling away (the same boundary the other targets observe).
20
21use serde::{Deserialize, Serialize};
22
23/// Selects which optional block attributes the djot **importer** applies to the
24/// document model. An attribute present in the source but disabled here is
25/// parsed and discarded, exactly like an unrepresentable construct.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27pub struct DjotImportOptions {
28 /// Apply paragraph alignment from `{alignment=left|right|center|justify}`.
29 pub alignment: bool,
30 /// Apply line height from `{line_height=<int>}`.
31 pub line_height: bool,
32 /// Apply text direction from `{direction=ltr|rtl}`.
33 pub direction: bool,
34 /// Apply non-breakable lines from `{non_breakable_lines=true|false}`.
35 pub non_breakable_lines: bool,
36 /// Apply block background color from `{background_color="<value>"}`.
37 pub background_color: bool,
38}
39
40impl DjotImportOptions {
41 /// Every optional block attribute enabled — the lossless default.
42 pub const fn all() -> Self {
43 Self {
44 alignment: true,
45 line_height: true,
46 direction: true,
47 non_breakable_lines: true,
48 background_color: true,
49 }
50 }
51
52 /// No optional block attributes — import only the core structural and
53 /// inline feature set, discarding any block-attribute styling.
54 pub const fn none() -> Self {
55 Self {
56 alignment: false,
57 line_height: false,
58 direction: false,
59 non_breakable_lines: false,
60 background_color: false,
61 }
62 }
63}
64
65impl Default for DjotImportOptions {
66 fn default() -> Self {
67 Self::all()
68 }
69}
70
71/// Selects which optional block attributes the djot **exporter** emits as
72/// `{key=value}` block attributes. A disabled attribute is omitted from the
73/// output even when the model carries a value for it.
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
75pub struct DjotExportOptions {
76 /// Emit paragraph alignment as `{alignment=…}`.
77 pub alignment: bool,
78 /// Emit line height as `{line_height=…}`.
79 pub line_height: bool,
80 /// Emit text direction as `{direction=…}`.
81 pub direction: bool,
82 /// Emit non-breakable lines as `{non_breakable_lines=…}`.
83 pub non_breakable_lines: bool,
84 /// Emit block background color as `{background_color=…}`.
85 pub background_color: bool,
86}
87
88impl DjotExportOptions {
89 /// Every optional block attribute emitted — the lossless default.
90 pub const fn all() -> Self {
91 Self {
92 alignment: true,
93 line_height: true,
94 direction: true,
95 non_breakable_lines: true,
96 background_color: true,
97 }
98 }
99
100 /// No optional block attributes — emit only the core structural and inline
101 /// feature set, dropping any block-attribute styling.
102 pub const fn none() -> Self {
103 Self {
104 alignment: false,
105 line_height: false,
106 direction: false,
107 non_breakable_lines: false,
108 background_color: false,
109 }
110 }
111}
112
113impl Default for DjotExportOptions {
114 fn default() -> Self {
115 Self::all()
116 }
117}