printnanny_settings/
lib.rs

1pub mod cam;
2pub mod error;
3pub mod klipper;
4pub mod mainsail;
5pub mod moonraker;
6pub mod octoprint;
7pub mod paths;
8pub mod printnanny;
9pub mod vcs;
10
11// re-export crates
12pub use clap;
13pub use figment;
14pub use git2;
15pub use printnanny_api_client; // OpenAPI v3 client
16pub use printnanny_asyncapi_models; // AsyncAPI 2.x Models
17pub use sys_info;
18pub use toml;
19
20use clap::{ArgEnum, PossibleValue};
21use serde::{Deserialize, Serialize};
22
23use printnanny_asyncapi_models::SettingsFormat as SettingsFormatPayload;
24
25#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Deserialize, Serialize)]
26pub enum SettingsFormat {
27    #[serde(rename = "ini")]
28    Ini,
29    #[serde(rename = "json")]
30    Json,
31    #[serde(rename = "toml")]
32    Toml,
33    #[serde(rename = "yaml")]
34    Yaml,
35}
36
37impl SettingsFormat {
38    pub fn possible_values() -> impl Iterator<Item = PossibleValue<'static>> {
39        SettingsFormat::value_variants()
40            .iter()
41            .filter_map(ArgEnum::to_possible_value)
42    }
43}
44
45impl std::fmt::Display for SettingsFormat {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        self.to_possible_value()
48            .expect("no values are skipped")
49            .get_name()
50            .fmt(f)
51    }
52}
53
54impl std::str::FromStr for SettingsFormat {
55    type Err = String;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        for variant in Self::value_variants() {
59            if variant.to_possible_value().unwrap().matches(s, false) {
60                return Ok(*variant);
61            }
62        }
63        Err(format!("Invalid variant: {}", s))
64    }
65}
66
67impl From<SettingsFormat> for SettingsFormatPayload {
68    fn from(f: SettingsFormat) -> SettingsFormatPayload {
69        match f {
70            SettingsFormat::Ini => SettingsFormatPayload::Ini,
71            SettingsFormat::Json => SettingsFormatPayload::Json,
72            SettingsFormat::Toml => SettingsFormatPayload::Toml,
73            SettingsFormat::Yaml => SettingsFormatPayload::Yaml,
74        }
75    }
76}