Mercurial > crates > nonstick
comparison src/constants.rs @ 185:fb8b547b36b7
Banish al(most al)l use of `i32` in favor of `c_int`.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Thu, 31 Jul 2025 14:45:38 -0400 |
parents | a1bb1d013567 |
children | 5e4ea9650f87 |
comparison
equal
deleted
inserted
replaced
184:42f747774d94 | 185:fb8b547b36b7 |
---|---|
1 //! Constants and enum values from the PAM library. | 1 //! Constants and enum values from the PAM library. |
2 | 2 |
3 use crate::_doc::{linklist, man7, manbsd, mansun, xsso}; | 3 use crate::_doc::{linklist, man7, manbsd, mansun, xsso}; |
4 use bitflags::bitflags; | 4 use bitflags::bitflags; |
5 use std::error::Error; | 5 use std::error::Error; |
6 use std::ffi::c_int; | |
6 use std::fmt; | 7 use std::fmt; |
7 use std::result::Result as StdResult; | 8 use std::result::Result as StdResult; |
8 | 9 |
9 macro_rules! wrapper { | 10 macro_rules! wrapper { |
10 ( | 11 ( |
12 $viz:vis $name:ident($wraps:ty); | 13 $viz:vis $name:ident($wraps:ty); |
13 ) => { | 14 ) => { |
14 $(#[$m])* | 15 $(#[$m])* |
15 #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 16 #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
16 #[repr(transparent)] | 17 #[repr(transparent)] |
17 $viz struct $name(i32); | 18 $viz struct $name($wraps); |
18 | 19 |
19 impl From<i32> for $name { | 20 impl From<$wraps> for $name { |
20 fn from(value: i32) -> Self { | 21 fn from(value: $wraps) -> Self { |
21 Self(value) | 22 Self(value) |
22 } | 23 } |
23 } | 24 } |
24 impl From<$name> for i32 { | 25 impl From<$name> for $wraps { |
25 fn from(value: $name) -> Self { | 26 fn from(value: $name) -> Self { |
26 value.0 | 27 value.0 |
27 } | 28 } |
28 } | 29 } |
29 } | 30 } |
30 } | 31 } |
31 | 32 |
32 wrapper! { | 33 wrapper! { |
33 /// Type of the flags that PAM passes to us (or that we pass to PAM). | 34 /// Type of the flags that PAM passes to us (or that we pass to PAM). |
34 pub RawFlags(i32); | 35 pub RawFlags(c_int); |
35 } | 36 } |
36 wrapper! { | 37 wrapper! { |
37 /// The error code that we return to PAM. | 38 /// The error code that we return to PAM. |
38 pub ReturnCode(i32); | 39 pub ReturnCode(c_int); |
39 } | 40 } |
40 | 41 |
41 impl ReturnCode { | 42 impl ReturnCode { |
42 /// A successful return. | 43 /// A successful return. |
43 pub const SUCCESS: Self = Self(0); | 44 pub const SUCCESS: Self = Self(0); |
66 | 67 |
67 #[cfg(feature = "link")] | 68 #[cfg(feature = "link")] |
68 impl From<RawFlags> for $name { | 69 impl From<RawFlags> for $name { |
69 #[allow(unused_doc_comments)] | 70 #[allow(unused_doc_comments)] |
70 fn from(value: RawFlags) -> Self { | 71 fn from(value: RawFlags) -> Self { |
71 let value: i32 = value.into(); | 72 let value: c_int = value.into(); |
72 let result = Self::empty(); | 73 let result = Self::empty(); |
73 $( | 74 $( |
74 $(#[$m_ident $($m_arg)*])* | 75 $(#[$m_ident $($m_arg)*])* |
75 let result = result | if value & $value_value == 0 { | 76 let result = result | if value & $value_value == 0 { |
76 Self::empty() | 77 Self::empty() |
182 } | 183 } |
183 } | 184 } |
184 | 185 |
185 #[cfg(feature = "link")] | 186 #[cfg(feature = "link")] |
186 impl $name { | 187 impl $name { |
187 const ALL_VALUES: i32 = 0 $( | $item_value)*; | 188 const ALL_VALUES: c_int = 0 $( | $item_value)*; |
188 | 189 |
189 fn split(value: RawFlags) -> Result<(Option<Self>, RawFlags)> { | 190 fn split(value: RawFlags) -> Result<(Option<Self>, RawFlags)> { |
190 let me = value.0 & Self::ALL_VALUES; | 191 let me = value.0 & Self::ALL_VALUES; |
191 let them = (value.0 & !Self::ALL_VALUES).into(); | 192 let them = (value.0 & !Self::ALL_VALUES).into(); |
192 let me = match RawFlags(me) { | 193 let me = match RawFlags(me) { |
377 impl fmt::Display for ErrorCode { | 378 impl fmt::Display for ErrorCode { |
378 #[cfg(any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun"))] | 379 #[cfg(any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun"))] |
379 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 380 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
380 use std::ffi::CStr; | 381 use std::ffi::CStr; |
381 use std::ptr; | 382 use std::ptr; |
383 let retcode: ReturnCode = (*self).into(); | |
382 // SAFETY: PAM impls don't care about the PAM handle and always return | 384 // SAFETY: PAM impls don't care about the PAM handle and always return |
383 // static strings. | 385 // static strings. |
384 let got = unsafe { libpam_sys::pam_strerror(ptr::null(), *self as i32) }; | 386 let got = unsafe { libpam_sys::pam_strerror(ptr::null(), retcode.into()) }; |
385 if got.is_null() { | 387 if got.is_null() { |
386 // This shouldn't happen. | 388 // This shouldn't happen. |
387 write!(f, "PAM error: {self:?} ({:?})", *self as i32) | 389 write!(f, "PAM error: {self:?} ({:?})", retcode) |
388 } else { | 390 } else { |
389 // SAFETY: We just got this back from PAM and we checked if it's null. | 391 // SAFETY: We just got this back from PAM and we checked if it's null. |
390 f.write_str(&unsafe { CStr::from_ptr(got) }.to_string_lossy()) | 392 f.write_str(&unsafe { CStr::from_ptr(got) }.to_string_lossy()) |
391 } | 393 } |
392 } | 394 } |
412 pub const BAD_CONST: ErrorCode = ErrorCode::BadConstant; | 414 pub const BAD_CONST: ErrorCode = ErrorCode::BadConstant; |
413 /// Returned when an invalid constant is used. | 415 /// Returned when an invalid constant is used. |
414 #[cfg(not(feature = "openpam-ext"))] | 416 #[cfg(not(feature = "openpam-ext"))] |
415 pub const BAD_CONST: ErrorCode = ErrorCode::SystemError; | 417 pub const BAD_CONST: ErrorCode = ErrorCode::SystemError; |
416 | 418 |
417 pub(crate) fn result_from(ret: i32) -> Result<()> { | 419 pub(crate) fn result_from(ret: c_int) -> Result<()> { |
418 match ret { | 420 match ret { |
419 0 => Ok(()), | 421 0 => Ok(()), |
420 value => Err(ReturnCode(value).try_into().unwrap_or(Self::BAD_CONST)), | 422 value => Err(ReturnCode(value).try_into().unwrap_or(Self::BAD_CONST)), |
421 } | 423 } |
422 } | 424 } |