Skip to main content

diagnostics/
phase.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3use template_model::DefectPhase;
4
5/// Validation pipeline phase a diagnostic originates from.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7#[cfg_attr(feature = "wasm-bindings", derive(tsify::Tsify))]
8#[cfg_attr(feature = "uniffi-bindings", derive(uniffi::Enum))]
9#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
10pub enum Phase {
11    Parse,
12    Schema,
13    Lint,
14}
15
16impl Phase {
17    pub const fn as_str(&self) -> &'static str {
18        match self {
19            Phase::Parse => "parse",
20            Phase::Schema => "schema",
21            Phase::Lint => "lint",
22        }
23    }
24}
25
26impl From<DefectPhase> for Phase {
27    fn from(phase: DefectPhase) -> Self {
28        match phase {
29            DefectPhase::Parse => Phase::Parse,
30            DefectPhase::Lint => Phase::Lint,
31        }
32    }
33}
34
35impl fmt::Display for Phase {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        f.write_str(self.as_str())
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn as_str_returns_lowercase_for_all_variants() {
47        assert_eq!(Phase::Parse.as_str(), "parse");
48        assert_eq!(Phase::Schema.as_str(), "schema");
49        assert_eq!(Phase::Lint.as_str(), "lint");
50    }
51
52    #[test]
53    fn serde_round_trips_all_variants() {
54        for phase in [Phase::Parse, Phase::Schema, Phase::Lint] {
55            let json = serde_json::to_string(&phase).unwrap();
56            let back: Phase = serde_json::from_str(&json).unwrap();
57            assert_eq!(back, phase, "round-trip failed for {:?}", phase);
58        }
59    }
60
61    #[test]
62    fn serializes_as_screaming_snake_case() {
63        assert_eq!(serde_json::to_string(&Phase::Lint).unwrap(), "\"LINT\"");
64        assert_eq!(serde_json::to_string(&Phase::Parse).unwrap(), "\"PARSE\"");
65        assert_eq!(serde_json::to_string(&Phase::Schema).unwrap(), "\"SCHEMA\"");
66    }
67}