ayaml 1.0.0

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
//! YAML tags and tagged values.

use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};

use crate::value::Value;

const CORE_PREFIX: &str = "tag:yaml.org,2002:";

#[derive(Clone, Debug)]
struct TagInner {
    handle: String,
    suffix: String,
}

/// A tag, stored as the prefix its handle resolved to plus a suffix.
///
/// Two tags are equal when they name the same tag: equality, ordering, and
/// hashing use the full tag (`handle` followed by `suffix`), so
/// `!<tag:yaml.org,2002:str>` equals `!!str`, and a tag written with a
/// `%TAG` handle equals the same tag written verbatim.
///
/// The writers write a local tag as `!name` and a core tag as `!!name`. Any
/// other tag is written verbatim, as `!<name>`, when that reads back as the
/// same tag, and otherwise as a shorthand under a `%TAG` directive that the
/// writer adds to the document, such as `%TAG !t0! tag:x:` for `tag:x:a b`.
/// A tag that is neither local nor core and has fewer than two characters has
/// no form, and writing it is an error.
#[derive(Clone)]
pub struct Tag {
    inner: Box<TagInner>,
}

impl fmt::Debug for Tag {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Tag")
            .field("handle", &self.inner.handle)
            .field("suffix", &self.inner.suffix)
            .finish()
    }
}

impl Tag {
    fn from_parts(handle: String, suffix: String) -> Self {
        Self {
            inner: Box::new(TagInner { handle, suffix }),
        }
    }

    /// Create a tag from its handle prefix and suffix.
    ///
    /// A full name that starts with the core prefix `tag:yaml.org,2002:` is
    /// split at that prefix whatever `handle` holds, so
    /// `Tag::new("", "tag:yaml.org,2002:str")` and
    /// `Tag::new("tag:yaml.org,2002:s", "tr")` are both the core tag `!!str`.
    /// A verbatim tag (empty `handle`) that starts with `!` is split there,
    /// so `Tag::new("", "!x")` is the local tag `!x`. Any other empty-handle
    /// full name is split after its last `:`, `/`, or `#` when it has one, or
    /// before its last character otherwise, so a reader can reproduce its
    /// `handle` and `suffix` from the full name. `Tag::new("foo", "bar")`
    /// keeps the explicit split it was given.
    #[must_use]
    pub fn new(handle: impl Into<String>, suffix: impl Into<String>) -> Self {
        let handle = handle.into();
        let suffix = suffix.into();
        if handle != CORE_PREFIX
            && (CORE_PREFIX.starts_with(handle.as_str()) || handle.starts_with(CORE_PREFIX))
        {
            let full = format!("{handle}{suffix}");
            if let Some(rest) = full.strip_prefix(CORE_PREFIX) {
                return Self::from_parts(CORE_PREFIX.to_owned(), rest.to_owned());
            }
        }
        if handle.is_empty()
            && let Some(rest) = suffix.strip_prefix('!')
        {
            return Self::from_parts("!".to_owned(), rest.to_owned());
        }
        // A full name with a URI scheme and no explicit handle is split like a writer would:
        // after its last `:`, `/`, or `#`. That keeps the handle/suffix pair independent of
        // whether a reader got the tag verbatim or through an expanded `%TAG` handle.
        if handle.is_empty()
            && let Some((last, _)) = suffix.char_indices().next_back()
            && last > 0
        {
            let cut = suffix[..last]
                .rfind([':', '/', '#'])
                .map_or(last, |index| index + 1);
            if cut < suffix.len() {
                return Self::from_parts(suffix[..cut].to_owned(), suffix[cut..].to_owned());
            }
        }
        Self::from_parts(handle, suffix)
    }

    /// Whether this is a core collection tag (`!!seq`, `!!map`, `!!set`,
    /// `!!omap`, or `!!pairs`). It only restates the node kind, so the
    /// composers drop it.
    pub(crate) fn is_core_collection(&self) -> bool {
        self.is_core() && matches!(self.suffix(), "seq" | "map" | "set" | "omap" | "pairs")
    }

    /// Whether this is a scalar tag the YAML 1.2 core schema defines. On a collection it names a
    /// node of another kind; an unknown core name is left for the application.
    pub(crate) fn is_core_scalar(&self) -> bool {
        self.is_core() && matches!(self.suffix(), "str" | "int" | "float" | "bool" | "null")
    }

