Skip to main content

atlas_local/models/
creation_source.rs

1use std::fmt::Display;
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub enum CreationSource {
5    AtlasCLI,
6    Container,
7    MCPServer,
8    AtlasLocal,
9    Unknown(String),
10}
11
12impl From<&str> for CreationSource {
13    fn from(s: &str) -> Self {
14        match s {
15            "ATLASCLI" => CreationSource::AtlasCLI,
16            "CONTAINER" => CreationSource::Container,
17            "MCPSERVER" => CreationSource::MCPServer,
18            "ATLAS_LOCAL" => CreationSource::AtlasLocal,
19            unknown => CreationSource::Unknown(unknown.to_string()),
20        }
21    }
22}
23
24impl Display for CreationSource {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            CreationSource::AtlasCLI => write!(f, "ATLASCLI"),
28            CreationSource::Container => write!(f, "CONTAINER"),
29            CreationSource::MCPServer => write!(f, "MCPSERVER"),
30            CreationSource::AtlasLocal => write!(f, "ATLAS_LOCAL"),
31            CreationSource::Unknown(s) => write!(f, "{}", s),
32        }
33    }
34}
35
36#[cfg(feature = "serde")]
37impl serde::Serialize for CreationSource {
38    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
39    where
40        S: serde::Serializer,
41    {
42        serializer.serialize_str(&self.to_string())
43    }
44}
45
46#[cfg(feature = "serde")]
47impl<'de> serde::Deserialize<'de> for CreationSource {
48    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
49    where
50        D: serde::Deserializer<'de>,
51    {
52        let s = String::deserialize(deserializer)?;
53        Ok(CreationSource::from(s.as_str()))
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use serde::{Deserialize, Serialize};
60    use serde_json::json;
61
62    use super::*;
63
64    #[test]
65    fn test_creation_source_from_atlascli() {
66        let source = CreationSource::from("ATLASCLI");
67        assert_eq!(source, CreationSource::AtlasCLI);
68    }
69
70    #[test]
71    fn test_creation_source_from_container() {
72        let source = CreationSource::from("CONTAINER");
73        assert_eq!(source, CreationSource::Container);
74    }
75
76    #[test]
77    fn test_creation_source_from_mcp_server() {
78        let source = CreationSource::from("MCPSERVER");
79        assert_eq!(source, CreationSource::MCPServer);
80    }
81
82    #[test]
83    fn test_creation_source_from_atlas_local() {
84        let source = CreationSource::from("ATLAS_LOCAL");
85        assert_eq!(source, CreationSource::AtlasLocal);
86    }
87
88    #[test]
89    fn test_creation_source_from_unknown() {
90        let source = CreationSource::from("some_unknown_source");
91        assert_eq!(
92            source,
93            CreationSource::Unknown("some_unknown_source".to_string())
94        );
95    }
96
97    #[test]
98    fn test_creation_source_to_string_atlascli() {
99        let source = CreationSource::AtlasCLI;
100        assert_eq!(source.to_string(), "ATLASCLI");
101    }
102
103    #[test]
104    fn test_creation_source_to_string_container() {
105        let source = CreationSource::Container;
106        assert_eq!(source.to_string(), "CONTAINER");
107    }
108
109    #[test]
110    fn test_creation_source_to_string_mcp_server() {
111        let source = CreationSource::MCPServer;
112        assert_eq!(source.to_string(), "MCPSERVER");
113    }
114
115    #[test]
116    fn test_creation_source_to_string_atlas_local() {
117        let source = CreationSource::AtlasLocal;
118        assert_eq!(source.to_string(), "ATLAS_LOCAL");
119    }
120
121    #[test]
122    fn test_creation_source_to_string_unknown() {
123        let source = CreationSource::Unknown("custom_source".to_string());
124        assert_eq!(source.to_string(), "custom_source");
125    }
126
127    #[test]
128    fn test_json_serialization() {
129        #[derive(Serialize)]
130        struct Test {
131            source: CreationSource,
132        }
133        let json = serde_json::to_value(&Test {
134            source: CreationSource::AtlasCLI,
135        })
136        .unwrap();
137        assert_eq!(json, json!({"source": "ATLASCLI"}));
138    }
139
140    #[test]
141    fn test_json_deserialization() {
142        #[derive(Debug, Deserialize, PartialEq, Eq)]
143        struct Test {
144            source: CreationSource,
145        }
146        let json = json!({"source": "OTHER"});
147        let source = serde_json::from_value::<Test>(json).unwrap();
148        assert_eq!(
149            source,
150            Test {
151                source: CreationSource::Unknown("OTHER".to_string()),
152            }
153        );
154    }
155}