Skip to main content

opendev_models/config/
formatter.rs

1//! Formatter configuration models.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Formatter configuration.
7///
8/// Controls auto-formatting of files after edit/write operations.
9/// Can disable all formatting, disable specific built-in formatters,
10/// or add custom formatters.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(untagged)]
13pub enum FormatterConfig {
14    /// Set to `false` to disable all formatters.
15    Disabled(bool),
16    /// Custom configuration with overrides.
17    Custom(FormatterOverrides),
18}
19
20impl Default for FormatterConfig {
21    fn default() -> Self {
22        FormatterConfig::Custom(FormatterOverrides::default())
23    }
24}
25
26impl FormatterConfig {
27    /// Check if this is the default (no overrides).
28    pub fn is_default(&self) -> bool {
29        matches!(self, FormatterConfig::Custom(o) if o.overrides.is_empty())
30    }
31
32    /// Check if formatting is globally disabled.
33    pub fn is_disabled(&self) -> bool {
34        matches!(self, FormatterConfig::Disabled(false))
35    }
36
37    /// Get custom formatter overrides (empty if disabled).
38    pub fn overrides(&self) -> &HashMap<String, FormatterOverride> {
39        match self {
40            FormatterConfig::Custom(o) => &o.overrides,
41            FormatterConfig::Disabled(_) => {
42                static EMPTY: std::sync::LazyLock<HashMap<String, FormatterOverride>> =
43                    std::sync::LazyLock::new(HashMap::new);
44                &EMPTY
45            }
46        }
47    }
48}
49
50/// Map of formatter name to override settings.
51#[derive(Debug, Clone, Default, Serialize, Deserialize)]
52pub struct FormatterOverrides {
53    #[serde(flatten)]
54    pub overrides: HashMap<String, FormatterOverride>,
55}
56
57/// Override settings for a single formatter.
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct FormatterOverride {
60    /// Disable this formatter.
61    #[serde(default)]
62    pub disabled: bool,
63    /// Custom command (overrides built-in). Use `$FILE` as placeholder.
64    #[serde(default, skip_serializing_if = "Vec::is_empty")]
65    pub command: Vec<String>,
66    /// File extensions this formatter handles.
67    #[serde(default, skip_serializing_if = "Vec::is_empty")]
68    pub extensions: Vec<String>,
69    /// Environment variables to set when running the formatter.
70    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
71    pub environment: HashMap<String, String>,
72}