Skip to main content

zumen_base/
code.rs

1//! 診断の code と、台帳で決まる性質(決定 0009)。
2//!
3//! コアの code は、台帳(`catalog/codes.toml`)から生成した [`crate::codes`] の定数だけを使う。外部のプラグインは、
4//! 自分の台帳の code を [`Code::plugin`] で作る(決定 0017・0243)。
5
6use core::cmp::Ordering;
7use core::hash::{Hash, Hasher};
8
9/// 診断の重さ。code ごとに決まる。
10#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub enum Severity {
12    /// 何も失っていない。別の表現に置き換えた、既定値を仮定した、など
13    Info,
14    /// 近似した、見た目だけにした、落とした、壊れた所を直した。結果は使える
15    Warning,
16    /// 処理が失敗した、または結果が欠けている
17    Error,
18}
19
20/// 変換の結果の段のうち、`native` 以外のもの。
21#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub enum Fidelity {
23    /// 別の表現で同じ意味
24    Equivalent,
25    /// 許容差つきの近似。許容差は診断の値に入れる
26    Approximated,
27    /// 見た目だけ
28    Drawn,
29    /// 落とした
30    Dropped,
31}
32
33/// 診断の値の型
34#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
35pub enum ParamType {
36    /// 符号のない整数
37    UInt,
38    /// 符号のある整数
39    Int,
40    /// 浮動小数
41    Float,
42    /// 図面に記録された名前などの文字列。翻訳しない
43    Text,
44    /// 列挙の値のキー。名前はカタログで引く
45    Key,
46}
47
48/// 台帳で決めた、値の名前と型
49#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
50pub struct Param {
51    name: &'static str,
52    ty: ParamType,
53}
54
55impl Param {
56    pub(crate) const fn new(name: &'static str, ty: ParamType) -> Self {
57        Self { name, ty }
58    }
59
60    /// 外部のプラグインの code の値(決定 0243)
61    pub const fn plugin(name: &'static str, ty: ParamType) -> Self {
62        Self::new(name, ty)
63    }
64
65    /// 値の名前
66    pub const fn name(&self) -> &'static str {
67        self.name
68    }
69
70    /// 値の型
71    pub const fn ty(&self) -> ParamType {
72        self.ty
73    }
74}
75
76/// 診断の code。名前で比べる。
77#[derive(Clone, Copy, Debug)]
78pub struct Code {
79    name: &'static str,
80    severity: Severity,
81    fidelity: Option<Fidelity>,
82    params: &'static [Param],
83    group_by: &'static [&'static str],
84}
85
86impl Code {
87    pub(crate) const fn new(
88        name: &'static str,
89        severity: Severity,
90        fidelity: Option<Fidelity>,
91        params: &'static [Param],
92        group_by: &'static [&'static str],
93    ) -> Self {
94        Self {
95            name,
96            severity,
97            fidelity,
98            params,
99            group_by,
100        }
101    }
102
103    /// 外部のプラグインの code(決定 0017・0243)。名前は、プラグインの形式のキーと `.` で始める
104    /// (`<キー>.file.invalid` など)。コアの crate は使わない
105    pub const fn plugin(
106        name: &'static str,
107        severity: Severity,
108        fidelity: Option<Fidelity>,
109        params: &'static [Param],
110        group_by: &'static [&'static str],
111    ) -> Self {
112        Self::new(name, severity, fidelity, params, group_by)
113    }
114
115    /// 変わらない名前(`budget.records.exceeded` など)
116    pub const fn name(&self) -> &'static str {
117        self.name
118    }
119
120    /// 重さ
121    pub const fn severity(&self) -> Severity {
122        self.severity
123    }
124
125    /// 変換の結果の段。変換の結果を表さない code では `None`
126    pub const fn fidelity(&self) -> Option<Fidelity> {
127        self.fidelity
128    }
129
130    /// 台帳で決めた値
131    pub const fn params(&self) -> &'static [Param] {
132        self.params
133    }
134
135    /// まとめるときに比べる値の名前
136    pub const fn group_by(&self) -> &'static [&'static str] {
137        self.group_by
138    }
139
140    /// 名前で値を引く
141    pub fn param(&self, name: &str) -> Option<Param> {
142        self.params.iter().find(|param| param.name == name).copied()
143    }
144}
145
146impl PartialEq for Code {
147    fn eq(&self, other: &Self) -> bool {
148        self.name == other.name
149    }
150}
151
152impl Eq for Code {}
153
154impl PartialOrd for Code {
155    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
156        Some(self.cmp(other))
157    }
158}
159
160impl Ord for Code {
161    fn cmp(&self, other: &Self) -> Ordering {
162        self.name.cmp(other.name)
163    }
164}
165
166impl Hash for Code {
167    fn hash<H: Hasher>(&self, state: &mut H) {
168        self.name.hash(state);
169    }
170}
171
172#[cfg(test)]
173mod tests {
174    use crate::codes;
175
176    #[test]
177    fn all_codes_are_sorted_unique_and_well_formed() {
178        let names: Vec<&str> = codes::ALL.iter().map(|code| code.name()).collect();
179        let mut sorted = names.clone();
180        sorted.sort_unstable();
181        sorted.dedup();
182        assert_eq!(names, sorted);
183        for name in names {
184            assert_eq!(name.split('.').count(), 3, "{name}");
185        }
186    }
187
188    #[test]
189    fn looks_up_params_by_name() {
190        let code = codes::BUDGET_RECORDS_EXCEEDED;
191        assert!(code.param("limit").is_some());
192        assert!(code.param("missing").is_none());
193    }
194}