authentication/
error.rs

1//! Error handling for the Authentication module.
2//!
3//! This module defines all possible errors that can occur during authentication operations,
4//! including user and group management, file I/O, and password validation.
5
6use core::fmt::Display;
7
8/// Result type alias for authentication operations.
9///
10/// This is a convenience type that wraps `Result<T, Error>` for all
11/// authentication-related operations.
12pub type Result<T> = core::result::Result<T, Error>;
13
14/// Comprehensive error enumeration for authentication operations.
15///
16/// This enum covers all possible error conditions that can occur during:
17/// - User authentication and management
18/// - Group management
19/// - File system operations
20/// - Password hashing and validation
21/// - Random salt generation
22#[derive(Debug, Clone)]
23pub enum Error {
24    /// Failed to get the current task identifier
25    FailedToGetCurrentTaskIdentifier(task::Error),
26    /// Failed to read the users directory from the filesystem
27    FailedToReadUsersDirectory(file_system::Error),
28    /// Failed to construct a valid user file path
29    FailedToGetUserFilePath,
30    /// Failed to open a user file for reading or writing
31    FailedToOpenUserFile(file_system::Error),
32    /// Failed to read the contents of a user file
33    FailedToReadUserFile(file_system::Error),
34    /// Failed to parse JSON content from a user file
35    FailedToParseUserFile(miniserde::Error),
36    /// Failed to add a user to the Users manager
37    FailedToAddUser(users::Error),
38    /// Failed to generate a new unique user identifier
39    FailedToGetNewUserIdentifier(users::Error),
40    /// Failed to create a new user account
41    FailedToCreateUser(users::Error),
42    /// Failed to write user data to a file
43    FailedToWriteUserFile(file_system::Error),
44    /// Failed to create the users directory
45    FailedToCreateUsersDirectory(file_system::Error),
46    /// Failed to read the groups directory from the filesystem
47    FailedToReadGroupDirectory(file_system::Error),
48    /// Failed to construct a valid group file path
49    FailedToGetGroupFilePath,
50    /// Failed to open a group file for reading or writing
51    FailedToOpenGroupFile(file_system::Error),
52    /// Failed to read the contents of a group file
53    FailedToReadGroupFile(file_system::Error),
54    /// Failed to parse JSON content from a group file
55    FailedToParseGroupFile(miniserde::Error),
56    /// Failed to add a group to the Users manager
57    FailedToAddGroup(users::Error),
58    /// Failed to generate a new unique group identifier
59    FailedToGetNewGroupIdentifier(users::Error),
60    /// Failed to create a new group
61    FailedToCreateGroup(users::Error),
62    /// Failed to write group data to a file
63    FailedToWriteGroupFile(file_system::Error),
64    /// Failed to create the groups directory
65    FailedToCreateGroupsDirectory(file_system::Error),
66    /// The provided password is invalid or incorrect
67    InvalidPassword,
68    /// Failed to open the random device for salt generation
69    FailedToOpenRandomDevice(file_system::Error),
70    /// Failed to read random data from the random device
71    FailedToReadRandomDevice(file_system::Error),
72    /// Failed to get user identifier from the Users manager
73    FailedToGetUserIdentifier(users::Error),
74}
75
76impl Display for Error {
77    fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
78        match self {
79            Self::FailedToGetCurrentTaskIdentifier(error) => {
80                write!(formatter, "Failed to get current task identifier: {error}")
81            }
82            Self::FailedToReadUsersDirectory(error) => {
83                write!(formatter, "Failed to read users directory: {error}")
84            }
85            Self::FailedToGetUserFilePath => {
86                write!(formatter, "Failed to get user file path")
87            }
88            Self::FailedToReadUserFile(error) => {
89                write!(formatter, "Failed to read user file: {error}")
90            }
91            Self::FailedToOpenUserFile(error) => {
92                write!(formatter, "Failed to open user file: {error}")
93            }
94            Self::FailedToParseUserFile(error) => {
95                write!(formatter, "Failed to parse user file: {error}")
96            }
97            Self::FailedToAddUser(error) => {
98                write!(formatter, "Failed to add user: {error}")
99            }
100            Self::FailedToCreateUsersDirectory(error) => {
101                write!(formatter, "Failed to create users directory: {error}")
102            }
103            Self::FailedToReadGroupDirectory(error) => {
104                write!(formatter, "Failed to read group directory: {error}")
105            }
106            Self::FailedToGetGroupFilePath => {
107                write!(formatter, "Failed to get group file path")
108            }
109            Self::FailedToOpenGroupFile(error) => {
110                write!(formatter, "Failed to open group file: {error}")
111            }
112            Self::FailedToReadGroupFile(error) => {
113                write!(formatter, "Failed to read group file: {error}")
114            }
115            Self::FailedToParseGroupFile(error) => {
116                write!(formatter, "Failed to parse group file: {error}")
117            }
118            Self::FailedToAddGroup(error) => {
119                write!(formatter, "Failed to add group: {error}")
120            }
121            Self::FailedToCreateGroupsDirectory(error) => {
122                write!(formatter, "Failed to create group directory: {error}")
123            }
124            Self::InvalidPassword => {
125                write!(formatter, "Invalid password")
126            }
127            Self::FailedToOpenRandomDevice(error) => {
128                write!(formatter, "Failed to open random device: {error}")
129            }
130            Self::FailedToReadRandomDevice(error) => {
131                write!(formatter, "Failed to read random device: {error}")
132            }
133            Self::FailedToCreateUser(error) => {
134                write!(formatter, "Failed to create user: {error}")
135            }
136            Self::FailedToGetNewUserIdentifier(error) => {
137                write!(formatter, "Failed to get new user identifier: {error}")
138            }
139            Self::FailedToWriteUserFile(error) => {
140                write!(formatter, "Failed to write user file: {error}")
141            }
142            Self::FailedToGetNewGroupIdentifier(error) => {
143                write!(formatter, "Failed to get new groupe identifier: {error}")
144            }
145            Self::FailedToCreateGroup(error) => {
146                write!(formatter, "Failed to create group: {error}")
147            }
148            Self::FailedToWriteGroupFile(error) => {
149                write!(
150                    formatter,
151                    "Failed to writeerror
152                 group file: {error}"
153                )
154            }
155            Self::FailedToGetUserIdentifier(error) => {
156                write!(formatter, "Failed to get user identifier: {error}")
157            }
158        }
159    }
160}
161
162impl From<task::Error> for Error {
163    fn from(error: task::Error) -> Self {
164        Self::FailedToGetCurrentTaskIdentifier(error)
165    }
166}