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 is_password(&self) -> bool { false }
/// 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) {}
}

View file

@ -132,7 +132,7 @@ impl ActionApi for CheckBoxes {
DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1))
).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);
match k {
'←' | '→' => {

View file

@ -24,7 +24,7 @@ impl ActionApi for ConsoleInput {
fn redraw(&self, _at_height: i16, _modal: &Modal) {
// 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);
match k {
'\u{0}' => {

View file

@ -88,7 +88,7 @@ impl ActionApi for Notification {
).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);
match k {
'\u{0}' => {

View file

@ -146,7 +146,7 @@ impl ActionApi for RadioButtons {
DrawStyle::new(color, color, 1))
).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);
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");
}
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);
if !self.is_progressbar {
match k {

View file

@ -7,6 +7,8 @@ use num_traits::*;
use core::fmt::Write;
pub type ValidatorErr = xous_ipc::String::<256>;
#[derive(Debug, Copy, Clone, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum TextEntryVisibility {
/// text is fully visible
@ -25,7 +27,7 @@ pub struct TextEntry {
pub action_payload: TextEntryPayload,
// 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
pub validator: Option<fn(TextEntryPayload, u32) -> Option<xous_ipc::String::<512>> >,
pub validator: Option<fn(TextEntryPayload, u32) -> Option<ValidatorErr> >,
}
impl ActionApi for TextEntry {
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))
).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);
match k {
'←' => {

View file

@ -3,7 +3,7 @@
pub mod api;
use api::*;
use gam::TextEntryPayload;
use gam::*;
use xous::{CID, SID, send_message, Message};
use num_traits::{ToPrimitive, FromPrimitive};
use xous_ipc::Buffer;
@ -23,7 +23,7 @@ impl Modals {
pub fn get_text_input(&self,
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>,
) -> Result<TextEntryPayload, xous::Error> {
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 input_int = match text_str.parse::<u32>() {
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);
match FromPrimitive::from_u32(opcode) {
Some(Opcode::UxMonth) => {
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) => {
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) => {
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) => {
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) => {
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) => {
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)))
}
}
_ => {