comparison src/pam_ffi/conversation.rs @ 74:c7c596e6388f

Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT) In previous versions of Conversation, you could send messages and then return messages of the wrong type or in the wrong order or whatever. The receiver would then have to make sure that there were the right number of messages and that each message was the right type. That's annoying. This change makes the `Message` enum a two-way channel, where the asker puts their question into it, and then the answerer (the conversation) puts the answer in and returns control to the asker. The asker then only has to pull the Answer of the type they wanted out of the message.
author Paul Fisher <paul@pfish.zone>
date Fri, 06 Jun 2025 22:21:17 -0400
parents ac6881304c78
children
comparison
equal deleted inserted replaced
73:ac6881304c78 74:c7c596e6388f
73 let messages: Vec<Message> = indir 73 let messages: Vec<Message> = indir
74 .iter(count as usize) 74 .iter(count as usize)
75 .map(Message::try_from) 75 .map(Message::try_from)
76 .collect::<StdResult<_, _>>() 76 .collect::<StdResult<_, _>>()
77 .map_err(|_| ErrorCode::ConversationError)?; 77 .map_err(|_| ErrorCode::ConversationError)?;
78 let responses = conv.converse(&messages)?; 78 let responses = conv.communicate(&messages)?;
79 let owned = 79 let owned =
80 OwnedResponses::build(&responses).map_err(|_| ErrorCode::ConversationError)?; 80 OwnedResponses::build(&responses).map_err(|_| ErrorCode::ConversationError)?;
81 *response_ptr = owned.into_ptr(); 81 *response_ptr = owned.into_ptr();
82 Ok(()) 82 Ok(())
83 }; 83 };
84 ErrorCode::result_to_c(call()) 84 ErrorCode::result_to_c(call())
85 } 85 }
86 } 86 }
87 87
88 impl Conversation for LibPamConversation<'_> { 88 impl Conversation for LibPamConversation<'_> {
89 fn converse(&mut self, messages: &[Message]) -> Result<Vec<Response>> { 89 fn communicate(&mut self, messages: &[Message]) -> Result<Vec<Response>> {
90 let mut msgs_to_send = OwnedMessages::alloc(messages.len()); 90 let mut msgs_to_send = OwnedMessages::alloc(messages.len());
91 for (dst, src) in iter::zip(msgs_to_send.iter_mut(), messages.iter()) { 91 for (dst, src) in iter::zip(msgs_to_send.iter_mut(), messages.iter()) {
92 dst.set(*src).map_err(|_| ErrorCode::ConversationError)? 92 dst.set(*src).map_err(|_| ErrorCode::ConversationError)?
93 } 93 }
94 let mut response_pointer = std::ptr::null_mut(); 94 let mut response_pointer = std::ptr::null_mut();
147 // SAFETY: We're the only ones using this, 147 // SAFETY: We're the only ones using this,
148 // and we haven't freed it. 148 // and we haven't freed it.
149 text_resp.free_contents(); 149 text_resp.free_contents();
150 ret 150 ret
151 } 151 }
152 Message::Error(_) | Message::Info(_) => Response::NoResponse, 152 Message::ErrorMsg(_) | Message::InfoMsg(_) => Response::NoResponse,
153 Message::BinaryPrompt { .. } => { 153 Message::BinaryPrompt { .. } => {
154 let bin_resp = unsafe { RawBinaryResponse::upcast(received) }; 154 let bin_resp = unsafe { RawBinaryResponse::upcast(received) };
155 let ret = Response::Binary(bin_resp.to_owned()); 155 let ret = Response::Binary(bin_resp.to_owned());
156 // SAFETY: We're the only ones using this, 156 // SAFETY: We're the only ones using this,
157 // and we haven't freed it. 157 // and we haven't freed it.