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, 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    #[default]
21    _3_0,
22}
23
24impl FromStr for Format {
25    type Err = InvalidRockspecFormat;
26
27    fn from_str(s: &str) -> Result<Self, Self::Err> {
28        match s {
29            "1.0" => Ok(Self::_1_0),
30            "2.0" => Ok(Self::_2_0),
31            "3.0" => Ok(Self::_3_0),
32            txt => Err(InvalidRockspecFormat(txt.to_string())),
33        }
34    }
35}
36
37impl Display for Format {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            Self::_1_0 => write!(f, "1.0"),
41            Self::_2_0 => write!(f, "2.0"),
42            Self::_3_0 => write!(f, "3.0"),
43        }
44    }
45}