turn the validator error message into a newtype

this allows us to fine-tune the error message static size parameter
later on more easily
This commit is contained in:
bunnie 2021-12-15 13:53:53 +08:00
parent e48c472b32
commit f3f3e677d2
9 changed files with 20 additions and 18 deletions

View file

@ -48,7 +48,7 @@ pub trait ActionApi {
fn close(&mut self) {} fn close(&mut self) {}
fn is_password(&self) -> bool { false } fn is_password(&self) -> bool { false }
/// navigation is one of '∴' | '←' | '→' | '↑' | '↓' /// navigation is one of '∴' | '←' | '→' | '↑' | '↓'
fn key_action(&mut self, _key: char) -> (Option<xous_ipc::String::<512>>, bool) {(None, true)} fn key_action(&mut self, _key: char) -> (Option<ValidatorErr>, bool) {(None, true)}
fn set_action_opcode(&mut self, _op: u32) {} fn set_action_opcode(&mut self, _op: u32) {}
} }

View file

@ -132,7 +132,7 @@ impl ActionApi for CheckBoxes {
DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1)) DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1))
).expect("couldn't draw entry line"); ).expect("couldn't draw entry line");
} }
fn key_action(&mut self, k: char) -> (Option<xous_ipc::String::<512>>, bool) { fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) {
log::trace!("key_action: {}", k); log::trace!("key_action: {}", k);
match k { match k {
'←' | '→' => { '←' | '→' => {

View file

@ -24,7 +24,7 @@ impl ActionApi for ConsoleInput {
fn redraw(&self, _at_height: i16, _modal: &Modal) { fn redraw(&self, _at_height: i16, _modal: &Modal) {
// has nothing // has nothing
} }
fn key_action(&mut self, k: char) -> (Option<xous_ipc::String::<512>>, bool) { fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) {
log::trace!("key_action: {}", k); log::trace!("key_action: {}", k);
match k { match k {
'\u{0}' => { '\u{0}' => {

View file

@ -88,7 +88,7 @@ impl ActionApi for Notification {
).expect("couldn't draw entry line"); ).expect("couldn't draw entry line");
} }
} }
fn key_action(&mut self, k: char) -> (Option<xous_ipc::String::<512>>, bool) { fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) {
log::trace!("key_action: {}", k); log::trace!("key_action: {}", k);
match k { match k {
'\u{0}' => { '\u{0}' => {

View file

@ -146,7 +146,7 @@ impl ActionApi for RadioButtons {
DrawStyle::new(color, color, 1)) DrawStyle::new(color, color, 1))
).expect("couldn't draw entry line"); ).expect("couldn't draw entry line");
} }
fn key_action(&mut self, k: char) -> (Option<xous_ipc::String::<512>>, bool) { fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) {
log::trace!("key_action: {}", k); log::trace!("key_action: {}", k);
match k { match k {
'←' | '→' => { '←' | '→' => {

View file

@ -161,7 +161,7 @@ impl ActionApi for Slider {
); );
modal.gam.draw_rectangle(modal.canvas, inner_rect).expect("couldn't draw inner rectangle"); modal.gam.draw_rectangle(modal.canvas, inner_rect).expect("couldn't draw inner rectangle");
} }
fn key_action(&mut self, k: char) -> (Option<xous_ipc::String::<512>>, bool) { fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) {
log::trace!("key_action: {}", k); log::trace!("key_action: {}", k);
if !self.is_progressbar { if !self.is_progressbar {
match k { match k {

View file

@ -7,6 +7,8 @@ use num_traits::*;
use core::fmt::Write; use core::fmt::Write;
pub type ValidatorErr = xous_ipc::String::<256>;
#[derive(Debug, Copy, Clone, num_derive::FromPrimitive, num_derive::ToPrimitive)] #[derive(Debug, Copy, Clone, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum TextEntryVisibility { pub enum TextEntryVisibility {
/// text is fully visible /// text is fully visible
@ -25,7 +27,7 @@ pub struct TextEntry {
pub action_payload: TextEntryPayload, pub action_payload: TextEntryPayload,
// validator borrows the text entry payload, and returns an error message if something didn't go well. // validator borrows the text entry payload, and returns an error message if something didn't go well.
// validator takes as ragument the current action_payload, and the current action_opcode // validator takes as ragument the current action_payload, and the current action_opcode
pub validator: Option<fn(TextEntryPayload, u32) -> Option<xous_ipc::String::<512>> >, pub validator: Option<fn(TextEntryPayload, u32) -> Option<ValidatorErr> >,
} }
impl ActionApi for TextEntry { impl ActionApi for TextEntry {
fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op}
@ -180,7 +182,7 @@ impl ActionApi for TextEntry {
DrawStyle::new(color, color, 1)) DrawStyle::new(color, color, 1))
).expect("couldn't draw entry line"); ).expect("couldn't draw entry line");
} }
fn key_action(&mut self, k: char) -> (Option<xous_ipc::String::<512>>, bool) { fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) {
log::trace!("key_action: {}", k); log::trace!("key_action: {}", k);
match k { match k {
'←' => { '←' => {

View file

@ -3,7 +3,7 @@
pub mod api; pub mod api;
use api::*; use api::*;
use gam::TextEntryPayload; use gam::*;
use xous::{CID, SID, send_message, Message}; use xous::{CID, SID, send_message, Message};
use num_traits::{ToPrimitive, FromPrimitive}; use num_traits::{ToPrimitive, FromPrimitive};
use xous_ipc::Buffer; use xous_ipc::Buffer;
@ -23,7 +23,7 @@ impl Modals {
pub fn get_text_input(&self, pub fn get_text_input(&self,
prompt: &str, prompt: &str,
maybe_validator: Option<fn(TextEntryPayload, u32) -> Option<xous_ipc::String::<256>> >, maybe_validator: Option<fn(TextEntryPayload, u32) -> Option<ValidatorErr> >,
maybe_validator_op: Option<u32>, maybe_validator_op: Option<u32>,
) -> Result<TextEntryPayload, xous::Error> { ) -> Result<TextEntryPayload, xous::Error> {
let validator = if let Some(validator) = maybe_validator { let validator = if let Some(validator) = maybe_validator {

View file

@ -522,42 +522,42 @@ mod implementation {
} }
} }
fn rtc_ux_validator(input: TextEntryPayload, opcode: u32) -> Option<xous_ipc::String::<512>> { fn rtc_ux_validator(input: TextEntryPayload, opcode: u32) -> Option<ValidatorErr> {
let text_str = input.as_str(); let text_str = input.as_str();
let input_int = match text_str.parse::<u32>() { let input_int = match text_str.parse::<u32>() {
Ok(input_int) => input_int, Ok(input_int) => input_int,
_ => return Some(xous_ipc::String::<512>::from_str(t!("rtc.integer_err", xous::LANG))), _ => return Some(ValidatorErr::from_str(t!("rtc.integer_err", xous::LANG))),
}; };
log::trace!("validating input {}, parsed as {} for opcode {}", text_str, input_int, opcode); log::trace!("validating input {}, parsed as {} for opcode {}", text_str, input_int, opcode);
match FromPrimitive::from_u32(opcode) { match FromPrimitive::from_u32(opcode) {
Some(Opcode::UxMonth) => { Some(Opcode::UxMonth) => {
if input_int < 1 || input_int > 12 { if input_int < 1 || input_int > 12 {
return Some(xous_ipc::String::<512>::from_str(t!("rtc.range_err", xous::LANG))) return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
} }
} }
Some(Opcode::UxDay) => { Some(Opcode::UxDay) => {
if input_int < 1 || input_int > 31 { if input_int < 1 || input_int > 31 {
return Some(xous_ipc::String::<512>::from_str(t!("rtc.range_err", xous::LANG))) return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
} }
} }
Some(Opcode::UxYear) => { Some(Opcode::UxYear) => {
if input_int > 99 { if input_int > 99 {
return Some(xous_ipc::String::<512>::from_str(t!("rtc.range_err", xous::LANG))) return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
} }
} }
Some(Opcode::UxHour) => { Some(Opcode::UxHour) => {
if input_int > 23 { if input_int > 23 {
return Some(xous_ipc::String::<512>::from_str(t!("rtc.range_err", xous::LANG))) return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
} }
} }
Some(Opcode::UxMinute) => { Some(Opcode::UxMinute) => {
if input_int > 59 { if input_int > 59 {
return Some(xous_ipc::String::<512>::from_str(t!("rtc.range_err", xous::LANG))) return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
} }
} }
Some(Opcode::UxSeconds) => { Some(Opcode::UxSeconds) => {
if input_int > 59 { if input_int > 59 {
return Some(xous_ipc::String::<512>::from_str(t!("rtc.range_err", xous::LANG))) return Some(ValidatorErr::from_str(t!("rtc.range_err", xous::LANG)))
} }
} }
_ => { _ => {