Mercurial > crates > nonstick
diff src/constants.rs @ 175:e30775c80b49
Separate #[cfg(feature = "link")] from other features.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 30 Jul 2025 14:57:12 -0400 |
parents | 6727cbe56f4a |
children | 0730f5f2ee2a |
line wrap: on
line diff
--- a/src/constants.rs Tue Jul 29 18:58:27 2025 -0400 +++ b/src/constants.rs Wed Jul 30 14:57:12 2025 -0400 @@ -3,9 +3,7 @@ use crate::_doc::{linklist, man7, manbsd, mansun, xsso}; use bitflags::bitflags; use std::error::Error; -use std::ffi::c_int; use std::fmt; -use std::fmt::{Display, Formatter}; use std::result::Result as StdResult; macro_rules! wrapper { @@ -33,11 +31,11 @@ wrapper! { /// Type of the flags that PAM passes to us (or that we pass to PAM). - pub RawFlags(c_int); + pub RawFlags(i32); } wrapper! { /// The error code that we return to PAM. - pub ReturnCode(c_int); + pub ReturnCode(i32); } impl ReturnCode { @@ -70,7 +68,7 @@ impl From<RawFlags> for $name { #[allow(unused_doc_comments)] fn from(value: RawFlags) -> Self { - let value: c_int = value.into(); + let value: i32 = value.into(); let result = Self::empty(); $( $(#[$m_ident $($m_arg)*])* @@ -128,7 +126,7 @@ const CHANGE_EXPIRED_AUTHTOK = (link = libpam_sys::PAM_CHANGE_EXPIRED_AUTHTOK, else = 0b10); /// Don't check if the password is any good (Sun only). - #[cfg(pam_impl = "Sun")] + #[cfg(feature = "sun-ext")] const NO_AUTHTOK_CHECK = (link = libpam_sys::PAM_NO_AUTHTOK_CHECK, else = 0b100); } } @@ -141,11 +139,6 @@ } } -#[cfg(feature = "openpam-ext")] -const BAD_CONST: ErrorCode = ErrorCode::BadConstant; -#[cfg(not(feature = "openpam-ext"))] -const BAD_CONST: ErrorCode = ErrorCode::SystemError; - macro_rules! flag_enum { ( $(#[$m:meta])* @@ -173,7 +166,7 @@ $( $item_value => Ok(Self::$item_name), )* - _ => Err(BAD_CONST), + _ => Err(ErrorCode::BAD_CONST), } } } @@ -198,7 +191,7 @@ let them = (value.0 & !Self::ALL_VALUES).into(); let me = match RawFlags(me) { RawFlags(0) => None, - other => Some(Self::try_from(other).map_err(|_| BAD_CONST)?), + other => Some(Self::try_from(other).map_err(|_| ErrorCode::BAD_CONST)?), }; Ok((me, them)) } @@ -254,7 +247,7 @@ pub(crate) fn extract(value: RawFlags) -> Result<(Self, AuthtokFlags)> { match Self::split(value)? { (Some(act), rest) => Ok((act, AuthtokFlags::from(rest))), - (None, _) => Err(BAD_CONST), + (None, _) => Err(ErrorCode::BAD_CONST), } } } @@ -288,7 +281,7 @@ $(#[$im])* $value => Ok(Self::$key), )* - _ => Err(BAD_CONST), + _ => Err(ErrorCode::BAD_CONST), } } } @@ -380,30 +373,32 @@ /// A PAM-specific Result type with an [ErrorCode] error. pub type Result<T> = StdResult<T, ErrorCode>; -impl Display for ErrorCode { - #[cfg(all( - feature = "link", - any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun") - ))] - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { +#[cfg(feature = "link")] +impl fmt::Display for ErrorCode { + #[cfg(any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun"))] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use std::ffi::CStr; use std::ptr; // SAFETY: PAM impls don't care about the PAM handle and always return // static strings. - let got = unsafe { libpam_sys::pam_strerror(ptr::null(), *self as c_int) }; + let got = unsafe { libpam_sys::pam_strerror(ptr::null(), *self as i32) }; if got.is_null() { // This shouldn't happen. - write!(f, "PAM error: {self:?} ({:?})", *self as c_int) + write!(f, "PAM error: {self:?} ({:?})", *self as i32) } else { // SAFETY: We just got this back from PAM and we checked if it's null. f.write_str(&unsafe { CStr::from_ptr(got) }.to_string_lossy()) } } - #[cfg(not(all( - feature = "link", - any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun") - )))] - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + #[cfg(not(any(pam_impl = "LinuxPam", pam_impl = "OpenPam", pam_impl = "Sun")))] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(self, f) + } +} + +#[cfg(not(feature = "link"))] +impl fmt::Display for ErrorCode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(self, f) } } @@ -412,14 +407,21 @@ #[cfg(feature = "link")] impl ErrorCode { - pub(crate) fn result_from(ret: c_int) -> Result<()> { + #[cfg(feature = "openpam-ext")] + const BAD_CONST: ErrorCode = ErrorCode::BadConstant; + #[cfg(not(feature = "openpam-ext"))] + const BAD_CONST: ErrorCode = ErrorCode::SystemError; + + + pub(crate) fn result_from(ret: i32) -> Result<()> { match ret { 0 => Ok(()), - value => Err(ReturnCode(value).try_into().unwrap_or(BAD_CONST)), + value => Err(ReturnCode(value).try_into().unwrap_or(Self::BAD_CONST)), } } } +#[cfg(feature = "link")] impl<T> From<Result<T>> for ReturnCode { fn from(value: Result<T>) -> Self { match value { @@ -444,7 +446,7 @@ Result::<()>::Err(ErrorCode::Abort), ErrorCode::result_from(libpam_sys::PAM_ABORT) ); - assert_eq!(Err(BAD_CONST), ErrorCode::result_from(423)); + assert_eq!(Err(ErrorCode::BAD_CONST), ErrorCode::result_from(423)); } #[test]