rsconstruct 0.9.118

Rust based fast build system
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
//! Document structure rules: yamllint's `anchors`, `document-start`,
//! `document-end`, `empty-values`, `key-duplicates` and `key-ordering`.

use std::collections::BTreeMap;

use super::super::Problem;
use super::super::config::RuleConf;
use super::super::parser::{Kind, Token};
use super::{Alt, Check, Ctx, Default, OptKind, OptSpec, RuleSpec, States};

// ─── anchors ─────────────────────────────────────────────────────────────────

struct AnchorInfo {
    line: usize,
    column: usize,
    used: bool,
}

#[derive(Default)]
pub struct AnchorsState {
    anchors: BTreeMap<String, AnchorInfo>,
}

fn check_anchors(conf: &RuleConf, ctx: &Ctx<'_, '_>, states: &mut States, out: &mut Vec<Problem>) {
    let forbid_undeclared = conf.bool_opt("forbid-undeclared-aliases");
    let forbid_duplicated = conf.bool_opt("forbid-duplicated-anchors");
    let forbid_unused = conf.bool_opt("forbid-unused-anchors");
    let any = forbid_undeclared || forbid_duplicated || forbid_unused;
    let token = ctx.token();
    let state = &mut states.anchors;

    if any
        && matches!(
            token.kind,
            Kind::StreamStart | Kind::DocumentStart | Kind::DocumentEnd
        )
    {
        state.anchors.clear();
    }

    if forbid_undeclared
        && let Kind::Alias(name) = &token.kind
        && !state.anchors.contains_key(name)
    {
        out.push(Problem::new(
            token.start.line + 1,
            token.start.column + 1,
            format!("found undeclared alias \"{name}\""),
        ));
    }

    if forbid_duplicated
        && let Kind::Anchor(name) = &token.kind
        && state.anchors.contains_key(name)
    {
        out.push(Problem::new(
            token.start.line + 1,
            token.start.column + 1,
            format!("found duplicated anchor \"{name}\""),
        ));
    }

    if forbid_unused {
        // Unused anchors can only be detected at the end of a document.
        let next_ends_document = ctx.next().is_some_and(|n| {
            matches!(
                n.kind,
                Kind::StreamEnd | Kind::DocumentStart | Kind::DocumentEnd
            )
        });
        if next_ends_document {
            for (anchor, info) in &state.anchors {
                if !info.used {
                    out.push(Problem::new(
                        info.line + 1,
                        info.column + 1,
                        format!("found unused anchor \"{anchor}\""),
                    ));
                }
            }
        } else if let Kind::Alias(name) = &token.kind
            && let Some(info) = state.anchors.get_mut(name)
        {
            info.used = true;
        }
    }

    if any && let Kind::Anchor(name) = &token.kind {
        state.anchors.insert(
            name.clone(),
            AnchorInfo {
                line: token.start.line,
                column: token.start.column,
                used: false,
            },
        );
    }
}

pub const ANCHORS: RuleSpec = RuleSpec {
    id: "anchors",
    options: &[
        OptSpec {
            name: "forbid-undeclared-aliases",
            kind: OptKind::Bool,
            default: Default::Bool(true),
        },
        OptSpec {
            name: "forbid-duplicated-anchors",
            kind: OptKind::Bool,
            default: Default::Bool(false),
        },
        OptSpec {
            name: "forbid-unused-anchors",
            kind: OptKind::Bool,
            default: Default::Bool(false),
        },
    ],
    validate: None,
    check: Check::Token(check_anchors),
};

// ─── document-end ────────────────────────────────────────────────────────────

fn check_document_end(conf: &RuleConf, ctx: &Ctx<'_, '_>, _: &mut States, out: &mut Vec<Problem>) {
    let token = ctx.token();
    let prev = ctx.prev();
    if conf.bool_opt("present") {
        let is_stream_end = token.kind == Kind::StreamEnd;
        let is_start = token.kind == Kind::DocumentStart;
        let prev_is_end_or_stream_start =
            prev.is_some_and(|p| matches!(p.kind, Kind::DocumentEnd | Kind::StreamStart));
        let prev_is_directive = prev.is_some_and(Token::is_directive);

        if is_stream_end && !prev_is_end_or_stream_start {
            out.push(Problem::new(
                token.start.line,
                1,
                "missing document end \"...\"",
            ));
        } else if is_start && !(prev_is_end_or_stream_start || prev_is_directive) {
            out.push(Problem::new(
                token.start.line + 1,
                1,
                "missing document end \"...\"",
            ));
        }
    } else if token.kind == Kind::DocumentEnd {
        out.push(Problem::new(
            token.start.line + 1,
            token.start.column + 1,
            "found forbidden document end \"...\"",
        ));
    }
}

