From dfd727277326d2a6414895f9d91e0afba725cb1e Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 22 Sep 2024 15:10:31 +0800 Subject: [PATCH] review-needed: modify the alert modal prompt loop The original loop takes advantage of the copy-trait that is no longer available. I think the intent of the loop was to mutate a field within the copy through each iteration so you can update the prompt to handle field validations that failed without re-allocating the ManagedPromtWithTextResponse structure. This has been rewritten to alloc that each iteration to avoid the borrow error, and instead the `prompt` itself is a string that's initialized with the initial prompt message and is updated on each iteration if the form validation fails. @gsora git seems to say you wrote this code, maybe I could use your help to review this patch. --- services/modals/src/lib.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 50864ea82..1195b9df1 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -17,7 +17,7 @@ use num_traits::*; use xous::{send_message, Message, CID}; use xous_ipc::Buffer; -pub type TextValidationFn = fn(TextEntryPayload) -> Option; +pub type TextValidationFn = fn(&TextEntryPayload) -> Option; pub struct AlertModalBuilder<'a> { prompt: String, @@ -88,7 +88,7 @@ impl<'a> AlertModalBuilder<'a> { for (index, placeholder) in self.placeholders.iter().enumerate() { if let Some((string, persist)) = placeholder { - pl[index] = Some((String::from(&string), *persist)) + pl[index] = Some((string.to_owned(), *persist)) } else { pl[index] = None } @@ -101,16 +101,16 @@ impl<'a> AlertModalBuilder<'a> { // _ => panic!("somehow len of placeholders was neither zero or >= 1...?"), } - let mut spec = ManagedPromptWithTextResponse { - token: self.modals.token, - prompt: String::from(&self.prompt), - fields: fields_amt as u32, - placeholders: final_placeholders, - growable: self.growable, - }; - + let mut prompt = self.prompt.to_owned(); // question: do we want to add a retry limit? loop { + let spec = ManagedPromptWithTextResponse { + token: self.modals.token, + prompt: String::from(&prompt), + fields: fields_amt as u32, + placeholders: final_placeholders.clone(), + growable: self.growable, + }; let mut buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; buf.lend_mut(self.modals.conn, Opcode::PromptWithTextResponse.to_u32().unwrap()) .or(Err(xous::Error::InternalError))?; @@ -119,9 +119,9 @@ impl<'a> AlertModalBuilder<'a> { let mut form_validation_failed = false; for (index, validator) in self.validators.iter().enumerate() { if let Some(validator) = validator { - if let Some(err_msg) = validator(response.content()[index]) { - spec.prompt.clear(); - spec.prompt.append(err_msg.as_str().unwrap_or("UTF-8 error")).ok(); + if let Some(err_msg) = validator(&response.content()[index]) { + prompt.clear(); + prompt.push_str(&err_msg); form_validation_failed = true; break; // one of the validator failed } @@ -232,11 +232,7 @@ impl Modals { Some(text) => Some(String::from(text)), None => None, }; - let spec = ManagedNotification { - token: self.token, - message: String::from(notification), - qrtext, - }; + let spec = ManagedNotification { token: self.token, message: String::from(notification), qrtext }; let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; buf.lend(self.conn, Opcode::Notification.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; self.unlock(); @@ -643,8 +639,7 @@ impl Modals { /// ``` pub fn get_radiobutton(&self, prompt: &str) -> Result { self.lock(); - let spec = - ManagedPromptWithFixedResponse { token: self.token, prompt: String::from(prompt) }; + let spec = ManagedPromptWithFixedResponse { token: self.token, prompt: String::from(prompt) }; let mut buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; buf.lend_mut(self.conn, Opcode::PromptWithFixedResponse.to_u32().unwrap()) .or(Err(xous::Error::InternalError))?; @@ -729,8 +724,7 @@ impl Modals { /// ``` pub fn get_checkbox(&self, prompt: &str) -> Result, xous::Error> { self.lock(); - let spec = - ManagedPromptWithFixedResponse { token: self.token, prompt: String::from(prompt) }; + let spec = ManagedPromptWithFixedResponse { token: self.token, prompt: String::from(prompt) }; let mut buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; buf.lend_mut(self.conn, Opcode::PromptWithMultiResponse.to_u32().unwrap()) .or(Err(xous::Error::InternalError))?;