stac_types/
version.rs

1use serde::{Deserialize, Serialize};
2use std::{convert::Infallible, fmt::Display, str::FromStr};
3
4/// A version of the STAC specification.
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Eq, Hash, PartialOrd)]
6#[allow(non_camel_case_types)]
7#[non_exhaustive]
8pub enum Version {
9    /// [v1.0.0](https://github.com/radiantearth/stac-spec/releases/tag/v1.0.0)
10    #[serde(rename = "1.0.0")]
11    v1_0_0,
12
13    /// [v1.1.0-beta.1](https://github.com/radiantearth/stac-spec/releases/tag/v1.1.0-beta.1)
14    #[serde(rename = "1.1.0-beta.1")]
15    v1_1_0_beta_1,
16
17    /// [v1.1.0](https://github.com/radiantearth/stac-spec/releases/tag/v1.1.0)
18    #[serde(rename = "1.1.0")]
19    v1_1_0,
20
21    /// An unknown STAC version.
22    #[serde(untagged)]
23    Unknown(String),
24}
25
26impl FromStr for Version {
27    type Err = Infallible;
28
29    fn from_str(s: &str) -> Result<Self, Self::Err> {
30        match s {
31            "1.0.0" => Ok(Version::v1_0_0),
32            "1.1.0-beta.1" => Ok(Version::v1_1_0_beta_1),
33            "1.1.0" => Ok(Version::v1_1_0),
34            _ => Ok(Version::Unknown(s.to_string())),
35        }
36    }
37}
38
39impl Display for Version {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(
42            f,
43            "{}",
44            match self {
45                Version::v1_0_0 => "1.0.0",
46                Version::v1_1_0_beta_1 => "1.1.0-beta.1",
47                Version::v1_1_0 => "1.1.0",
48                Version::Unknown(v) => v,
49            }
50        )
51    }
52}
53
54impl Default for Version {
55    fn default() -> Self {
56        crate::STAC_VERSION
57    }
58}