Skip to main content

cityjson_lib/
version.rs

1use std::fmt::{Display, Formatter};
2
3use crate::{Error, Result};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
6pub enum CityJSONVersion {
7    V1_0,
8    V1_1,
9    V2_0,
10}
11
12impl CityJSONVersion {
13    pub(crate) fn supported_versions() -> &'static str {
14        "1.0, 1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.1, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 2.0, 2.0.0, 2.0.1"
15    }
16}
17
18impl Default for CityJSONVersion {
19    fn default() -> Self {
20        Self::V2_0
21    }
22}
23
24impl Display for CityJSONVersion {
25    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26        match self {
27            Self::V1_0 => write!(f, "1.0"),
28            Self::V1_1 => write!(f, "1.1"),
29            Self::V2_0 => write!(f, "2.0"),
30        }
31    }
32}
33
34impl TryFrom<&str> for CityJSONVersion {
35    type Error = Error;
36
37    fn try_from(value: &str) -> Result<Self> {
38        match value {
39            "1.0" | "1.0.0" | "1.0.1" | "1.0.2" | "1.0.3" => Ok(Self::V1_0),
40            "1.1" | "1.1.0" | "1.1.1" | "1.1.2" | "1.1.3" => Ok(Self::V1_1),
41            "2.0" | "2.0.0" | "2.0.1" => Ok(Self::V2_0),
42            other => Err(Error::UnsupportedVersion {
43                found: other.to_string(),
44                supported: Self::supported_versions().to_string(),
45            }),
46        }
47    }
48}
49
50impl TryFrom<String> for CityJSONVersion {
51    type Error = Error;
52
53    fn try_from(value: String) -> Result<Self> {
54        Self::try_from(value.as_str())
55    }
56}