ag_agent/model/
permission.rs1use std::fmt;
2use std::str::FromStr;
3
4#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
6pub enum PermissionMode {
7 #[default]
9 AutoEdit,
10 AutoEditAddressComments,
13 ReadOnly,
16}
17
18impl PermissionMode {
19 pub const ALL: [PermissionMode; 3] = [
21 PermissionMode::AutoEdit,
22 PermissionMode::AutoEditAddressComments,
23 PermissionMode::ReadOnly,
24 ];
25
26 pub fn description(self) -> &'static str {
28 match self {
29 Self::AutoEdit => "Allow the agent to edit files automatically.",
30 Self::AutoEditAddressComments => {
31 "Auto Edit, then address focused-review suggestions up to 3 times."
32 }
33 Self::ReadOnly => "Inspect the repository without changing files.",
34 }
35 }
36
37 pub fn label(self) -> &'static str {
39 match self {
40 Self::AutoEdit => "auto_edit",
41 Self::AutoEditAddressComments => "auto_edit_address_comments",
42 Self::ReadOnly => "read_only",
43 }
44 }
45
46 pub fn display_label(self) -> &'static str {
48 match self {
49 Self::AutoEdit => "Auto Edit",
50 Self::AutoEditAddressComments => "Auto Edit + Auto Address Comments",
51 Self::ReadOnly => "Read Only",
52 }
53 }
54
55 pub fn is_read_only(self) -> bool {
57 self == Self::ReadOnly
58 }
59}
60
61impl fmt::Display for PermissionMode {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 write!(f, "{}", self.label())
64 }
65}
66
67impl FromStr for PermissionMode {
68 type Err = String;
69
70 fn from_str(s: &str) -> Result<Self, Self::Err> {
71 match s {
72 "auto_edit" => Ok(PermissionMode::AutoEdit),
73 "auto_edit_address_comments" => Ok(PermissionMode::AutoEditAddressComments),
74 "read_only" => Ok(PermissionMode::ReadOnly),
75 _ => Err(format!("Unknown permission mode: {s}")),
76 }
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn test_from_str_accepts_supported_modes() {
86 let permission_modes = ["auto_edit", "auto_edit_address_comments", "read_only"];
88
89 let parsed_permission_modes = permission_modes.map(PermissionMode::from_str);
91
92 assert_eq!(
94 parsed_permission_modes,
95 [
96 Ok(PermissionMode::AutoEdit),
97 Ok(PermissionMode::AutoEditAddressComments),
98 Ok(PermissionMode::ReadOnly),
99 ]
100 );
101 }
102
103 #[test]
104 fn test_from_str_rejects_removed_permission_modes() {
105 let removed_mode = "autonomous";
107
108 let parsed_permission_mode = PermissionMode::from_str(removed_mode);
110
111 assert_eq!(
113 parsed_permission_mode,
114 Err("Unknown permission mode: autonomous".to_string())
115 );
116 }
117
118 #[test]
119 fn test_default_uses_auto_edit_mode() {
120 let permission_mode = PermissionMode::default();
122
123 assert_eq!(permission_mode, PermissionMode::AutoEdit);
125 }
126
127 #[test]
128 fn test_label_and_display_label_return_persisted_and_user_facing_text() {
129 let permission_modes = PermissionMode::ALL;
131
132 let labels = permission_modes.map(PermissionMode::label);
134 let display_labels = permission_modes.map(PermissionMode::display_label);
135 let descriptions = permission_modes.map(PermissionMode::description);
136 let read_only = permission_modes.map(PermissionMode::is_read_only);
137
138 assert_eq!(
140 labels,
141 ["auto_edit", "auto_edit_address_comments", "read_only"]
142 );
143 assert_eq!(
144 display_labels,
145 [
146 "Auto Edit",
147 "Auto Edit + Auto Address Comments",
148 "Read Only"
149 ]
150 );
151 assert_eq!(
152 descriptions,
153 [
154 "Allow the agent to edit files automatically.",
155 "Auto Edit, then address focused-review suggestions up to 3 times.",
156 "Inspect the repository without changing files.",
157 ]
158 );
159 assert_eq!(read_only, [false, false, true]);
160 }
161
162 #[test]
163 fn test_display_uses_persisted_label() {
164 let permission_mode = PermissionMode::AutoEdit;
166
167 let formatted = permission_mode.to_string();
169
170 assert_eq!(formatted, "auto_edit");
172 }
173}