Skip to main content

claude_agent/common/
source_type.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
4#[serde(rename_all = "lowercase")]
5pub enum SourceType {
6    Builtin,
7    #[default]
8    User,
9    Project,
10    Managed,
11    Plugin,
12}
13
14impl std::fmt::Display for SourceType {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            Self::Builtin => write!(f, "builtin"),
18            Self::User => write!(f, "user"),
19            Self::Project => write!(f, "project"),
20            Self::Managed => write!(f, "managed"),
21            Self::Plugin => write!(f, "plugin"),
22        }
23    }
24}
25
26impl SourceType {
27    pub fn from_str_opt(s: Option<&str>) -> Self {
28        match s {
29            Some("builtin") => Self::Builtin,
30            Some("project") => Self::Project,
31            Some("managed") => Self::Managed,
32            Some("plugin") => Self::Plugin,
33            _ => Self::User,
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_default() {
44        assert_eq!(SourceType::default(), SourceType::User);
45    }
46
47    #[test]
48    fn test_display() {
49        assert_eq!(SourceType::Builtin.to_string(), "builtin");
50        assert_eq!(SourceType::User.to_string(), "user");
51        assert_eq!(SourceType::Project.to_string(), "project");
52        assert_eq!(SourceType::Managed.to_string(), "managed");
53        assert_eq!(SourceType::Plugin.to_string(), "plugin");
54    }
55
56    #[test]
57    fn test_from_str_opt() {
58        assert_eq!(
59            SourceType::from_str_opt(Some("builtin")),
60            SourceType::Builtin
61        );
62        assert_eq!(
63            SourceType::from_str_opt(Some("project")),
64            SourceType::Project
65        );
66        assert_eq!(
67            SourceType::from_str_opt(Some("managed")),
68            SourceType::Managed
69        );
70        assert_eq!(SourceType::from_str_opt(Some("plugin")), SourceType::Plugin);
71        assert_eq!(SourceType::from_str_opt(Some("user")), SourceType::User);
72        assert_eq!(SourceType::from_str_opt(None), SourceType::User);
73        assert_eq!(SourceType::from_str_opt(Some("unknown")), SourceType::User);
74    }
75
76    #[test]
77    fn test_serde() {
78        let json = serde_json::to_string(&SourceType::Builtin).unwrap();
79        assert_eq!(json, "\"builtin\"");
80
81        let parsed: SourceType = serde_json::from_str("\"project\"").unwrap();
82        assert_eq!(parsed, SourceType::Project);
83
84        let plugin_json = serde_json::to_string(&SourceType::Plugin).unwrap();
85        assert_eq!(plugin_json, "\"plugin\"");
86
87        let parsed_plugin: SourceType = serde_json::from_str("\"plugin\"").unwrap();
88        assert_eq!(parsed_plugin, SourceType::Plugin);
89    }
90}