    /// The tag's name in the Serde data model: a local tag's name after its
    /// `!` (`Foo` for `!Foo`, and for `!e!Foo` under `%TAG !e! !`) or, for
    /// any other tag, its full name in angle brackets
    /// (`<tag:yaml.org,2002:set>` for `!!set`). A local name that is empty
    /// or starts with `!` or `<` is bracketed too, so that
    /// [`Tag::from_serde_name`] reads every name back.
    pub(crate) fn serde_name(&self) -> String {
        if let Some(prefix) = self.inner.handle.strip_prefix('!') {
            let name = format!("{prefix}{}", self.inner.suffix);
            if bare_serde_name(&name) {
                return name;
            }
        }
        format!("<{}{}>", self.inner.handle, self.inner.suffix)
    }

    /// The name of the enum variant this tag selects: a local tag's name after its first `!`, as
    /// readers built on libyaml name it, so `!<!!x>` selects `!x`, or any other tag's full name in
    /// angle brackets, as in [`Tag::serde_name`]. Unlike a Serde name, a local name is never
    /// bracketed, so `Tag::local(name)` selects the variant `name`, which is how a variant is
    /// written, and a local tag spelled `!%3Ctag:x:B%3E` selects the variant that `!<tag:x:B>`
    /// selects.
    pub(crate) fn variant_name(&self) -> String {
        match self.inner.handle.strip_prefix('!') {
            Some(prefix) => format!("{prefix}{}", self.inner.suffix),
            None => format!("<{}{}>", self.inner.handle, self.inner.suffix),
        }
    }

    /// Whether this is the non-specific tag `!`, which makes a scalar a string (YAML 1.2.2
    /// section 6.9.1). It is the same as a local tag with an empty name.
    pub(crate) fn is_non_specific(&self) -> bool {
        self.inner.handle == "!" && self.inner.suffix.is_empty()
    }

    /// Whether this is a local tag (its full name starts with `!`), the non-specific `!` included.
    pub(crate) fn is_local(&self) -> bool {
        self.inner.handle.starts_with('!')
            || (self.inner.handle.is_empty() && self.inner.suffix.starts_with('!'))
    }

    /// Whether this is a global tag outside the YAML 1.2 core schema, such as `!!timestamp`,
    /// `!!binary`, or `!<tag:example.com,2000:x>`.
    pub(crate) fn is_non_core_global(&self) -> bool {
        !self.is_local() && !self.is_core_scalar() && !self.is_core_collection()
    }

    /// [`Tag::variant_name`], which moves the suffix of a tag written with `!` rather than
    /// copying it.
    pub(crate) fn into_variant_name(self) -> String {
        if self.inner.handle == "!" {
            self.inner.suffix
        } else {
            self.variant_name()
        }
    }

    /// The tag a Serde name stands for (see [`Tag::serde_name`]). A bare
    /// name is a local tag.
    pub(crate) fn from_serde_name(name: &str) -> Self {
        match name
            .strip_prefix('<')
            .and_then(|rest| rest.strip_suffix('>'))
        {
            Some(full) => Self::new("", full),
            None if name.starts_with('!') => Self::new("", name),
            None => Self::local(name),
        }
    }

    fn full(&self) -> impl Iterator<Item = u8> + '_ {
        self.inner.handle.bytes().chain(self.inner.suffix.bytes())
    }

    /// Create a core-schema tag such as `!!str`.
    #[must_use]
    pub fn core(suffix: impl Into<String>) -> Self {
        Self::new(CORE_PREFIX, suffix)
    }

    /// Create a local tag such as `!Custom`.
    #[must_use]
    pub fn local(suffix: impl Into<String>) -> Self {
        Self::new("!", suffix)
    }

    /// The tag handle.
    #[must_use]
    pub fn handle(&self) -> &str {
        &self.inner.handle
    }

    /// The bytes of text this tag holds, which every copy of it holds too.
    pub(crate) fn text_len(&self) -> usize {
        self.inner
            .handle
            .len()
            .saturating_add(self.inner.suffix.len())
    }

    /// The tag suffix.
    #[must_use]
    pub fn suffix(&self) -> &str {
        &self.inner.suffix
    }

    /// Whether this is a core-schema tag.
    #[must_use]
    pub fn is_core(&self) -> bool {
        self.inner.handle == CORE_PREFIX
    }
}

