Skip to main content

coqui_access_acl/
error.rs

1//! Why a schema, an ACL or a walk could not be used.
2
3use core::fmt;
4
5use coqui_access::guatiao::library::ProviderError;
6
7/// Why a schema, an ACL or a walk could not be used. Messages give positions, never the
8/// names or values involved.
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum AclError {
12    /// A right or alias name is empty or holds a control character.
13    InvalidName,
14    /// A schema defines a name twice.
15    DuplicateName,
16    /// A name the schema does not define: in entry `entry` (or in a list given directly,
17    /// `None`), at position `at` of its names.
18    UnknownRight {
19        /// The entry that names it, when the names came from an entry.
20        entry: Option<usize>,
21        /// The position among the names.
22        at: usize,
23    },
24    /// A schema defines more rights than [`Limits::rights`](crate::Limits::rights).
25    TooManyRights,
26    /// An ACL holds more entries than [`Limits::entries`](crate::Limits::entries).
27    TooManyEntries,
28    /// Entry `entry` is a deny in a mode that has none (`Additive`, `Posix`,
29    /// `MostSpecific`).
30    DenyNotAllowed {
31        /// The entry.
32        entry: usize,
33    },
34    /// Entry `entry` names a principal its mode does not take: `Mask` outside `Posix`, a
35    /// second `Mask` in force (or a second inherit-only one), `Authenticated` in `Posix`.
36    PrincipalNotAllowed {
37        /// The entry.
38        entry: usize,
39    },
40    /// A walk met an ACL that is not in `MostSpecific` mode.
41    ModeMismatch,
42    /// A walk went past [`Limits::depth`](crate::Limits::depth) levels.
43    TooDeep,
44    /// An item is its own ancestor.
45    Cycle,
46    /// The store could not answer: "could not find out", never a denial.
47    Store(ProviderError),
48}
49
50impl fmt::Display for AclError {
51    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            Self::InvalidName => formatter.write_str("a right name is empty or not printable"),
54            Self::DuplicateName => formatter.write_str("a right name is defined twice"),
55            Self::UnknownRight {
56                entry: Some(entry),
57                at,
58            } => {
59                write!(formatter, "entry {entry} names an unknown right at {at}")
60            }
61            Self::UnknownRight { entry: None, at } => {
62                write!(formatter, "unknown right at position {at}")
63            }
64            Self::TooManyRights => formatter.write_str("the schema defines too many rights"),
65            Self::TooManyEntries => formatter.write_str("the acl holds too many entries"),
66            Self::DenyNotAllowed { entry } => {
67                write!(formatter, "entry {entry} denies in a mode without denies")
68            }
69            Self::PrincipalNotAllowed { entry } => {
70                write!(
71                    formatter,
72                    "entry {entry} names a principal its mode does not take"
73                )
74            }
75            Self::ModeMismatch => formatter.write_str("a walked acl is not in most-specific mode"),
76            Self::TooDeep => formatter.write_str("the item is nested too deep"),
77            Self::Cycle => formatter.write_str("the item is its own ancestor"),
78            Self::Store(_) => formatter.write_str("the acl store could not answer"),
79        }
80    }
81}
82
83impl core::error::Error for AclError {
84    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
85        match self {
86            Self::Store(error) => Some(error),
87            _ => None,
88        }
89    }
90}
91
92impl From<ProviderError> for AclError {
93    fn from(error: ProviderError) -> Self {
94        Self::Store(error)
95    }
96}