ag_contracts/
permission.rs1use std::fmt;
4use std::str::FromStr;
5
6#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
8pub enum PermissionMode {
9 #[default]
11 AutoEdit,
12 ReadOnly,
15}
16
17impl PermissionMode {
18 pub const ALL: [PermissionMode; 2] = [PermissionMode::AutoEdit, PermissionMode::ReadOnly];
20
21 pub fn label(self) -> &'static str {
23 match self {
24 Self::AutoEdit => "auto_edit",
25 Self::ReadOnly => "read_only",
26 }
27 }
28
29 pub fn display_label(self) -> &'static str {
31 match self {
32 Self::AutoEdit => "Auto Edit",
33 Self::ReadOnly => "Read Only",
34 }
35 }
36
37 pub fn is_read_only(self) -> bool {
39 self == Self::ReadOnly
40 }
41}
42
43impl fmt::Display for PermissionMode {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 write!(f, "{}", self.label())
46 }
47}
48
49impl FromStr for PermissionMode {
50 type Err = String;
51
52 fn from_str(s: &str) -> Result<Self, Self::Err> {
53 match s {
54 "auto_edit" => Ok(PermissionMode::AutoEdit),
55 "read_only" => Ok(PermissionMode::ReadOnly),
56 _ => Err(format!("Unknown permission mode: {s}")),
57 }
58 }
59}
60
61#[cfg(test)]
62#[path = "permission_test.rs"]
63mod tests;