impl PartialEq for Tag {
    fn eq(&self, other: &Self) -> bool {
        self.inner.handle.len() + self.inner.suffix.len()
            == other.inner.handle.len() + other.inner.suffix.len()
            && self.full().eq(other.full())
    }
}

impl Eq for Tag {}

impl Hash for Tag {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // Equal tags can split their text differently, and not every hasher reads two writes as
        // one, so the split can't show.
        for byte in self.full() {
            state.write_u8(byte);
        }
        state.write_u8(0xff);
    }
}

impl PartialOrd for Tag {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Tag {
    fn cmp(&self, other: &Self) -> Ordering {
        self.full().cmp(other.full())
    }
}

impl fmt::Display for Tag {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.inner.handle == "!" {
            write!(formatter, "!{}", self.inner.suffix)
        } else if self.inner.handle == CORE_PREFIX {
            write!(formatter, "!!{}", self.inner.suffix)
        } else {
            write!(formatter, "{}{}", self.inner.handle, self.inner.suffix)
        }
    }
}

/// A tagged value.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TaggedValue {
    /// The tag.
    pub tag: Tag,
    /// The tagged value.
    pub value: Value,
}

impl TaggedValue {
    /// Create a tagged value.
    #[must_use]
    pub fn new(tag: Tag, value: Value) -> Self {
        Self { tag, value }
    }
}

/// Whether a local tag's name stands for itself in the Serde data model, unbracketed.
fn bare_serde_name(name: &str) -> bool {
    !name.is_empty() && !name.starts_with(['!', '<'])
}

/// Whether a verbatim tag holding `handle` and then `suffix` reads back as that name: a local name
/// or one with a URI scheme, of URI characters and escapes alone (YAML 1.2.2 section 6.9.1).
///
/// The reader and the writer share this test, so a tag the reader accepts as a verbatim tag is one
/// the writers can write that way, and a tag they cannot write verbatim is refused when it is read.
pub(crate) fn verbatim_reads_back(handle: &str, suffix: &str) -> bool {
    let mut bytes = handle.bytes().chain(suffix.bytes());
    let mut first = true;
    let mut scheme = true;
    let mut has_scheme = false;
    let mut local = false;
    while let Some(byte) = bytes.next() {
        if first {
            first = false;
            local = byte == b'!';
            scheme = byte.is_ascii_alphabetic();
        } else if scheme && !has_scheme {
            if byte == b':' {
                has_scheme = true;
            } else if !(byte.is_ascii_alphanumeric() || matches!(byte, b'+' | b'-' | b'.')) {
                scheme = false;
            }
        }
        if byte == b'%' {
            for _ in 0..2 {
                if !bytes.next().is_some_and(|digit| digit.is_ascii_hexdigit()) {
                    return false;
                }
            }
        } else if !(is_uri_byte(byte) || matches!(byte, b'!' | b',' | b'[' | b']')) {
            return false;
        }
    }
    local || has_scheme
}

/// A URI character that a tag shorthand may hold raw (`ns-tag-char` without escapes): not `!`, a
/// flow indicator, or `%`.
pub(crate) fn is_uri_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric()
        || matches!(
            byte,
            b'-' | b'#'
                | b';'
                | b'/'
                | b'?'
                | b':'
                | b'@'
                | b'&'
                | b'='
                | b'+'
                | b'$'
                | b'_'
                | b'.'
                | b'~'
                | b'*'
                | b'\''
                | b'('
                | b')'
        )
}

#[cfg(test)]
mod tests {
    use super::{Tag, TaggedValue};
    use crate::value::Value;

    /// Equal tags hash alike under any hasher, including one that reads each write on its own.
    #[test]
    fn a_tags_hash_does_not_show_where_its_text_splits() {
        use std::hash::{Hash, Hasher};

        #[derive(Default)]
        struct Writes(Vec<Vec<u8>>);
        impl Hasher for Writes {
            fn write(&mut self, bytes: &[u8]) {
                self.0.push(bytes.to_vec());
            }
            fn finish(&self) -> u64 {
                0
            }
        }
        let writes = |tag: Tag| {
            let mut writes = Writes::default();
            tag.hash(&mut writes);
            writes.0
        };
        let (split, other) = (Tag::new("!ab", "c"), Tag::new("!", "abc"));
        assert_eq!(split, other);
        assert_eq!(writes(split), writes(other));
    }

