Skip to main content

commit_wizard/engine/config/
schema.rs

1use serde::{Deserialize, Serialize};
2
3use crate::engine::{ErrorCode, Result};
4
5// external schema for config documents (global, registry, project)
6use super::{BaseConfig, RulesConfig};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Versioned<T> {
10    #[serde(default, skip_serializing_if = "Option::is_none")]
11    pub version: Option<u32>,
12    #[serde(flatten)]
13    pub inner: T,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct ProjectBody {
18    #[serde(flatten)]
19    pub config: BaseConfig,
20    #[serde(default, skip_serializing_if = "Option::is_none")]
21    pub rules: Option<RulesConfig>,
22}
23
24// external representation of global and registry config
25pub type StandardConfig = Versioned<BaseConfig>;
26// external representation of project config
27pub type ProjectConfig = Versioned<ProjectBody>;
28
29impl StandardConfig {
30    pub fn from_toml_str(input: &str) -> Result<Self> {
31        toml::from_str(input).map_err(|err| {
32            ErrorCode::ConfigInvalid
33                .error()
34                .with_context("error", err.to_string())
35        })
36    }
37
38    pub fn minimal() -> Self {
39        Self {
40            version: None,
41            inner: BaseConfig::minimal(),
42        }
43    }
44
45    pub fn standard() -> Self {
46        Self {
47            version: None,
48            inner: BaseConfig::standard(),
49        }
50    }
51
52    pub fn full() -> Self {
53        Self {
54            version: None,
55            inner: BaseConfig::full(),
56        }
57    }
58}
59
60impl ProjectConfig {
61    pub fn from_toml_str(input: &str) -> Result<Self> {
62        toml::from_str(input).map_err(|err| {
63            ErrorCode::ConfigInvalid
64                .error()
65                .with_context("error", err.to_string())
66        })
67    }
68
69    pub fn minimal() -> Self {
70        Self {
71            version: None,
72            inner: ProjectBody {
73                config: BaseConfig::minimal(),
74                rules: None,
75            },
76        }
77    }
78
79    pub fn standard() -> Self {
80        Self {
81            version: None,
82            inner: ProjectBody {
83                config: BaseConfig::standard(),
84                rules: None,
85            },
86        }
87    }
88
89    pub fn full() -> Self {
90        Self {
91            version: None,
92            inner: ProjectBody {
93                config: BaseConfig::full(),
94                rules: None,
95            },
96        }
97    }
98}