Skip to main content

objects/
config_types.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Configuration values shared by local repositories and hosted clients.
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum FsMonitorMode {
9    #[default]
10    Off,
11    Auto,
12    Native,
13    Watchman,
14}
15
16impl FsMonitorMode {
17    pub fn parse(value: &str) -> Option<Self> {
18        match value.trim().to_ascii_lowercase().as_str() {
19            "0" | "off" | "false" | "disabled" => Some(Self::Off),
20            "1" | "auto" | "true" | "enabled" => Some(Self::Auto),
21            "native" | "local" => Some(Self::Native),
22            "watchman" => Some(Self::Watchman),
23            _ => None,
24        }
25    }
26}
27
28#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq, Default)]
29#[serde(rename_all = "lowercase")]
30pub enum OutputFormat {
31    Json,
32    #[default]
33    Text,
34}
35
36impl<'de> Deserialize<'de> for OutputFormat {
37    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
38    where
39        D: serde::Deserializer<'de>,
40    {
41        struct Visitor;
42        impl serde::de::Visitor<'_> for Visitor {
43            type Value = OutputFormat;
44            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45                f.write_str("'text' or 'json'")
46            }
47            fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<OutputFormat, E> {
48                match value {
49                    "text" => Ok(OutputFormat::Text),
50                    "json" => Ok(OutputFormat::Json),
51                    other => Err(E::custom(format!(
52                        "invalid output.format: '{other}' — valid values are 'text' or 'json'"
53                    ))),
54                }
55            }
56        }
57        deserializer.deserialize_str(Visitor)
58    }
59}