Skip to main content

khive_types/
substrate.rs

1//! Substrate discriminant — the 3 data types in khive (ADR-004).
2//!
3//! Full substrate structs live in the sibling modules (`note`, `entity`,
4//! `event`). This module provides the discriminant for typed dispatch and
5//! persistence.
6
7use core::fmt;
8use core::str::FromStr;
9
10/// The 3 substrate types in khive OSS (ADR-004).
11///
12/// - **Note**: temporal-referential records (observations, insights, decisions)
13/// - **Entity**: graph nodes with properties and typed links
14/// - **Event**: universal system log — every verb execution produces one
15#[repr(u8)]
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
19pub enum SubstrateKind {
20    Note = 0,
21    Entity = 1,
22    Event = 2,
23}
24
25pub const SUBSTRATE_COUNT: usize = 3;
26
27impl SubstrateKind {
28    pub const ALL: [SubstrateKind; SUBSTRATE_COUNT] = [
29        SubstrateKind::Note,
30        SubstrateKind::Entity,
31        SubstrateKind::Event,
32    ];
33
34    #[inline]
35    pub const fn name(self) -> &'static str {
36        match self {
37            Self::Note => "note",
38            Self::Entity => "entity",
39            Self::Event => "event",
40        }
41    }
42
43    #[inline]
44    pub const fn from_u8(v: u8) -> Option<Self> {
45        match v {
46            0 => Some(Self::Note),
47            1 => Some(Self::Entity),
48            2 => Some(Self::Event),
49            _ => None,
50        }
51    }
52}
53
54impl fmt::Display for SubstrateKind {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        f.write_str(self.name())
57    }
58}
59
60const SUBSTRATE_KIND_VALID: &[&str] = &["note", "entity", "event"];
61
62impl FromStr for SubstrateKind {
63    type Err = crate::error::UnknownVariant;
64
65    fn from_str(s: &str) -> Result<Self, Self::Err> {
66        match s {
67            "note" | "Note" => Ok(Self::Note),
68            "entity" | "Entity" => Ok(Self::Entity),
69            "event" | "Event" => Ok(Self::Event),
70            other => Err(crate::error::UnknownVariant::new(
71                "substrate_kind",
72                other,
73                SUBSTRATE_KIND_VALID,
74            )),
75        }
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn all_variants() {
85        assert_eq!(SubstrateKind::ALL.len(), SUBSTRATE_COUNT);
86        for (i, &kind) in SubstrateKind::ALL.iter().enumerate() {
87            assert_eq!(kind as u8, i as u8);
88            assert_eq!(SubstrateKind::from_u8(i as u8), Some(kind));
89        }
90    }
91
92    #[test]
93    fn parse_roundtrip() {
94        for kind in SubstrateKind::ALL {
95            let parsed: SubstrateKind = kind.name().parse().unwrap();
96            assert_eq!(parsed, kind);
97        }
98    }
99
100    #[test]
101    fn out_of_range() {
102        assert_eq!(SubstrateKind::from_u8(3), None);
103        assert_eq!(SubstrateKind::from_u8(255), None);
104    }
105}