Skip to main content

kmp_domain/value_objects/
source_kind.rs

1use crate::DomainError;
2
3/// Classification of the system or actor that produced a fact.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum SourceKind {
6    Human,
7    Agent,
8    Projection,
9    Derived,
10    Unknown,
11}
12
13impl SourceKind {
14    pub fn parse(value: &str) -> Result<Self, DomainError> {
15        match value.trim() {
16            "human" => Ok(Self::Human),
17            "agent" => Ok(Self::Agent),
18            "projection" => Ok(Self::Projection),
19            "derived" => Ok(Self::Derived),
20            "unknown" => Ok(Self::Unknown),
21            other => Err(DomainError::InvalidState(format!(
22                "invalid source_kind `{other}`"
23            ))),
24        }
25    }
26
27    pub fn as_str(&self) -> &'static str {
28        match self {
29            Self::Human => "human",
30            Self::Agent => "agent",
31            Self::Projection => "projection",
32            Self::Derived => "derived",
33            Self::Unknown => "unknown",
34        }
35    }
36
37    pub fn all() -> &'static [Self] {
38        &[
39            Self::Human,
40            Self::Agent,
41            Self::Projection,
42            Self::Derived,
43            Self::Unknown,
44        ]
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn parse_roundtrip() {
54        for kind in SourceKind::all() {
55            let parsed = SourceKind::parse(kind.as_str()).expect("valid");
56            assert_eq!(&parsed, kind);
57        }
58    }
59
60    #[test]
61    fn parse_invalid_returns_error() {
62        assert!(SourceKind::parse("bogus").is_err());
63    }
64}