wdl_grammar/
version.rs

1//! Representation for version definitions.
2
3use std::str::FromStr;
4
5use strum::IntoEnumIterator;
6
7/// Represents a supported V1 WDL version.
8// NOTE: it is expected that this enumeration is in increasing order of 1.x versions.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, strum::EnumIter)]
10#[non_exhaustive]
11pub enum V1 {
12    /// The document version is 1.0.
13    Zero,
14    /// The document version is 1.1.
15    One,
16    /// The document version is 1.2.
17    Two,
18}
19
20impl std::fmt::Display for V1 {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            V1::Zero => write!(f, "1.0"),
24            V1::One => write!(f, "1.1"),
25            V1::Two => write!(f, "1.2"),
26        }
27    }
28}
29
30/// Represents a supported WDL version.
31///
32/// The `Default` implementation of this type returns the most recent
33/// fully-supported ratified version of WDL.
34// NOTE: it is expected that this enumeration is in increasing order of WDL versions.
35#[derive(
36    Debug,
37    Clone,
38    Copy,
39    PartialEq,
40    Eq,
41    PartialOrd,
42    Ord,
43    serde_with::DeserializeFromStr,
44    serde_with::SerializeDisplay,
45)]
46#[non_exhaustive]
47pub enum SupportedVersion {
48    /// The document version is 1.x.
49    V1(V1),
50}
51
52impl SupportedVersion {
53    /// Returns `true` if the other version has the same major version as this
54    /// one.
55    ///
56    /// ```
57    /// # use wdl_grammar::SupportedVersion;
58    /// # use wdl_grammar::version::V1;
59    /// assert!(SupportedVersion::V1(V1::Zero).has_same_major_version(SupportedVersion::V1(V1::Two)));
60    /// ```
61    pub fn has_same_major_version(self, other: SupportedVersion) -> bool {
62        match (self, other) {
63            (SupportedVersion::V1(_), SupportedVersion::V1(_)) => true,
64        }
65    }
66
67    /// Returns an iterator over all supported WDL versions.
68    pub fn all() -> impl Iterator<Item = Self> {
69        V1::iter().map(Self::V1)
70    }
71}
72
73impl Default for SupportedVersion {
74    fn default() -> Self {
75        Self::V1(V1::Two)
76    }
77}
78
79impl std::fmt::Display for SupportedVersion {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        match self {
82            SupportedVersion::V1(version) => write!(f, "{version}"),
83        }
84    }
85}
86
87impl FromStr for SupportedVersion {
88    type Err = String;
89
90    fn from_str(s: &str) -> Result<Self, Self::Err> {
91        match s {
92            "1.0" => Ok(Self::V1(V1::Zero)),
93            "1.1" => Ok(Self::V1(V1::One)),
94            "1.2" => Ok(Self::V1(V1::Two)),
95            _ => Err(s.to_string()),
96        }
97    }
98}