ayaml 0.1.3

A safe, pure-Rust YAML parser, event stream, value model, Serde layer, and emitter
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
//! Structured errors with a stable kind and, when the source provides one, a span.

use std::error::Error as StdError;
use std::fmt;

use crate::span::{Location, Span};

/// The longest path a message holds. A longer one keeps its deepest steps, after [`ELIDED`].
const MAX_PATH_BYTES: usize = 1024;
/// The most characters of one key a path shows.
const MAX_KEY_CHARS: usize = 64;
/// What starts a path that lost its outer steps.
const ELIDED: &str = "…";

/// Stable error categories.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
    /// Input is not valid UTF-8 or uses an unsupported encoding.
    InvalidEncoding,
    /// The scanner could not produce a token.
    Scanner,
    /// The parser could not build an event.
    Parser,
    /// An alias is unknown, cyclic, or over budget.
    Alias,
    /// A mapping contains a duplicate semantic key.
    DuplicateKey,
    /// A tag is malformed or unsupported.
    Tag,
    /// A scalar could not be resolved under the active profile.
    Resolution,
    /// A configured resource limit was exceeded.
    Limit,
    /// Composition or value-model construction failed.
    Composition,
    /// Serde deserialization or serialization failed.
    Serde,
    /// The emitter failed.
    Emitter,
    /// I/O failed.
    Io,
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::InvalidEncoding => "invalid encoding",
            Self::Scanner => "scanner error",
            Self::Parser => "parser error",
            Self::Alias => "alias error",
            Self::DuplicateKey => "duplicate key",
            Self::Tag => "tag error",
            Self::Resolution => "scalar resolution error",
            Self::Limit => "resource limit",
            Self::Composition => "composition error",
            Self::Serde => "serde error",
            Self::Emitter => "emitter error",
            Self::Io => "I/O error",
        };
        formatter.write_str(name)
    }
}

/// A private failure code, for mappings that must not depend on an error's message text.
///
/// The codes are not public yet. A later release can expose them without reclassifying the
/// failures they name.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum Code {
    /// No specific code; the message and kind are all the error carries.
    #[default]
    Unspecified,
    /// A reserved indicator follows a byte order mark at the start of a line.
    ReservedIndicatorAfterBom,
    /// A token appears where a mapping value belongs.
    UnexpectedMappingValue,
    /// A single-document reader found another document.
    TrailingDocuments,
}

/// An ayaml error with a stable category, optional source span, and, for
/// resource-limit failures, the structured limit that was exceeded.
#[derive(Debug)]
pub struct Error {
    kind: ErrorKind,
    code: Code,
    /// How much of `message` is the path to what a typed reader rejected, with the `: ` after it.
    /// A path is at most `MAX_PATH_BYTES` and one step long. Two `u16`s keep `Error` at 112 bytes.
    path_len: u16,
    /// How much of the end of `message` is context that the error's location goes before.
    context_len: u16,
    span: Option<Span>,
    message: String,
    limit: Option<Box<crate::limits::LimitExceeded>>,
    source: Option<Box<dyn StdError + Send + Sync + 'static>>,
}

