Skip to main content

sokf_core/api/guide/
report.rs

1//! This module holds the wire types of `sokf guide --json` (CLI-17_AC-17),
2//! which `sokf new --json` also gives for the document that it creates.
3
4use std::collections::BTreeMap;
5
6use schemars::JsonSchema;
7use serde::Serialize;
8
9use crate::RepoPath;
10
11// sokf:begin guide-report
12/// `GuideReport` is the report of `sokf guide --json`: the guide of each
13/// document that an argument names, in the order of the arguments
14/// (CLI-17_AC-17).
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
16#[schemars(title = "sokf guide report")]
17pub struct GuideReport {
18    /// `documents` holds one guide for each argument, in the order of the
19    /// arguments.
20    pub documents: Vec<DocumentGuide>,
21}
22
23/// `DocumentGuide` holds the guidance that a document's schema gives for
24/// the parts that the document's lifecycle states admit, in the schema's
25/// order (CLI-17_AC-6), with each part's example (CLI-17_AC-38). Each `text`
26/// holds the words of the guidance line that the scaffold of the schema
27/// writes before the same part at the same states, and each `example` the
28/// text that the scaffold writes for the part, with the document's own
29/// frontmatter values.
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
31pub struct DocumentGuide {
32    /// `path` is the path of the document.
33    pub path: RepoPath,
34    /// `id` is the `id` of the document. It is absent when the frontmatter
35    /// has none.
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    #[schemars(with = "String")]
38    pub id: Option<String>,
39    /// `type` is the `type` of the document.
40    #[serde(rename = "type")]
41    pub type_name: String,
42    /// `schema` is the path of the schema that governs the document. It is
43    /// absent for a schema, which the grammar of the schema language
44    /// governs. Each `line` of the guide is a line of this file.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    #[schemars(with = "RepoPath")]
47    pub schema: Option<RepoPath>,
48    /// `states` maps each lifecycle key, in the order of the lifecycle, to
49    /// the state at which the guide shows the document: the document's own
50    /// state, or the state that `--state` gives.
51    #[schemars(with = "BTreeMap<String, String>")]
52    pub states: serde_json::Map<String, serde_json::Value>,
53    /// `guidance` is the schema's top-level `guidance`.
54    #[serde(default, skip_serializing_if = "Option::is_none")]
55    #[schemars(with = "String")]
56    pub guidance: Option<String>,
57    /// `bans` holds the message of each ban of the whole body, in order.
58    pub bans: Vec<String>,
59    /// `text` holds the words of the guidance line that opens the body: the
60    /// schema's guidance, then the message of each ban of the whole body. It
61    /// is absent when the schema has neither.
62    #[serde(default, skip_serializing_if = "Option::is_none")]
63    #[schemars(with = "String")]
64    pub text: Option<String>,
65    /// `labels` holds each attribution label that the schema declares, in
66    /// the schema's order, once for the whole document (CLI-17_AC-35). It is
67    /// empty for a schema without labels.
68    pub labels: Vec<LabelGuide>,
69    /// `frontmatter` holds each frontmatter key that the schema declares and
70    /// the states admit, in the schema's order, then each lifecycle key.
71    pub frontmatter: Vec<KeyGuide>,
72    /// `links` holds each `links` rule that the states admit, in the schema's
73    /// order, except a rule whose hard limit of 0 bans its links.
74    pub links: Vec<LinkGuide>,
75    /// `preamble` is the part of the body before the first heading, where
76    /// the schema declares one that the states admit.
77    #[serde(default, skip_serializing_if = "Option::is_none")]
78    #[schemars(with = "PreambleGuide")]
79    pub preamble: Option<PreambleGuide>,
80    /// `sections` holds each section that the states admit, in the schema's
81    /// order, with each section that declares `parent` after its parent.
82    pub sections: Vec<SectionGuide>,
83    /// `frontmatter_text` is the YAML of the document's frontmatter, from which
84    /// the text output leaves out each example of a key that it holds
85    /// already. The JSON report leaves this field out.
86    #[serde(skip)]
87    #[schemars(skip)]
88    pub frontmatter_text: String,
89    /// `body_text` is the document's body, from which the text output leaves out
90    /// each example of a block that it holds already. The JSON report leaves
91    /// this field out.
92    #[serde(skip)]
93    #[schemars(skip)]
94    pub body_text: String,
95}
96
97/// `LabelGuide` holds one attribution label of the schema.
98#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
99pub struct LabelGuide {
100    /// `key` is the label's key, `human` or `agent`.
101    pub key: String,
102    /// `words` are the words that a document writes between the label's
103    /// square brackets: sokf's words for the label in the language of the
104    /// schema, or else the key.
105    pub words: String,
106    /// `meaning` is sokf's sentence that says what the label means, in the
107    /// language of the schema. It is absent where sokf's texts hold none in
108    /// that language.
109    #[serde(default, skip_serializing_if = "Option::is_none")]
110    #[schemars(with = "String")]
111    pub meaning: Option<String>,
112}
113
114/// `KeyGuide` holds the guidance of one frontmatter key.
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
116pub struct KeyGuide {
117    /// `key` is the key.
118    pub key: String,
119    /// `lifecycle` is true when the key is a lifecycle key rather than a
120    /// declared one.
121    pub lifecycle: bool,
122    /// `optional` is true when the key can be absent.
123    pub optional: bool,
124    /// `guidance` is the key's own `guidance`.
125    #[serde(default, skip_serializing_if = "Option::is_none")]
126    #[schemars(with = "String")]
127    pub guidance: Option<String>,
128    /// `limit` is the limit on the elements of the key's list. It is absent
129    /// when the key has no `max`.
130    #[serde(default, skip_serializing_if = "Option::is_none")]
131    #[schemars(with = "GuideLimit")]
132    pub limit: Option<GuideLimit>,
133    /// `text` holds the words of the guidance line before the key: sokf's own
134    /// text for the key, which is the rule for writing a `description`, then
135    /// the key's guidance, then the words that state its limit and the limit's
136    /// `when-at-limit`. It is absent when the key has none of them.
137    #[serde(default, skip_serializing_if = "Option::is_none")]
138    #[schemars(with = "String")]
139    pub text: Option<String>,
140    /// `example` is the line that the scaffold of the schema writes for the
141    /// key, with the document's own values, such as `priority: medium`. It is
142    /// absent where the scaffold writes none.
143    #[serde(default, skip_serializing_if = "Option::is_none")]
144    #[schemars(with = "String")]
145    pub example: Option<String>,
146    /// `line` is the line of the key's rule in the schema.
147    pub line: usize,
148}
149
150/// `LinkGuide` holds the guidance of one `links` rule.
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
152pub struct LinkGuide {
153    /// `rel` is the relationship of the links that the rule governs.
154    pub rel: String,
155    /// `optional` is true when the rule's `min` is 0.
156    pub optional: bool,
157    /// `limit` is the limit on the links of the rule. It is absent when the
158    /// rule has no `max`.
159    #[serde(default, skip_serializing_if = "Option::is_none")]
160    #[schemars(with = "GuideLimit")]
161    pub limit: Option<GuideLimit>,
162    /// `text` holds the words of the guidance line before the link: the
163    /// rule's guidance, then the words that state its limit and the limit's
164    /// `when-at-limit`. It is absent when the rule has neither.
165    #[serde(default, skip_serializing_if = "Option::is_none")]
166    #[schemars(with = "String")]
167    pub text: Option<String>,
168    /// `example` is the `links` key with one entry of the rule, which names
169    /// its target by the placeholder `target-id`.
170    pub example: String,
171}
172
173/// `PreambleGuide` holds the guidance of the preamble.
174#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
175pub struct PreambleGuide {
176    /// `guidance` is the preamble's own `guidance`.
177    #[serde(default, skip_serializing_if = "Option::is_none")]
178    #[schemars(with = "String")]
179    pub guidance: Option<String>,
180    /// `text` holds the words of the guidance line before the preamble.
181    #[serde(default, skip_serializing_if = "Option::is_none")]
182    #[schemars(with = "String")]
183    pub text: Option<String>,
184    /// `example` is the text that the scaffold of the schema writes for the
185    /// preamble. It is absent where the scaffold writes none.
186    #[serde(default, skip_serializing_if = "Option::is_none")]
187    #[schemars(with = "String")]
188    pub example: Option<String>,
189    /// `blocks` holds each block that the states admit, in order.
190    pub blocks: Vec<BlockGuide>,
191    /// `line` is the line of the preamble in the schema.
192    pub line: usize,
193}
194
195/// `SectionGuide` holds the guidance of one section.
196#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
197pub struct SectionGuide {
198    /// `heading` is the heading as a finding of `validate` names it: its
199    /// text, its template or pattern, or `<key>` for a heading that takes
200    /// the value of the frontmatter key `key`.
201    pub heading: String,
202    /// `level` is the level of the heading.
203    pub level: u8,
204    /// `rule_id` is the section's `rule-id`.
205    #[serde(default, skip_serializing_if = "Option::is_none")]
206    #[schemars(with = "String")]
207    pub rule_id: Option<String>,
208    /// `parent` is the `rule-id` of the section that this one lies inside.
209    #[serde(default, skip_serializing_if = "Option::is_none")]
210    #[schemars(with = "String")]
211    pub parent: Option<String>,
212    /// `optional` is true when the section can be absent at the states.
213    pub optional: bool,
214    /// `repeat` is true when the heading can occur more than once.
215    pub repeat: bool,
216    /// `held` is true when tramlines-md's match of a heading to its section
217    /// gives the section a heading at its level whose reading, as
218    /// tramlines-md normalises it, has the section's heading shape, or one
219    /// of its `renamed-from` texts (CLI-17_AC-15).
220    pub held: bool,
221    /// `guidance` is the section's own `guidance`.
222    #[serde(default, skip_serializing_if = "Option::is_none")]
223    #[schemars(with = "String")]
224    pub guidance: Option<String>,
225    /// `text` holds the words of the guidance line before the heading.
226    #[serde(default, skip_serializing_if = "Option::is_none")]
227    #[schemars(with = "String")]
228    pub text: Option<String>,
229    /// `example` is the text that the scaffold of the schema writes for the
230    /// section: each occurrence, from its heading, with the sections inside
231    /// it and the section's `examples`. It is absent where the scaffold
232    /// writes none. A section inside a repeating one gives the text of its
233    /// first occurrence.
234    #[serde(default, skip_serializing_if = "Option::is_none")]
235    #[schemars(with = "String")]
236    pub example: Option<String>,
237    /// `blocks` holds each block of the section's body that the states
238    /// admit, in order.
239    pub blocks: Vec<BlockGuide>,
240    /// `line` is the line of the section's rule in the schema.
241    pub line: usize,
242}
243
244/// `BlockGuide` holds the guidance of one block.
245#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
246pub struct BlockGuide {
247    /// `kind` is the kind of the block, as the grammar names it, such as
248    /// `prose` or `bullet-list`.
249    pub kind: String,
250    /// `rule_id` is the block's `rule-id`.
251    #[serde(default, skip_serializing_if = "Option::is_none")]
252    #[schemars(with = "String")]
253    pub rule_id: Option<String>,
254    /// `pos` is the block's place in the list that declares it, from 1.
255    pub pos: usize,
256    /// `optional` is true when the block can be absent at the states.
257    pub optional: bool,
258    /// `guidance` is the block's own `guidance`.
259    #[serde(default, skip_serializing_if = "Option::is_none")]
260    #[schemars(with = "String")]
261    pub guidance: Option<String>,
262    /// `limits` holds the block's own limit, then the limit of each of its
263    /// unit rules and line rules, in the schema's order.
264    pub limits: Vec<RuleLimit>,
265    /// `labels` holds the block's own `label`, then the `label` of each of its
266    /// unit rules that declares one, in the schema's order (CLI-17_AC-37).
267    pub labels: Vec<RuleLabel>,
268    /// `bans` holds the message of each ban of the block's unit rules, of
269    /// the formats that they bind, and of their line rules, each once, in
270    /// order.
271    pub bans: Vec<String>,
272    /// `text` holds the words of the guidance line before the block: its
273    /// guidance, then the words that state each limit and the limit's
274    /// `when-at-limit`, then the message of each ban. It is absent when the
275    /// block has none of them.
276    #[serde(default, skip_serializing_if = "Option::is_none")]
277    #[schemars(with = "String")]
278    pub text: Option<String>,
279    /// `example` is the text that the scaffold of the schema writes for the
280    /// block, without the indent of an item that holds it. It is absent where
281    /// the scaffold writes none. A block that the scaffold writes more than
282    /// once gives its first occurrence.
283    #[serde(default, skip_serializing_if = "Option::is_none")]
284    #[schemars(with = "String")]
285    pub example: Option<String>,
286    /// `blocks` holds the blocks of a group, and the blocks inside the items
287    /// of a list, that the states admit, in order.
288    pub blocks: Vec<BlockGuide>,
289    /// `line` is the line of the block's rule in the schema.
290    pub line: usize,
291}
292
293/// `RuleLimit` is one limit of a block: its own, or one of a unit rule's or
294/// a line rule's.
295#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
296pub struct RuleLimit {
297    /// `rule` is the place of the unit rule or the line rule whose limit this
298    /// is, among the unit rules of the schema, counted from 0. It is absent for
299    /// the block's own limit.
300    #[serde(default, skip_serializing_if = "Option::is_none")]
301    #[schemars(with = "usize")]
302    pub rule: Option<usize>,
303    /// `format` is the name of the format that the unit rule or the line
304    /// rule binds. It is absent for the block's own limit and for a rule
305    /// that binds no format.
306    #[serde(default, skip_serializing_if = "Option::is_none")]
307    #[schemars(with = "String")]
308    pub format: Option<String>,
309    /// `rule_id` is the `rule-id` of the unit rule or the line rule. It is
310    /// absent for the block's own limit and for a rule without one.
311    #[serde(default, skip_serializing_if = "Option::is_none")]
312    #[schemars(with = "String")]
313    pub rule_id: Option<String>,
314    /// `counts` is what the unit rule or the line rule counts: the units of
315    /// the block, or the content lines of a code block or an HTML block. It
316    /// is absent for the block's own limit.
317    #[serde(default, skip_serializing_if = "Option::is_none")]
318    #[schemars(with = "GuideCounts")]
319    pub counts: Option<GuideCounts>,
320    /// `line` is the line of the rule in the schema.
321    pub line: usize,
322    /// The limit.
323    #[serde(flatten)]
324    pub limit: GuideLimit,
325}
326
327/// `RuleLabel` says whether the units of a block, or the units of one of its
328/// unit rules, carry a label.
329#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
330pub struct RuleLabel {
331    /// `rule` is the place of the unit rule whose `label` this is, among the
332    /// unit rules of the schema, counted from 0. It is absent for the block's
333    /// own `label`.
334    #[serde(default, skip_serializing_if = "Option::is_none")]
335    #[schemars(with = "usize")]
336    pub rule: Option<usize>,
337    /// `rule_id` is the `rule-id` of the unit rule. It is absent for the
338    /// block's own `label` and for a rule without one.
339    #[serde(default, skip_serializing_if = "Option::is_none")]
340    #[schemars(with = "String")]
341    pub rule_id: Option<String>,
342    /// `line` is the line of the rule in the schema.
343    pub line: usize,
344    /// `label` is `required` where each unit carries a label, and `optional`
345    /// where a unit carries one if its writer gives one.
346    pub label: GuideLabel,
347}
348
349/// `GuideLabel` says whether a unit carries a label.
350#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)]
351#[serde(rename_all = "lowercase")]
352pub enum GuideLabel {
353    /// Every unit carries a label.
354    Required,
355    /// A unit carries a label where its writer gives one.
356    Optional,
357}
358
359/// `GuideLimit` is a limit on a part: its kind, the number of units, lines, or
360/// elements that it states, and what the writer does at the limit.
361#[derive(Debug, Clone, PartialEq, Eq, Serialize, JsonSchema)]
362pub struct GuideLimit {
363    /// `kind` is `soft` for the usual length, which nothing checks, or
364    /// `hard` for the most that a part can hold.
365    pub kind: GuideLimitKind,
366    /// `count` is the number that the limit states.
367    pub count: usize,
368    /// `when_at_limit` is the schema's text that says what the writer does at
369    /// the limit.
370    pub when_at_limit: String,
371}
372
373/// `GuideCounts` is what the limit of a unit rule or a line rule counts.
374#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)]
375#[serde(rename_all = "lowercase")]
376pub enum GuideCounts {
377    /// The units of the block: its paragraphs, items, rows, or repetitions.
378    Units,
379    /// The content lines of a code block or an HTML block.
380    Lines,
381}
382
383/// `GuideLimitKind` is the kind of a limit.
384#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)]
385#[serde(rename_all = "lowercase")]
386pub enum GuideLimitKind {
387    /// The usual length, which nothing checks.
388    Soft,
389    /// The most that a part can hold.
390    Hard,
391}
392// sokf:end guide-report