Mercurial > crates > nonstick
comparison src/constants.rs @ 71:58f9d2a4df38
Reorganize everything again???
- Splits ffi/memory stuff into a bunch of stuff in the pam_ffi module.
- Builds infrastructure for passing Messages and Responses.
- Adds tests for some things at least.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Tue, 03 Jun 2025 21:54:58 -0400 |
parents | 9f8381a1c09c |
children | 351bdc13005e |
comparison
equal
deleted
inserted
replaced
70:9f8381a1c09c | 71:58f9d2a4df38 |
---|---|
4 use libc::{c_int, c_uint}; | 4 use libc::{c_int, c_uint}; |
5 use num_derive::FromPrimitive; | 5 use num_derive::FromPrimitive; |
6 use num_traits::FromPrimitive; | 6 use num_traits::FromPrimitive; |
7 use std::any; | 7 use std::any; |
8 use std::marker::PhantomData; | 8 use std::marker::PhantomData; |
9 use std::result::Result as StdResult; | |
9 | 10 |
10 bitflags! { | 11 bitflags! { |
11 /// The available PAM flags. | 12 /// The available PAM flags. |
12 /// | 13 /// |
13 /// See `/usr/include/security/_pam_types.h` and | 14 /// See `/usr/include/security/_pam_types.h` and |
135 #[error("call this function again to complete authentication stack")] | 136 #[error("call this function again to complete authentication stack")] |
136 Incomplete = 31, | 137 Incomplete = 31, |
137 } | 138 } |
138 | 139 |
139 /// A PAM-specific Result type with an [ErrorCode] error. | 140 /// A PAM-specific Result type with an [ErrorCode] error. |
140 pub type Result<T> = std::result::Result<T, ErrorCode>; | 141 pub type Result<T> = StdResult<T, ErrorCode>; |
141 | 142 |
142 impl ErrorCode { | 143 impl ErrorCode { |
143 /// Converts this [Result] into a C-compatible result code. | 144 /// Converts this [Result] into a C-compatible result code. |
144 pub fn result_to_c<T>(value: Result<T>) -> c_int { | 145 pub fn result_to_c<T>(value: Result<T>) -> c_int { |
145 match value { | 146 match value { |
159 } | 160 } |
160 | 161 |
161 impl TryFrom<c_int> for ErrorCode { | 162 impl TryFrom<c_int> for ErrorCode { |
162 type Error = InvalidEnum<Self>; | 163 type Error = InvalidEnum<Self>; |
163 | 164 |
164 fn try_from(value: c_int) -> std::result::Result<Self, Self::Error> { | 165 fn try_from(value: c_int) -> StdResult<Self, Self::Error> { |
165 Self::from_i32(value).ok_or(value.into()) | 166 Self::from_i32(value).ok_or(value.into()) |
166 } | 167 } |
167 } | 168 } |
168 | 169 |
169 impl From<ErrorCode> for c_int { | 170 impl From<ErrorCode> for c_int { |
191 } | 192 } |
192 | 193 |
193 /// Returned when text that should not have any `\0` bytes in it does. | 194 /// Returned when text that should not have any `\0` bytes in it does. |
194 /// Analogous to [`std::ffi::NulError`], but the data it was created from | 195 /// Analogous to [`std::ffi::NulError`], but the data it was created from |
195 /// is borrowed. | 196 /// is borrowed. |
196 #[derive(Debug, thiserror::Error)] | |
197 #[error("null byte within input at byte {0}")] | |
198 pub struct NulError(pub usize); | |
199 | |
200 /// Returned when trying to fit too much data into a binary message. | |
201 #[derive(Debug, thiserror::Error)] | |
202 #[error("cannot create a message of {actual} bytes; maximum is {max}")] | |
203 pub struct TooBigError { | |
204 pub actual: usize, | |
205 pub max: usize, | |
206 } | |
207 | |
208 #[cfg(test)] | 197 #[cfg(test)] |
209 mod tests { | 198 mod tests { |
210 use super::*; | 199 use super::*; |
211 | 200 |
212 #[test] | 201 #[test] |