impl Error {
    /// Create an error with no source location.
    #[must_use]
    pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            code: Code::Unspecified,
            path_len: 0,
            context_len: 0,
            span: None,
            message: message.into(),
            limit: None,
            source: None,
        }
    }

    /// Give this error its private failure code.
    #[must_use]
    pub(crate) fn with_code(mut self, code: Code) -> Self {
        self.code = code;
        self
    }

    /// The private failure code, for mappings that must not read the message.
    #[cfg(feature = "libyaml-compat")]
    #[must_use]
    pub(crate) fn code(&self) -> Code {
        self.code
    }

    /// Attach a source span.
    #[must_use]
    pub fn with_span(mut self, span: Span) -> Self {
        self.span = Some(span);
        self
    }

    /// End the message with `context`, which follows the location when the error is displayed.
    #[cold]
    pub(crate) fn with_context(mut self, context: &str) -> Self {
        self.message.push_str(context);
        // The parser's contexts are short. A longer one would stay before the location.
        self.context_len = u16::try_from(context.len()).unwrap_or(0);
        self
    }

    /// Attach a source error.
    #[must_use]
    pub fn with_source(mut self, source: impl StdError + Send + Sync + 'static) -> Self {
        self.source = Some(Box::new(source));
        self
    }

    /// Attach the structured limit that was exceeded.
    #[must_use]
    pub fn with_limit(mut self, limit: crate::limits::LimitExceeded) -> Self {
        self.limit = Some(Box::new(limit));
        self
    }

    /// The structured resource-limit failure, when this is a limit error.
    #[must_use]
    pub fn limit(&self) -> Option<crate::limits::LimitExceeded> {
        self.limit.as_deref().copied()
    }

    /// The stable category.
    #[must_use]
    pub const fn kind(&self) -> ErrorKind {
        self.kind
    }

    /// The source span, if any.
    #[must_use]
    pub const fn span(&self) -> Option<Span> {
        self.span
    }

    /// The one-based line/column, if the error has a span.
    #[must_use]
    pub fn location(&self) -> Option<Location> {
        self.span.map(|span| span.start().location())
    }

    /// The human-readable message without location decoration.
    ///
    /// A flow collection's error ends with where the collection opened, as in
    /// `unterminated flow sequence, opened at line 1 column 1`. That is part of the message, and
    /// the error's own location goes before it when the error is displayed.
    ///
    /// When a typed reader rejects something below the document's root, the message starts with
    /// the path to it, as in `tasks[0].limits.cpu: invalid type: string "x", expected u32`. A
    /// field is its name, an element is its index in brackets, and a key that isn't a short name
    /// made of ASCII letters, digits, `_` and `-` is quoted in brackets, as in `ports["80"]`. A
    /// key that is a collection is `[?]`. An enum's content adds the variant's name. A long key,
    /// or a long path, is cut short with `…`.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Say that this error came from inside element `index` of a sequence.
    #[cold]
    pub(crate) fn inside_index(self, index: usize) -> Self {
        self.inside(&format!("[{index}]"))
    }

    /// Say that this error came from inside the value of the mapping key with this text.
    #[cold]
    pub(crate) fn inside_key(self, key: &str) -> Self {
        if is_short_name(key) {
            self.inside(key)
        } else {
            self.inside(&quoted_key(key))
        }
    }

    /// Say that this error came from inside the value of a key that is a collection.
    #[cold]
    pub(crate) fn inside_collection_key(self) -> Self {
        self.inside("[?]")
    }

    /// Put one step in front of the path. Errors travel outwards, so the path grows from its
    /// deepest step, and only the step that reads a value adds to it.
    fn inside(mut self, step: &str) -> Self {
        let path_len = self.path_len as usize;
        if path_len == 0 {
            self.message.insert_str(0, ": ");
            self.message.insert_str(0, step);
            self.path_len = path_bytes(step.len() + 2);
            return self;
        }
        if self.message.starts_with(ELIDED) {
            return self;
        }
        let separator = if self.message.starts_with('[') {
            ""
        } else {
            "."
        };
        let step = if path_len + step.len() + separator.len() > MAX_PATH_BYTES {
            ELIDED
        } else {
            step
        };
        self.message.insert_str(0, separator);
        self.message.insert_str(0, step);
        self.path_len = path_bytes(path_len + step.len() + separator.len());
        self
    }
}

/// A path's length as kept. One past `MAX_PATH_BYTES` has the same effect as any more: the next
/// step is elided.
fn path_bytes(len: usize) -> u16 {
    u16::try_from(len).unwrap_or(u16::MAX)
}

/// Whether a key can stand in a path as it is, with no doubt about where it ends.
fn is_short_name(key: &str) -> bool {
    let mut chars = key.chars();
    key.len() <= MAX_KEY_CHARS
        && chars
            .next()
            .is_some_and(|first| first.is_ascii_alphabetic() || first == '_')
        && chars.all(|next| next.is_ascii_alphanumeric() || next == '_' || next == '-')
}

/// A key in brackets and quotes, with anything that could change how a terminal shows it escaped.
#[allow(
    clippy::format_push_string,
    reason = "one small escaped code point appended to an already-owned string"
)]
fn quoted_key(key: &str) -> String {
    let mut quoted = String::from("[\"");
    let mut chars = key.chars();
    for next in chars.by_ref().take(MAX_KEY_CHARS) {
        match next {
            '"' | '\\' => {
                quoted.push('\\');
                quoted.push(next);
            }
            _ if hides_text(next) => {
                quoted.push_str(&format!("\\u{{{:x}}}", u32::from(next)));
            }
            _ => quoted.push(next),
        }
    }
    if chars.next().is_some() {
        quoted.push_str(ELIDED);
    }
    quoted.push_str("\"]");
    quoted
}

