diff src/conv.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 d83623951070
children bbe84835d6db
line wrap: on
line diff
--- a/src/conv.rs	Wed May 21 23:10:09 2025 -0400
+++ b/src/conv.rs	Wed May 21 23:19:43 2025 -0400
@@ -6,12 +6,45 @@
 use libc::{c_char, c_int};
 use std::ffi::{CStr, CString};
 use std::ptr;
-
-use crate::constants::ErrorCode;
+use num_derive::FromPrimitive;
+use crate::constants::{ErrorCode, InvalidEnum};
 use crate::constants::MessageStyle;
 use crate::constants::Result;
 use crate::items::Item;
 
+/// Styles of message that are shown to the user.
+#[derive(Debug, PartialEq, FromPrimitive)]
+#[non_exhaustive] // non-exhaustive because C might give us back anything!
+pub enum MessageStyle {
+    /// Requests information from the user; will be masked when typing.
+    PromptEchoOff = 1,
+    /// Requests information from the user; will not be masked.
+    PromptEchoOn = 2,
+    /// An error message.
+    ErrorMsg = 3,
+    /// An informational message.
+    TextInfo = 4,
+    /// Yes/No/Maybe conditionals. Linux-PAM specific.
+    RadioType = 5,
+    /// For server–client non-human interaction.
+    /// NOT part of the X/Open PAM specification.
+    BinaryPrompt = 7,
+}
+
+impl TryFrom<c_int> for MessageStyle {
+    type Error = InvalidEnum<Self>;
+    fn try_from(value: c_int) -> std::result::Result<Self, Self::Error> {
+        Self::from_i32(value).ok_or(value.into())
+    }
+}
+
+impl From<MessageStyle> for c_int {
+    fn from(val: MessageStyle) -> Self {
+        val as Self
+    }
+}
+
+
 #[repr(C)]
 struct Message {
     msg_style: MessageStyle,