    #[test]
    fn local_and_core_tags_display_canonically() {
        assert_eq!(Tag::local("Custom").to_string(), "!Custom");
        assert_eq!(Tag::core("str").to_string(), "!!str");
        assert_eq!(
            Tag::new("tag:example.com,2026:", "foo").to_string(),
            "tag:example.com,2026:foo"
        );
    }

    #[test]
    fn serde_names_read_back() {
        for tag in [
            Tag::local("Foo"),
            Tag::local(""),
            Tag::local("!"),
            Tag::local("<x>"),
            Tag::local("a:b"),
            Tag::core("set"),
            Tag::new("tag:example.com,2000:", "foo"),
            Tag::new("", "tag:x"),
            Tag::new("!a-", "b"),
            Tag::new("!", ""),
            Tag::new("!!", "x"),
        ] {
            assert_eq!(Tag::from_serde_name(&tag.serde_name()), tag, "{tag}");
        }
        assert_eq!(Tag::local("Foo").serde_name(), "Foo");
        // A local tag that a named handle wrote has the name it has when written with `!`.
        assert_eq!(Tag::new("!a-", "b").serde_name(), "a-b");
        assert_eq!(Tag::core("set").serde_name(), "<tag:yaml.org,2002:set>");
    }

    #[test]
    fn variant_names_read_back() {
        for (tag, name) in [
            (Tag::local("B"), "B"),
            (Tag::new("!a-", "b"), "a-b"),
            (Tag::new("", "!!x"), "!x"),
            (Tag::new("!!", "x"), "!x"),
            (Tag::local("<!x>"), "<!x>"),
            (Tag::local(""), ""),
            (Tag::new("tag:x:", "B"), "<tag:x:B>"),
            (Tag::core("C"), "<tag:yaml.org,2002:C>"),
        ] {
            assert_eq!(tag.variant_name(), name, "{tag}");
            assert_eq!(tag.clone().into_variant_name(), name, "{tag}");
            // A variant is written under the local tag of its name, which selects it.
            assert_eq!(Tag::local(name).variant_name(), name, "{tag}");
        }
    }

    #[test]
    fn tags_compare_by_full_name() {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        fn hash(tag: &Tag) -> u64 {
            let mut hasher = DefaultHasher::new();
            tag.hash(&mut hasher);
            hasher.finish()
        }
        let verbatim = Tag::new("", "tag:yaml.org,2002:str");
        assert_eq!(verbatim, Tag::core("str"));
        assert!(verbatim.is_core());
        assert_eq!(verbatim.suffix(), "str");
        assert_eq!(Tag::new("", "!x"), Tag::local("x"));
        let handle = Tag::new("tag:example.com,2000:app/", "foo");
        let full = Tag::new("", "tag:example.com,2000:app/foo");
        assert_eq!(handle, full);
        assert_eq!(hash(&handle), hash(&full));
        assert_eq!(handle.cmp(&full), std::cmp::Ordering::Equal);
        assert_ne!(Tag::local("a"), Tag::local("b"));
        assert!(Tag::local("a") < Tag::local("b"));
    }

    #[test]
    fn core_tag_is_recognized() {
        assert!(Tag::core("int").is_core());
        assert!(!Tag::local("int").is_core());
    }

    #[test]
    fn tagged_value_keeps_its_tag_and_payload() {
        let tagged = TaggedValue::new(Tag::local("Custom"), Value::from(1_u64));
        assert_eq!(tagged.tag.to_string(), "!Custom");
        assert_eq!(tagged.value, Value::from(1_u64));
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn tag_is_pointer_sized() {
        assert_eq!(size_of::<Tag>(), 8);
        assert_eq!(size_of::<Option<Tag>>(), 8);
        assert_eq!(
            format!("{:?}", Tag::local("Custom")),
            "Tag { handle: \"!\", suffix: \"Custom\" }"
        );
    }
}