/// Control characters, line separators, the byte order mark, and the marks that reorder text.
fn hides_text(character: char) -> bool {
    character.is_control()
        || matches!(
            character,
            '\u{61c}'
                | '\u{200e}'
                | '\u{200f}'
                | '\u{2028}'
                | '\u{2029}'
                | '\u{202a}'..='\u{202e}'
                | '\u{2066}'..='\u{2069}'
                | '\u{feff}'
        )
}

impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Some(span) = self.span else {
            return formatter.write_str(&self.message);
        };
        let (problem, context) = self
            .message
            .split_at(self.message.len() - self.context_len as usize);
        let location = span.start().location();
        write!(
            formatter,
            "{problem} at line {} column {}{context}",
            location.line(),
            location.column()
        )
    }
}

impl StdError for Error {
    #[allow(
        trivial_casts,
        reason = "Box<dyn Error + Send + Sync> is widened to the plain error trait object"
    )]
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        self.source
            .as_deref()
            .map(|source| source as &(dyn StdError + 'static))
    }
}

impl serde::de::Error for Error {
    fn custom<T: fmt::Display>(message: T) -> Self {
        Self::new(ErrorKind::Serde, message.to_string())
    }
}

impl serde::ser::Error for Error {
    fn custom<T: fmt::Display>(message: T) -> Self {
        Self::new(ErrorKind::Serde, message.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::{Error, ErrorKind};
    use crate::span::{Marker, Span};
    use std::error::Error as _;

    #[test]
    fn kind_and_message_are_exposed() {
        let error = Error::new(ErrorKind::Parser, "unexpected token");
        assert_eq!(error.kind(), ErrorKind::Parser);
        assert_eq!(error.message(), "unexpected token");
        assert_eq!(error.to_string(), "unexpected token");
        assert!(error.span().is_none());
        assert!(error.location().is_none());
    }

    #[test]
    fn span_and_location_are_one_based() {
        let span = Span::new(Marker::new(4, 2, 3), Marker::new(5, 2, 4));
        let error = Error::new(ErrorKind::Scanner, "bad token").with_span(span);
        assert_eq!(error.span(), Some(span));
        let location = error.location().expect("location");
        assert_eq!((location.line(), location.column()), (2, 3));
    }

    #[test]
    fn source_is_chained() {
        let inner = std::io::Error::other("inner");
        let error = Error::new(ErrorKind::Io, "outer").with_source(inner);
        assert!(error.source().is_some());
    }

    #[test]
    fn a_path_joins_its_steps_and_stands_before_the_message() {
        let error = Error::new(ErrorKind::Serde, "bad")
            .inside_key("c")
            .inside_index(2)
            .inside_key("b")
            .inside_key("a b")
            .inside_index(0);
        assert_eq!(error.message(), "[0][\"a b\"].b[2].c: bad");
        assert_eq!(error.kind(), ErrorKind::Serde);
        let error = Error::new(ErrorKind::Serde, "bad").inside_collection_key();
        assert_eq!(error.message(), "[?]: bad");
    }

    #[test]
    fn a_key_is_bare_only_if_it_is_a_short_name() {
        for bare in ["a", "_a", "runs-on", "a_1", &"k".repeat(64)] {
            let error = Error::new(ErrorKind::Serde, "bad").inside_key(bare);
            assert_eq!(error.message(), format!("{bare}: bad"));
        }
        for (key, shown) in [
            ("", "[\"\"]"),
            ("80", "[\"80\"]"),
            ("-a", "[\"-a\"]"),
            ("a.b", "[\"a.b\"]"),
            ("名前", "[\"名前\"]"),
            ("a\"b\\c", "[\"a\\\"b\\\\c\"]"),
            ("a\nb\u{1b}", "[\"a\\u{a}b\\u{1b}\"]"),
            ("\u{85}\u{2028}\u{2029}", "[\"\\u{85}\\u{2028}\\u{2029}\"]"),
            ("\u{200e}\u{200f}", "[\"\\u{200e}\\u{200f}\"]"),
            ("a\u{61c}b", "[\"a\\u{61c}b\"]"),
            (
                "\u{202e}\u{2066}\u{feff}",
                "[\"\\u{202e}\\u{2066}\\u{feff}\"]",
            ),
        ] {
            let error = Error::new(ErrorKind::Serde, "bad").inside_key(key);
            assert_eq!(error.message(), format!("{shown}: bad"));
        }
    }

    #[test]
    fn a_long_key_and_a_long_path_are_cut_short() {
        let error = Error::new(ErrorKind::Serde, "bad").inside_key(&"é".repeat(65));
        assert_eq!(error.message(), format!("[\"{}…\"]: bad", "é".repeat(64)));

        let mut error = Error::new(ErrorKind::Serde, "bad");
        let name = "k".repeat(64);
        for _ in 0..100 {
            error = error.inside_key(&name);
        }
        let (path, rest) = error.message().split_once(": ").expect("a path");
        assert_eq!(rest, "bad");
        assert!(path.starts_with("….k"), "{path}");
        assert!(path.len() <= 1024 + 4, "{}", path.len());
        let before = error.message().to_owned();
        let error = error.inside_key("outer").inside_index(1);
        assert_eq!(error.message(), before);

        let mut error = Error::new(ErrorKind::Serde, "bad").inside_index(7);
        for _ in 0..200 {
            error = error.inside_index(1_234_567);
        }
        assert!(error.message().starts_with("…["), "{}", error.message());
    }

    /// Text that reads like a location, in a key or in the message, never stands for the error's.
    #[test]
    fn a_location_in_the_text_does_not_stand_for_the_error_s() {
        let span = Span::new(Marker::new(4, 2, 3), Marker::new(5, 2, 4));
        let error = Error::new(ErrorKind::Serde, "bad")
            .with_span(span)
            .inside_key("at line 9");
        assert_eq!(error.to_string(), "[\"at line 9\"]: bad at line 2 column 3");
        let error = Error::new(ErrorKind::Serde, "invalid type: string \"at line 9\"")
            .with_span(span)
            .inside_key("a");
        assert_eq!(
            error.to_string(),
            "a: invalid type: string \"at line 9\" at line 2 column 3"
        );
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn an_error_stays_small() {
        assert_eq!(size_of::<Error>(), 112);
        assert_eq!(size_of::<Result<crate::Value, Error>>(), 112);
    }

    /// A path's steps are as long as `inside` lets them be, and a context too long to keep its
    /// length still shows.
    #[test]
    fn a_long_path_or_context_keeps_its_text() {
        let long = "k".repeat(70_000);
        let error = Error::new(ErrorKind::Serde, "bad").inside_key(&long);
        assert!(
            error.message().ends_with("…\"]: bad"),
            "{}",
            error.message()
        );
        let error = (0..2_000).fold(Error::new(ErrorKind::Serde, "bad"), |error, index| {
            error.inside_index(index)
        });
        let start: String = error.message().chars().take(40).collect();
        assert!(error.message().starts_with('…'), "{start}");
        assert!(error.message().len() < 2_048);
        let span = Span::new(Marker::new(4, 2, 3), Marker::new(5, 2, 4));
        let context = format!(", {}", "c".repeat(70_000));
        let error = Error::new(ErrorKind::Parser, "bad")
            .with_context(&context)
            .with_span(span);
        assert_eq!(
            error.to_string(),
            format!("bad{context} at line 2 column 3")
        );
    }

    #[test]
    fn the_location_goes_before_the_context() {
        let span = Span::new(Marker::new(4, 2, 3), Marker::new(5, 2, 4));
        let error = Error::new(ErrorKind::Parser, "unterminated flow sequence")
            .with_context(", opened at line 1 column 1")
            .with_span(span);
        assert_eq!(
            error.message(),
            "unterminated flow sequence, opened at line 1 column 1"
        );
        assert_eq!(
            error.to_string(),
            "unterminated flow sequence at line 2 column 3, opened at line 1 column 1"
        );
        let error = error.inside_index(0).inside_key("k");
        assert_eq!(
            error.to_string(),
            "k[0]: unterminated flow sequence at line 2 column 3, opened at line 1 column 1"
        );
        // With no location, the context follows the problem.
        let error = Error::new(ErrorKind::Parser, "unterminated flow sequence")
            .with_context(", opened at line 1 column 1");
        assert_eq!(
            error.to_string(),
            "unterminated flow sequence, opened at line 1 column 1"
        );
    }

    #[test]
    fn categories_display_stably() {
        assert_eq!(ErrorKind::DuplicateKey.to_string(), "duplicate key");
        assert_eq!(ErrorKind::Limit.to_string(), "resource limit");
    }
}