pub const DOCUMENT_END: RuleSpec = RuleSpec {
    id: "document-end",
    options: &[OptSpec {
        name: "present",
        kind: OptKind::Bool,
        default: Default::Bool(true),
    }],
    validate: None,
    check: Check::Token(check_document_end),
};

// ─── document-start ──────────────────────────────────────────────────────────

fn check_document_start(
    conf: &RuleConf,
    ctx: &Ctx<'_, '_>,
    _: &mut States,
    out: &mut Vec<Problem>,
) {
    let token = ctx.token();
    let prev = ctx.prev();
    if conf.bool_opt("present") {
        let prev_opens = prev.is_some_and(|p| {
            matches!(p.kind, Kind::StreamStart | Kind::DocumentEnd) || p.is_directive()
        });
        let token_is_start =
            matches!(token.kind, Kind::DocumentStart | Kind::StreamEnd) || token.is_directive();
        if prev_opens && !token_is_start {
            out.push(Problem::new(
                token.start.line + 1,
                1,
                "missing document start \"---\"",
            ));
        }
    } else if token.kind == Kind::DocumentStart {
        out.push(Problem::new(
            token.start.line + 1,
            token.start.column + 1,
            "found forbidden document start \"---\"",
        ));
    }
}

pub const DOCUMENT_START: RuleSpec = RuleSpec {
    id: "document-start",
    options: &[OptSpec {
        name: "present",
        kind: OptKind::Bool,
        default: Default::Bool(true),
    }],
    validate: None,
    check: Check::Token(check_document_start),
};

// ─── empty-values ────────────────────────────────────────────────────────────

fn check_empty_values(conf: &RuleConf, ctx: &Ctx<'_, '_>, _: &mut States, out: &mut Vec<Problem>) {
    let token = ctx.token();
    let Some(next) = ctx.next() else {
        return;
    };
    if conf.bool_opt("forbid-in-block-mappings")
        && token.kind == Kind::Value
        && matches!(next.kind, Kind::Key | Kind::BlockEnd)
    {
        out.push(Problem::new(
            token.start.line + 1,
            token.end.column + 1,
            "empty value in block mapping",
        ));
    }
    if conf.bool_opt("forbid-in-flow-mappings")
        && token.kind == Kind::Value
        && matches!(next.kind, Kind::FlowEntry | Kind::FlowMappingEnd)
    {
        out.push(Problem::new(
            token.start.line + 1,
            token.end.column + 1,
            "empty value in flow mapping",
        ));
    }
    if conf.bool_opt("forbid-in-block-sequences")
        && token.kind == Kind::BlockEntry
        && matches!(next.kind, Kind::Key | Kind::BlockEnd | Kind::BlockEntry)
    {
        out.push(Problem::new(
            token.start.line + 1,
            token.end.column + 1,
            "empty value in block sequence",
        ));
    }
}

pub const EMPTY_VALUES: RuleSpec = RuleSpec {
    id: "empty-values",
    options: &[
        OptSpec {
            name: "forbid-in-block-mappings",
            kind: OptKind::Bool,
            default: Default::Bool(true),
        },
        OptSpec {
            name: "forbid-in-flow-mappings",
            kind: OptKind::Bool,
            default: Default::Bool(true),
        },
        OptSpec {
            name: "forbid-in-block-sequences",
            kind: OptKind::Bool,
            default: Default::Bool(true),
        },
    ],
    validate: None,
    check: Check::Token(check_empty_values),
};

// ─── key-duplicates / key-ordering ───────────────────────────────────────────

/// A mapping or sequence being walked, with the keys seen so far.
struct KeysParent {
    is_map: bool,
    keys: Vec<String>,
}

/// The collection stack both key rules keep.
#[derive(Default)]
pub struct KeysState {
    stack: Vec<KeysParent>,
}

