codewhale_protocol/
role.rs1use std::fmt;
21
22use serde::{Deserialize, Deserializer, Serialize, Serializer};
23
24use crate::request::INTERRUPTED_ASSISTANT_ROLE;
25
26#[derive(Debug, Clone, PartialEq, Eq, Hash)]
33pub enum Role {
34 User,
36 Assistant,
38 System,
41 Developer,
45 InterruptedAssistant,
48 Unrecognized(String),
51}
52
53impl Role {
54 #[must_use]
58 pub fn as_str(&self) -> &str {
59 match self {
60 Self::User => "user",
61 Self::Assistant => "assistant",
62 Self::System => "system",
63 Self::Developer => "developer",
64 Self::InterruptedAssistant => INTERRUPTED_ASSISTANT_ROLE,
65 Self::Unrecognized(raw) => raw.as_str(),
66 }
67 }
68
69 #[must_use]
71 pub fn is_assistant_like(&self) -> bool {
72 matches!(self, Self::Assistant | Self::InterruptedAssistant)
73 }
74}
75
76impl From<&str> for Role {
77 fn from(value: &str) -> Self {
78 match value {
79 "user" => Self::User,
80 "assistant" => Self::Assistant,
81 "system" => Self::System,
82 "developer" => Self::Developer,
83 INTERRUPTED_ASSISTANT_ROLE => Self::InterruptedAssistant,
84 other => Self::Unrecognized(other.to_string()),
85 }
86 }
87}
88
89impl From<String> for Role {
90 fn from(value: String) -> Self {
91 match value.as_str() {
92 "user" => Self::User,
93 "assistant" => Self::Assistant,
94 "system" => Self::System,
95 "developer" => Self::Developer,
96 INTERRUPTED_ASSISTANT_ROLE => Self::InterruptedAssistant,
97 _ => Self::Unrecognized(value),
98 }
99 }
100}
101
102impl fmt::Display for Role {
103 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104 f.write_str(self.as_str())
105 }
106}
107
108impl PartialEq<str> for Role {
109 fn eq(&self, other: &str) -> bool {
110 self.as_str() == other
111 }
112}
113
114impl PartialEq<&str> for Role {
115 fn eq(&self, other: &&str) -> bool {
116 self.as_str() == *other
117 }
118}
119
120impl PartialEq<String> for Role {
121 fn eq(&self, other: &String) -> bool {
122 self.as_str() == other.as_str()
123 }
124}
125
126impl PartialEq<Role> for str {
127 fn eq(&self, other: &Role) -> bool {
128 self == other.as_str()
129 }
130}
131
132impl PartialEq<Role> for &str {
133 fn eq(&self, other: &Role) -> bool {
134 *self == other.as_str()
135 }
136}
137
138impl PartialEq<Role> for String {
139 fn eq(&self, other: &Role) -> bool {
140 self.as_str() == other.as_str()
141 }
142}
143
144impl Serialize for Role {
145 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
146 serializer.serialize_str(self.as_str())
147 }
148}
149
150impl<'de> Deserialize<'de> for Role {
151 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
152 Ok(Self::from(String::deserialize(deserializer)?))
153 }
154}
155
156#[cfg(test)]
157mod tests {
158 use super::Role;
159 use crate::request::INTERRUPTED_ASSISTANT_ROLE;
160
161 #[test]
162 fn known_roles_serialize_as_the_strings_they_replaced() {
163 for (role, expected) in [
164 (Role::User, "\"user\""),
165 (Role::Assistant, "\"assistant\""),
166 (Role::System, "\"system\""),
167 (Role::Developer, "\"developer\""),
168 (Role::InterruptedAssistant, "\"assistant_interrupted\""),
169 ] {
170 assert_eq!(serde_json::to_string(&role).expect("serialize"), expected);
171 }
172 }
173
174 #[test]
175 fn serialized_bytes_match_the_raw_string_encoding() {
176 for raw in [
180 "user",
181 "assistant",
182 "system",
183 "developer",
184 INTERRUPTED_ASSISTANT_ROLE,
185 ] {
186 assert_eq!(
187 serde_json::to_vec(&Role::from(raw)).expect("serialize role"),
188 serde_json::to_vec(raw).expect("serialize string"),
189 "role {raw} must serialize like its string",
190 );
191 }
192 }
193
194 #[test]
195 fn unknown_roles_round_trip_verbatim() {
196 let role = Role::from("future_role");
197 assert_eq!(role, Role::Unrecognized("future_role".to_string()));
198 let encoded = serde_json::to_string(&role).expect("serialize");
199 assert_eq!(encoded, "\"future_role\"");
200 let decoded: Role = serde_json::from_str(&encoded).expect("deserialize");
201 assert_eq!(decoded, role);
202 }
203
204 #[test]
205 fn interrupted_assistant_is_not_assistant() {
206 let decoded: Role =
207 serde_json::from_str("\"assistant_interrupted\"").expect("deserialize sentinel");
208 assert_eq!(decoded, Role::InterruptedAssistant);
209 assert_ne!(decoded, Role::Assistant);
210 assert_ne!(decoded, "assistant");
211 assert!(decoded.is_assistant_like());
212 }
213
214 #[test]
215 fn string_comparisons_work_in_both_directions() {
216 assert_eq!(Role::User, "user");
217 assert_eq!("user", Role::User);
218 assert_eq!(Role::User, "user".to_string());
219 assert_ne!(Role::System, "user");
220 }
221}