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(virtual_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(virtual_file_system::Error),
32    /// Failed to read the contents of a user file
33    FailedToReadUserFile(virtual_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(virtual_file_system::Error),
44    /// Failed to create the users directory
45    FailedToCreateUsersDirectory(virtual_file_system::Error),
46    /// Failed to read the groups directory from the filesystem
47    FailedToReadGroupDirectory(virtual_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(virtual_file_system::Error),
52    /// Failed to read the contents of a group file
53    FailedToReadGroupFile(virtual_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(virtual_file_system::Error),
64    /// Failed to create the groups directory
65    FailedToCreateGroupsDirectory(virtual_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(virtual_file_system::Error),
70    /// Failed to read random data from the random device
71    FailedToReadRandomDevice(virtual_file_system::Error),
72    /// Failed to get user identifier from the Users manager
73    FailedToGetUserIdentifier(users::Error),
74    /// Failed to close a file
75    FailedToCloseFile(virtual_file_system::Error),
76    /// Failed to hash
77    FailedToHashPassword(virtual_file_system::Error),
78}
79
80impl Display for Error {
81    fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
82        match self {
83            Self::FailedToGetCurrentTaskIdentifier(error) => {
84                write!(formatter, "Failed to get current task identifier: {error}")
85            }
86            Self::FailedToReadUsersDirectory(error) => {
87                write!(formatter, "Failed to read users directory: {error}")
88            }
89            Self::FailedToGetUserFilePath => {
90                write!(formatter, "Failed to get user file path")
91            }
92            Self::FailedToReadUserFile(error) => {
93                write!(formatter, "Failed to read user file: {error}")
94            }
95            Self::FailedToOpenUserFile(error) => {
96                write!(formatter, "Failed to open user file: {error}")
97            }
98            Self::FailedToParseUserFile(error) => {
99                write!(formatter, "Failed to parse user file: {error}")
100            }
101            Self::FailedToAddUser(error) => {
102                write!(formatter, "Failed to add user: {error}")
103            }
104            Self::FailedToCreateUsersDirectory(error) => {
105                write!(formatter, "Failed to create users directory: {error}")
106            }
107            Self::FailedToReadGroupDirectory(error) => {
108                write!(formatter, "Failed to read group directory: {error}")
109            }
110            Self::FailedToGetGroupFilePath => {
111                write!(formatter, "Failed to get group file path")
112            }
113            Self::FailedToOpenGroupFile(error) => {
114                write!(formatter, "Failed to open group file: {error}")
115            }
116            Self::FailedToReadGroupFile(error) => {
117                write!(formatter, "Failed to read group file: {error}")
118            }
119            Self::FailedToParseGroupFile(error) => {
120                write!(formatter, "Failed to parse group file: {error}")
121            }
122            Self::FailedToAddGroup(error) => {
123                write!(formatter, "Failed to add group: {error}")
124            }
125            Self::FailedToCreateGroupsDirectory(error) => {
126                write!(formatter, "Failed to create group directory: {error}")
127            }
128            Self::InvalidPassword => {
129                write!(formatter, "Invalid password")
130            }
131            Self::FailedToOpenRandomDevice(error) => {
132                write!(formatter, "Failed to open random device: {error}")
133            }
134            Self::FailedToReadRandomDevice(error) => {
135                write!(formatter, "Failed to read random device: {error}")
136            }
137            Self::FailedToCreateUser(error) => {
138                write!(formatter, "Failed to create user: {error}")
139            }
140            Self::FailedToGetNewUserIdentifier(error) => {
141                write!(formatter, "Failed to get new user identifier: {error}")
142            }
143            Self::FailedToWriteUserFile(error) => {
144                write!(formatter, "Failed to write user file: {error}")
145            }
146            Self::FailedToGetNewGroupIdentifier(error) => {
147                write!(formatter, "Failed to get new groupe identifier: {error}")
148            }
149            Self::FailedToCreateGroup(error) => {
150                write!(formatter, "Failed to create group: {error}")
151            }
152            Self::FailedToWriteGroupFile(error) => {
153                write!(
154                    formatter,
155                    "Failed to writeerror
156                 group file: {error}"
157                )
158            }
159            Self::FailedToGetUserIdentifier(error) => {
160                write!(formatter, "Failed to get user identifier: {error}")
161            }
162            Self::FailedToCloseFile(error) => {
163                write!(formatter, "Failed to close file: {error}")
164            }
165            Self::FailedToHashPassword(error) => {
166                write!(formatter, "Failed to hash password: {error}")
167            }
168        }
169    }
170}
171
172impl From<task::Error> for Error {
173    fn from(error: task::Error) -> Self {
174        Self::FailedToGetCurrentTaskIdentifier(error)
175    }
176}