Skip to main content

rust_yaml/
version.rs

1//! YAML version handling for `%YAML` directive support.
2//!
3//! The YAML 1.2.2 spec is the default. A `%YAML 1.1` directive enables
4//! YAML 1.1 implicit-resolution rules (`yes`/`no`/`on`/`off` as booleans).
5
6/// YAML spec version, derived from a `%YAML major.minor` directive or
7/// defaulted when no directive is present.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum YamlVersion {
10    /// YAML 1.1 — accepts legacy boolean forms.
11    V1_1,
12    /// YAML 1.2 (default per current spec).
13    #[default]
14    V1_2,
15}
16
17impl YamlVersion {
18    /// Convert a parsed `(major, minor)` directive pair into a `YamlVersion`.
19    ///
20    /// `1.1` selects [`Self::V1_1`]. Any other `1.x` selects [`Self::V1_2`]
21    /// (the 1.2.2 spec states that 1.x documents must be processed by the
22    /// most-recent 1.x parser).
23    #[must_use]
24    pub const fn from_directive(major: u8, minor: u8) -> Self {
25        match (major, minor) {
26            (1, 1) => Self::V1_1,
27            _ => Self::V1_2,
28        }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn default_is_1_2() {
38        assert_eq!(YamlVersion::default(), YamlVersion::V1_2);
39    }
40
41    #[test]
42    fn from_directive_recognizes_1_1() {
43        assert_eq!(YamlVersion::from_directive(1, 1), YamlVersion::V1_1);
44    }
45
46    #[test]
47    fn from_directive_defaults_to_1_2_for_other_1x() {
48        assert_eq!(YamlVersion::from_directive(1, 2), YamlVersion::V1_2);
49        assert_eq!(YamlVersion::from_directive(1, 3), YamlVersion::V1_2);
50        assert_eq!(YamlVersion::from_directive(1, 0), YamlVersion::V1_2);
51    }
52}