impl KeysState {
    /// The stack bookkeeping shared by `key-duplicates` and `key-ordering`.
    /// Returns the key scalar's value when the token is a key of the
    /// innermost mapping, which is what either rule then judges.
    fn track<'s>(&mut self, ctx: &Ctx<'s, '_>) -> Option<&'s str> {
        let token = ctx.token();
        match token.kind {
            Kind::BlockMappingStart | Kind::FlowMappingStart => self.stack.push(KeysParent {
                is_map: true,
                keys: Vec::new(),
            }),
            Kind::BlockSequenceStart | Kind::FlowSequenceStart => self.stack.push(KeysParent {
                is_map: false,
                keys: Vec::new(),
            }),
            Kind::BlockEnd | Kind::FlowMappingEnd | Kind::FlowSequenceEnd => {
                self.stack.pop();
            }
            Kind::Key => {
                // KeyTokens can be found inside flow sequences: strange, but
                // allowed, hence the check that the parent is a mapping.
                let value = ctx.next().and_then(|n| n.scalar_value())?;
                if self.stack.last().is_some_and(|p| p.is_map) {
                    return Some(value);
                }
            }
            _ => {}
        }
        None
    }
}

fn check_key_duplicates(
    conf: &RuleConf,
    ctx: &Ctx<'_, '_>,
    states: &mut States,
    out: &mut Vec<Problem>,
) {
    let state = &mut states.key_duplicates;
    let Some(value) = state.track(ctx) else {
        return;
    };
    let Some(parent) = state.stack.last_mut() else {
        return;
    };
    // `<<` is the merge key (http://yaml.org/type/merge.html): repeating it
    // is fine unless the config forbids that too.
    if parent.keys.iter().any(|k| k == value)
        && (value != "<<" || conf.bool_opt("forbid-duplicated-merge-keys"))
    {
        let next = ctx.next().expect("a key scalar follows the Key token");
        out.push(Problem::new(
            next.start.line + 1,
            next.start.column + 1,
            format!("duplication of key \"{value}\" in mapping"),
        ));
    } else {
        parent.keys.push(value.to_owned());
    }
}

pub const KEY_DUPLICATES: RuleSpec = RuleSpec {
    id: "key-duplicates",
    options: &[OptSpec {
        name: "forbid-duplicated-merge-keys",
        kind: OptKind::Bool,
        default: Default::Bool(false),
    }],
    validate: None,
    check: Check::Token(check_key_duplicates),
};

fn check_key_ordering(
    conf: &RuleConf,
    ctx: &Ctx<'_, '_>,
    states: &mut States,
    out: &mut Vec<Problem>,
) {
    let state = &mut states.key_ordering;
    let Some(value) = state.track(ctx) else {
        return;
    };
    let ignored = conf
        .str_list_opt("ignored-keys")
        .iter()
        .any(|pattern| regex::Regex::new(pattern).is_ok_and(|re| re.is_match(value)));
    if ignored {
        return;
    }
    let Some(parent) = state.stack.last_mut() else {
        return;
    };
    // yamllint compares with `strcoll`; with no `locale` configured (the
    // only mode this port accepts) that is code-point order.
    if parent.keys.iter().any(|key| value < key.as_str()) {
        let next = ctx.next().expect("a key scalar follows the Key token");
        out.push(Problem::new(
            next.start.line + 1,
            next.start.column + 1,
            format!("wrong ordering of key \"{value}\" in mapping"),
        ));
    } else {
        parent.keys.push(value.to_owned());
    }
}

/// Every pattern must compile: the check skips a pattern that does not, so
/// rejecting it here keeps a typo from silently ignoring nothing.
pub(super) fn validate_regex_list(conf: &RuleConf, name: &str) -> Option<String> {
    conf.str_list_opt(name).iter().find_map(|pattern| {
        regex::Regex::new(pattern)
            .err()
            .map(|e| format!("invalid regex {pattern:?} in {name}: {e}"))
    })
}

fn validate_key_ordering(conf: &RuleConf) -> Option<String> {
    validate_regex_list(conf, "ignored-keys")
}

const STR_LIST: &[Alt] = &[Alt::AnyStr];

pub const KEY_ORDERING: RuleSpec = RuleSpec {
    id: "key-ordering",
    options: &[OptSpec {
        name: "ignored-keys",
        kind: OptKind::ListOf(STR_LIST),
        default: Default::EmptyList,
    }],
    validate: Some(validate_key_ordering),
    check: Check::Token(check_key_ordering),
};