Debian Codegraph @

src/format/format_no_serde.rs

raw symbols SCIP
1 //! Implements helper functions for the built-in `AclEntry` format.
2 //! These are used when `serde` is not available
3
4 use std::fmt;
5 use std::io;
6
7 use crate::aclentry::AclEntryKind;
8 use crate::flag::FlagName;
9 use crate::perm::PermName;
10
11 const ACLENTRYKINDS: &'static [(AclEntryKind, &'static str)] = &[
12 (AclEntryKind::User, "user"),
13 (AclEntryKind::Group, "group"),
14 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
15 (AclEntryKind::Mask, "mask"),
16 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
17 (AclEntryKind::Other, "other"),
18 #[cfg(target_os = "freebsd")]
19 (AclEntryKind::Everyone, "everyone"),
20 (AclEntryKind::Unknown, "unknown"),
21 ];
22
23 const FLAGS: &'static [(FlagName, &'static str)] = &[
24 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
25 (FlagName::inherited, "inherited"),
26 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
27 (FlagName::file_inherit, "file_inherit"),
28 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
29 (FlagName::directory_inherit, "directory_inherit"),
30 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
31 (FlagName::limit_inherit, "limit_inherit"),
32 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
33 (FlagName::only_inherit, "only_inherit"),
34 #[cfg(any(target_os = "linux", target_os = "freebsd"))]
35 (FlagName::default, "default"),
36 ];
37
38 const PERMS: &'static [(PermName, &'static str)] = &[
39 (PermName::read, "read"),
40 (PermName::write, "write"),
41 (PermName::execute, "execute"),
42 #[cfg(target_os = "freebsd")]
43 (PermName::read_data, "read_data"),
44 #[cfg(target_os = "freebsd")]
45 (PermName::write_data, "write_data"),
46 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
47 (PermName::delete, "delete"),
48 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
49 (PermName::append, "append"),
50 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
51 (PermName::delete_child, "delete_child"),
52 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
53 (PermName::readattr, "readattr"),
54 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
55 (PermName::writeattr, "writeattr"),
56 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
57 (PermName::readextattr, "readextattr"),
58 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
59 (PermName::writeextattr, "writeextattr"),
60 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
61 (PermName::readsecurity, "readsecurity"),
62 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
63 (PermName::writesecurity, "writesecurity"),
64 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
65 (PermName::chown, "chown"),
66 #[cfg(any(target_os = "macos", target_os = "freebsd"))]
67 (PermName::sync, "sync"),
68 ];
69
70 /// Write value of an enum as a string using the given (enum, str) table.
71 fn write_enum<T: PartialEq>(
72 f: &mut fmt::Formatter,
73 value: T,
74 table: &'static [(T, &'static str)],
75 ) -> fmt::Result {
76 match table.iter().find(|item| item.0 == value) {
77 Some((_, name)) => write!(f, "{name}"),
78 None => write!(f, "!!"),
79 }
80 }
81
82 /// Read value of an enum from a string using the given (enum, str) table.
83 fn read_enum<T: Copy>(s: &str, table: &'static [(T, &'static str)]) -> Result<T> {
84 match table.iter().find(|item| item.1 == s) {
85 Some((value, _)) => Ok(*value),
86 None => Err(err_enum(s, table)),
87 }
88 }
89
90 /// Produce the error message when the variant can't be found.
91 fn err_enum<T>(s: &str, table: &'static [(T, &'static str)]) -> Error {
92 let variants = table
93 .iter()
94 .map(|item| format!("`{}`", item.1))
95 .collect::<Vec<String>>()
96 .join(", ");
97
98 let msg = if table.len() == 1 {
99 format!("unknown variant `{s}`, expected {variants}")
100 } else {
101 format!("unknown variant `{s}`, expected one of {variants}")
102 };
103
104 Error::Message(msg)
105 }
106
107 /// Write value of an AclEntryKind.
108 pub fn write_aclentrykind(f: &mut fmt::Formatter, value: AclEntryKind) -> fmt::Result {
109 write_enum(f, value, ACLENTRYKINDS)
110 }
111
112 // Read value of an AclEntryKind.
113 pub fn read_aclentrykind(s: &str) -> Result<AclEntryKind> {
114 read_enum(s, ACLENTRYKINDS)
115 }
116
117 /// Write value of a FlagName.
118 pub fn write_flagname(f: &mut fmt::Formatter, value: FlagName) -> fmt::Result {
119 write_enum(f, value, FLAGS)
120 }
121
122 // Read value of a FlagName.
123 pub fn read_flagname(s: &str) -> Result<FlagName> {
124 read_enum(s, FLAGS)
125 }
126
127 /// Write value of a PermName.
128 pub fn write_permname(f: &mut fmt::Formatter, value: PermName) -> fmt::Result {
129 write_enum(f, value, PERMS)
130 }
131
132 // Read value of a PermName.
133 pub fn read_permname(s: &str) -> Result<PermName> {
134 read_enum(s, PERMS)
135 }
136
137 ////////////////////////////////////////////////////////////////////////////////
138
139 #[derive(Clone, Debug, PartialEq)]
140 pub enum Error {
141 Message(String),
142 NotImplemented,
143 }
144
145 impl From<Error> for io::Error {
146 fn from(err: Error) -> Self {
147 io::Error::new(io::ErrorKind::InvalidInput, err)
148 }
149 }
150
151 impl fmt::Display for Error {
152 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153 match self {
154 Error::Message(msg) => write!(f, "{msg}"),
155 Error::NotImplemented => write!(f, "Not implemented"),
156 }
157 }
158 }
159
160 impl std::error::Error for Error {}
161
162 type Result<T> = std::result::Result<T, Error>;