Skip to main content

lux_lib/rockspec/
format.rs

1use miette::Diagnostic;
2use serde::{Deserialize, Serialize};
3use std::fmt::Display;
4use std::str::FromStr;
5use thiserror::Error;
6
7#[derive(Error, Debug, Diagnostic)]
8#[non_exhaustive]
9#[error("invalid rockspec format")]
10pub struct InvalidRockspecFormat(String);
11
12#[derive(Default, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
13#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
14pub enum Format {
15    #[serde(rename = "1.0")]
16    _1_0,
17    #[serde(rename = "2.0")]
18    _2_0,
19    #[serde(rename = "3.0")]
20    _3_0,
21    #[serde(rename = "3.1")]
22    #[default]
23    _3_1,
24    #[serde(rename = "3.2")]
25    _3_2,
26}
27
28impl FromStr for Format {
29    type Err = InvalidRockspecFormat;
30
31    fn from_str(s: &str) -> Result<Self, Self::Err> {
32        match s {
33            "1.0" => Ok(Self::_1_0),
34            "2.0" => Ok(Self::_2_0),
35            "3.0" => Ok(Self::_3_0),
36            "3.1" => Ok(Self::_3_1),
37            "3.2" => Ok(Self::_3_2),
38            txt => Err(InvalidRockspecFormat(txt.to_string())),
39        }
40    }
41}
42
43impl Display for Format {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            Self::_1_0 => write!(f, "1.0"),
47            Self::_2_0 => write!(f, "2.0"),
48            Self::_3_0 => write!(f, "3.0"),
49            Self::_3_1 => write!(f, "3.1"),
50            Self::_3_2 => write!(f, "3.2"),
51        }
52    }
53}