comparison src/constants.rs @ 63:a7aa5ca0d00d

Move MessageStyle to conv, the only place it is used.
author Paul Fisher <paul@pfish.zone>
date Wed, 21 May 2025 23:19:43 -0400
parents 05cc2c27334f
children bbe84835d6db
comparison
equal deleted inserted replaced
62:d83623951070 63:a7aa5ca0d00d
30 const REFRESH_CRED = 0x0010; 30 const REFRESH_CRED = 0x0010;
31 /// The password service should only update those passwords 31 /// The password service should only update those passwords
32 /// that have aged. If this flag is _not_ passed, 32 /// that have aged. If this flag is _not_ passed,
33 /// the password service should update all passwords. 33 /// the password service should update all passwords.
34 const CHANGE_EXPIRED_AUTHTOK = 0x0020; 34 const CHANGE_EXPIRED_AUTHTOK = 0x0020;
35 }
36 }
37
38 /// Styles of message that are shown to the user.
39 #[derive(Debug, PartialEq, FromPrimitive)]
40 #[non_exhaustive] // non-exhaustive because C might give us back anything!
41 pub enum MessageStyle {
42 /// Requests information from the user; will be masked when typing.
43 PromptEchoOff = 1,
44 /// Requests information from the user; will not be masked.
45 PromptEchoOn = 2,
46 /// An error message.
47 ErrorMsg = 3,
48 /// An informational message.
49 TextInfo = 4,
50 /// Yes/No/Maybe conditionals. Linux-PAM specific.
51 RadioType = 5,
52 /// For server–client non-human interaction.
53 /// NOT part of the X/Open PAM specification.
54 BinaryPrompt = 7,
55 }
56
57 impl TryFrom<c_int> for MessageStyle {
58 type Error = InvalidEnum<Self>;
59 fn try_from(value: c_int) -> std::result::Result<Self, Self::Error> {
60 Self::from_i32(value).ok_or(value.into())
61 }
62 }
63
64 impl From<MessageStyle> for c_int {
65 fn from(val: MessageStyle) -> Self {
66 val as Self
67 } 35 }
68 } 36 }
69 37
70 /// The Linux-PAM error return values. Success is an Ok [Result]. 38 /// The Linux-PAM error return values. Success is an Ok [Result].
71 /// 39 ///
198 mod tests { 166 mod tests {
199 use super::*; 167 use super::*;
200 168
201 #[test] 169 #[test]
202 fn test_enums() { 170 fn test_enums() {
203 assert_eq!(Ok(MessageStyle::ErrorMsg), 3.try_into()); 171 assert_eq!(Ok(ErrorCode::ServiceError), 3.try_into());
204 assert_eq!(Err(InvalidEnum::from(999)), ErrorCode::try_from(999)); 172 assert_eq!(Err(InvalidEnum::from(999)), ErrorCode::try_from(999));
205 assert_eq!(Ok(()), ErrorCode::result_from(0)); 173 assert_eq!(Ok(()), ErrorCode::result_from(0));
206 assert_eq!(Err(ErrorCode::Abort), ErrorCode::result_from(26)); 174 assert_eq!(Err(ErrorCode::Abort), ErrorCode::result_from(26));
207 assert_eq!(Err(ErrorCode::SystemError), ErrorCode::result_from(423)); 175 assert_eq!(Err(ErrorCode::SystemError), ErrorCode::result_from(423));
208 assert!(InvalidEnum::<MessageStyle>(33, PhantomData) 176 assert!(InvalidEnum::<ErrorCode>(33, PhantomData)
209 .to_string() 177 .to_string()
210 .starts_with("33 is not a valid ")); 178 .starts_with("33 is not a valid "));
211 } 179 }
212 } 180 }