diff src/libpam/conversation.rs @ 78:002adfb98c5c

Rename files, reorder structs, remove annoying BorrowedBinaryData type. This is basically a cleanup change. Also it adds tests. - Renames the files with Questions and Answers to question and answer. - Reorders the structs in those files to put the important ones first. - Removes the BorrowedBinaryData type. It was a bad idea all along. Instead, we just use (&[u8], u8). - Adds some tests because I just can't help myself.
author Paul Fisher <paul@pfish.zone>
date Sun, 08 Jun 2025 03:48:40 -0400
parents 351bdc13005e
children 5aa1a010f1e8
line wrap: on
line diff
--- a/src/libpam/conversation.rs	Sun Jun 08 01:03:46 2025 -0400
+++ b/src/libpam/conversation.rs	Sun Jun 08 03:48:40 2025 -0400
@@ -1,10 +1,9 @@
 use crate::conv::{
-    BinaryQAndA, Conversation, ErrorMsg, InfoMsg, MaskedQAndA, Message, QAndA,
-    RadioQAndA,
+    BinaryQAndA, Conversation, ErrorMsg, InfoMsg, MaskedQAndA, Message, QAndA, RadioQAndA,
 };
+use crate::libpam::answer::{Answer, Answers, BinaryAnswer, TextAnswer};
 use crate::libpam::memory::Immovable;
-use crate::libpam::message::{Indirect, Questions};
-use crate::libpam::response::{Answer, Answers, BinaryAnswer, TextAnswer};
+use crate::libpam::question::{Indirect, Questions};
 use crate::ErrorCode;
 use crate::Result;
 use std::ffi::c_int;
@@ -76,7 +75,7 @@
             // Build our owned list of Q&As from the questions we've been asked
             let messages: Vec<OwnedMessage> = indirect
                 .iter(count as usize)
-                .map(OwnedMessage::try_from)
+                .map(TryInto::try_into)
                 .collect::<Result<_>>()
                 .map_err(|_| ErrorCode::ConversationError)?;
             // Borrow all those Q&As and ask them
@@ -95,16 +94,13 @@
 impl Conversation for LibPamConversation<'_> {
     fn communicate(&mut self, messages: &[Message]) {
         let internal = || {
-            let mut msgs_to_send = Questions::alloc(messages.len());
-            for (dst, src) in iter::zip(msgs_to_send.iter_mut(), messages.iter()) {
-                dst.fill(src).map_err(|_| ErrorCode::ConversationError)?
-            }
+            let questions = Questions::new(messages)?;
             let mut response_pointer = std::ptr::null_mut();
             // SAFETY: We're calling into PAM with valid everything.
             let result = unsafe {
                 (self.callback)(
                     messages.len() as c_int,
-                    msgs_to_send.indirect(),
+                    questions.indirect(),
                     &mut response_pointer,
                     self.appdata,
                 )
@@ -128,6 +124,7 @@
 }
 
 /// Like [`Message`], but this time we own the contents.
+#[derive(Debug)]
 pub enum OwnedMessage<'a> {
     MaskedPrompt(MaskedQAndA<'a>),
     Prompt(QAndA<'a>),
@@ -157,9 +154,11 @@
 /// You are responsible for ensuring that the src-dst pair matches.
 unsafe fn convert(msg: &Message, resp: &mut Answer) {
     macro_rules! fill_text {
-    ($dst:ident, $src:ident) => {{let text_resp = unsafe {TextAnswer::upcast($src)};
-    $dst.set_answer(text_resp.contents().map(Into::into));}}
-}
+        ($dst:ident, $src:ident) => {{
+            let text_resp = unsafe { TextAnswer::upcast($src) };
+            $dst.set_answer(text_resp.contents().map(Into::into));
+        }};
+    }
     match *msg {
         Message::MaskedPrompt(qa) => fill_text!(qa, resp),
         Message::Prompt(qa) => fill_text!(qa, resp),