big_code_analysis/spaces/space_kind.rs
1//! Inherent and `Display` impl blocks for [`super::SpaceKind`].
2//!
3//! Split out of `spaces.rs` to keep that module focused on the public
4//! API type definitions. The blocks are moved verbatim; method and
5//! trait resolution is by type, so `crate::spaces::SpaceKind`'s methods
6//! and `Display` impl resolve unchanged.
7
8use super::*;
9
10impl SpaceKind {
11 /// Parse a [`SpaceKind`] from its lowercase serialized form — the
12 /// `#[serde(rename_all = "lowercase")]` representation that appears in
13 /// the JSON / wire `kind` field. An unrecognized string maps to
14 /// [`SpaceKind::Unknown`] so a JSON-walking front-end degrades
15 /// gracefully on a future kind rather than erroring.
16 ///
17 /// This is the single source of truth for the string-to-kind mapping a
18 /// consumer needs when it reads a serialized `kind` (the Python
19 /// `to_sarif` binding uses it to apply per-metric threshold scope via
20 /// [`crate::metric_catalog::MetricScope::admits`]). A round-trip test
21 /// pins it against the serde representation so the two cannot drift.
22 #[must_use]
23 pub fn from_serialized(serialized: &str) -> Self {
24 match serialized {
25 "function" => Self::Function,
26 "class" => Self::Class,
27 "struct" => Self::Struct,
28 "trait" => Self::Trait,
29 "impl" => Self::Impl,
30 "unit" => Self::Unit,
31 "namespace" => Self::Namespace,
32 "interface" => Self::Interface,
33 _ => Self::Unknown,
34 }
35 }
36}
37
38impl fmt::Display for SpaceKind {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 let s = match self {
41 SpaceKind::Unknown => "unknown",
42 SpaceKind::Function => "function",
43 SpaceKind::Class => "class",
44 SpaceKind::Struct => "struct",
45 SpaceKind::Trait => "trait",
46 SpaceKind::Impl => "impl",
47 SpaceKind::Unit => "unit",
48 SpaceKind::Namespace => "namespace",
49 SpaceKind::Interface => "interface",
50 };
51 write!(f, "{s}")
52 }
53}