claude_agent/common/
source_type.rs1use 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 std::str::FromStr for SourceType {
27 type Err = std::convert::Infallible;
28
29 fn from_str(s: &str) -> Result<Self, Self::Err> {
30 Ok(match s {
31 "builtin" => Self::Builtin,
32 "project" => Self::Project,
33 "managed" => Self::Managed,
34 "plugin" => Self::Plugin,
35 _ => Self::User,
36 })
37 }
38}
39
40impl SourceType {
41 pub fn from_str_opt(s: Option<&str>) -> Self {
42 s.map(|v| v.parse().unwrap_or_default()).unwrap_or_default()
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_default() {
52 assert_eq!(SourceType::default(), SourceType::User);
53 }
54
55 #[test]
56 fn test_display() {
57 assert_eq!(SourceType::Builtin.to_string(), "builtin");
58 assert_eq!(SourceType::User.to_string(), "user");
59 assert_eq!(SourceType::Project.to_string(), "project");
60 assert_eq!(SourceType::Managed.to_string(), "managed");
61 assert_eq!(SourceType::Plugin.to_string(), "plugin");
62 }
63
64 #[test]
65 fn test_from_str_opt() {
66 assert_eq!(
67 SourceType::from_str_opt(Some("builtin")),
68 SourceType::Builtin
69 );
70 assert_eq!(
71 SourceType::from_str_opt(Some("project")),
72 SourceType::Project
73 );
74 assert_eq!(
75 SourceType::from_str_opt(Some("managed")),
76 SourceType::Managed
77 );
78 assert_eq!(SourceType::from_str_opt(Some("plugin")), SourceType::Plugin);
79 assert_eq!(SourceType::from_str_opt(Some("user")), SourceType::User);
80 assert_eq!(SourceType::from_str_opt(None), SourceType::User);
81 assert_eq!(SourceType::from_str_opt(Some("unknown")), SourceType::User);
82 }
83
84 #[test]
85 fn test_from_str() {
86 assert_eq!(
87 "builtin".parse::<SourceType>().unwrap(),
88 SourceType::Builtin
89 );
90 assert_eq!(
91 "project".parse::<SourceType>().unwrap(),
92 SourceType::Project
93 );
94 assert_eq!(
95 "managed".parse::<SourceType>().unwrap(),
96 SourceType::Managed
97 );
98 assert_eq!("plugin".parse::<SourceType>().unwrap(), SourceType::Plugin);
99 assert_eq!("user".parse::<SourceType>().unwrap(), SourceType::User);
100 assert_eq!("unknown".parse::<SourceType>().unwrap(), SourceType::User);
101 }
102
103 #[test]
104 fn test_serde() {
105 let json = serde_json::to_string(&SourceType::Builtin).unwrap();
106 assert_eq!(json, "\"builtin\"");
107
108 let parsed: SourceType = serde_json::from_str("\"project\"").unwrap();
109 assert_eq!(parsed, SourceType::Project);
110
111 let plugin_json = serde_json::to_string(&SourceType::Plugin).unwrap();
112 assert_eq!(plugin_json, "\"plugin\"");
113
114 let parsed_plugin: SourceType = serde_json::from_str("\"plugin\"").unwrap();
115 assert_eq!(parsed_plugin, SourceType::Plugin);
116 }
117}