From 8e52081d8af7591ef05922e7233e0dc6aa88d40d Mon Sep 17 00:00:00 2001 From: matejcik Date: Thu, 13 Jun 2024 15:51:10 +0200 Subject: [PATCH] refactor(core/rust): unify String usage --- .../rust/src/ui/component/text/common.rs | 22 +++++++++---------- .../model_mercury/component/keyboard/bip39.rs | 5 ++--- .../component/keyboard/passphrase.rs | 4 ++-- .../model_mercury/component/keyboard/pin.rs | 13 +++++------ .../component/keyboard/slip39.rs | 18 +++++++-------- .../component/input_methods/number_input.rs | 8 +++---- .../component/input_methods/passphrase.rs | 4 ++-- .../model_tr/component/input_methods/pin.rs | 4 ++-- .../component/input_methods/wordlist.rs | 4 ++-- .../ui/model_tt/component/keyboard/bip39.rs | 5 ++--- .../model_tt/component/keyboard/passphrase.rs | 4 ++-- .../src/ui/model_tt/component/keyboard/pin.rs | 15 ++++++------- .../ui/model_tt/component/keyboard/slip39.rs | 20 ++++++++--------- 13 files changed, 58 insertions(+), 68 deletions(-) diff --git a/core/embed/rust/src/ui/component/text/common.rs b/core/embed/rust/src/ui/component/text/common.rs index 82d66e16c3..19ea2a54d8 100644 --- a/core/embed/rust/src/ui/component/text/common.rs +++ b/core/embed/rust/src/ui/component/text/common.rs @@ -1,4 +1,7 @@ -use crate::ui::{component::EventCtx, util::ResultExt}; +use crate::{ + strutil::ShortString, + ui::{component::EventCtx, util::ResultExt}, +}; use heapless::String; /// Reified editing operations of `TextBox`. @@ -13,19 +16,20 @@ pub enum TextEdit { /// Wraps a character buffer of maximum length `L` and provides text editing /// operations over it. Text ops usually take a `EventCtx` to request a paint /// pass in case of any state modification. -pub struct TextBox { - text: String, +pub struct TextBox { + text: ShortString, } -impl TextBox { +impl TextBox { /// Create a new `TextBox` with content `text`. - pub fn new(text: String) -> Self { + pub fn new(text: &str) -> Self { + let text = unwrap!(String::try_from(text)); Self { text } } /// Create an empty `TextBox`. pub fn empty() -> Self { - Self::new(String::new()) + Self::new("") } pub fn content(&self) -> &str { @@ -40,10 +44,6 @@ impl TextBox { self.text.is_empty() } - pub fn is_full(&self) -> bool { - self.text.len() == self.text.capacity() - } - /// Delete the last character of content, if any. pub fn delete_last(&mut self, ctx: &mut EventCtx) { let changed = self.text.pop().is_some(); @@ -107,7 +107,7 @@ impl TextBox { // DEBUG-ONLY SECTION BELOW #[cfg(feature = "ui_debug")] -impl crate::trace::Trace for TextBox { +impl crate::trace::Trace for TextBox { fn trace(&self, t: &mut dyn crate::trace::Tracer) { t.component("TextBox"); t.string("text", self.text.as_str().into()); diff --git a/core/embed/rust/src/ui/model_mercury/component/keyboard/bip39.rs b/core/embed/rust/src/ui/model_mercury/component/keyboard/bip39.rs index 5fb9488af2..a26604f177 100644 --- a/core/embed/rust/src/ui/model_mercury/component/keyboard/bip39.rs +++ b/core/embed/rust/src/ui/model_mercury/component/keyboard/bip39.rs @@ -18,7 +18,6 @@ use crate::{ shape::Renderer, }, }; -use heapless::String; const MAX_LENGTH: usize = 8; @@ -26,7 +25,7 @@ pub struct Bip39Input { button: Button, // used only to keep track of suggestion text color button_suggestion: Button, - textbox: TextBox, + textbox: TextBox, multi_tap: MultiTapKeyboard, options_num: Option, suggested_word: Option<&'static str>, @@ -194,7 +193,7 @@ impl Bip39Input { // Styling the input to reflect already filled word Self { button: Button::empty().styled(theme::button_recovery_confirm()), - textbox: TextBox::new(unwrap!(String::try_from(word))), + textbox: TextBox::new(word), multi_tap: MultiTapKeyboard::new(), options_num: bip39::options_num(word), suggested_word: bip39::complete_word(word), diff --git a/core/embed/rust/src/ui/model_mercury/component/keyboard/passphrase.rs b/core/embed/rust/src/ui/model_mercury/component/keyboard/passphrase.rs index 2e4966bf8d..abe8454bbc 100644 --- a/core/embed/rust/src/ui/model_mercury/component/keyboard/passphrase.rs +++ b/core/embed/rust/src/ui/model_mercury/component/keyboard/passphrase.rs @@ -236,7 +236,7 @@ impl PassphraseKeyboard { /// We should disable the input when the passphrase has reached maximum /// length and we are not cycling through the characters. fn is_button_active(&self, key: usize) -> bool { - let textbox_not_full = !self.input.inner().textbox.is_full(); + let textbox_not_full = self.input.inner().textbox.len() < MAX_LENGTH; let key_is_pending = { if let Some(pending) = self.input.inner().multi_tap.pending_key() { pending == key @@ -406,7 +406,7 @@ impl Component for PassphraseKeyboard { struct Input { area: Rect, - textbox: TextBox, + textbox: TextBox, multi_tap: MultiTapKeyboard, } diff --git a/core/embed/rust/src/ui/model_mercury/component/keyboard/pin.rs b/core/embed/rust/src/ui/model_mercury/component/keyboard/pin.rs index 8abda3993b..68c3574229 100644 --- a/core/embed/rust/src/ui/model_mercury/component/keyboard/pin.rs +++ b/core/embed/rust/src/ui/model_mercury/component/keyboard/pin.rs @@ -2,7 +2,7 @@ use core::mem; use heapless::String; use crate::{ - strutil::TString, + strutil::{ShortString, TString}, time::Duration, trezorhal::random, ui::{ @@ -14,11 +14,10 @@ use crate::{ event::TouchEvent, geometry::{Alignment, Alignment2D, Grid, Insets, Offset, Rect}, model_mercury::component::{ - button::{Button, ButtonContent, ButtonMsg, ButtonMsg::Clicked}, + button::{Button, ButtonContent, ButtonMsg::{self, Clicked}}, theme, }, - shape, - shape::Renderer, + shape::{self, Renderer}, }, }; @@ -278,7 +277,7 @@ struct PinDots { area: Rect, pad: Pad, style: TextStyle, - digits: String, + digits: ShortString, display_digits: bool, } @@ -309,7 +308,7 @@ impl PinDots { } fn is_full(&self) -> bool { - self.digits.len() == self.digits.capacity() + self.digits.len() >= MAX_LENGTH } fn clear(&mut self, ctx: &mut EventCtx) { @@ -455,7 +454,7 @@ impl crate::trace::Trace for PinKeyboard<'_> { fn trace(&self, t: &mut dyn crate::trace::Tracer) { t.component("PinKeyboard"); // So that debuglink knows the locations of the buttons - let mut digits_order: String<10> = String::new(); + let mut digits_order = ShortString::new(); for btn in self.digit_btns.iter() { let btn_content = btn.inner().content(); if let ButtonContent::Text(text) = btn_content { diff --git a/core/embed/rust/src/ui/model_mercury/component/keyboard/slip39.rs b/core/embed/rust/src/ui/model_mercury/component/keyboard/slip39.rs index 79173a1db0..60cfe425d2 100644 --- a/core/embed/rust/src/ui/model_mercury/component/keyboard/slip39.rs +++ b/core/embed/rust/src/ui/model_mercury/component/keyboard/slip39.rs @@ -3,8 +3,7 @@ use core::iter; use heapless::String; use crate::{ - trezorhal::slip39, - ui::{ + strutil::ShortString, trezorhal::slip39, ui::{ component::{ text::common::{TextBox, TextEdit}, Component, Event, EventCtx, @@ -21,17 +20,16 @@ use crate::{ }, theme, }, - shape, - shape::Renderer, + shape::{self, Renderer}, util::ResultExt, - }, + } }; const MAX_LENGTH: usize = 8; pub struct Slip39Input { button: Button, - textbox: TextBox, + textbox: TextBox, multi_tap: MultiTapKeyboard, final_word: Option<&'static str>, input_mask: Slip39Mask, @@ -137,7 +135,7 @@ impl Component for Slip39Input { // To simplify things, we always copy the printed string here, even if it // wouldn't be strictly necessary. - let mut text: String = String::new(); + let mut text = ShortString::new(); if let Some(word) = self.final_word { // We're done with input, paint the full word. text.push_str(word) @@ -222,7 +220,7 @@ impl Slip39Input { Self { // Button has the same style the whole time button: Button::empty().styled(theme::button_recovery_confirm()), - textbox: TextBox::new(buff), + textbox: TextBox::new(&buff), multi_tap: MultiTapKeyboard::new(), final_word, input_mask, @@ -231,8 +229,8 @@ impl Slip39Input { fn setup_from_prefilled_word( word: &str, - ) -> (String, Slip39Mask, Option<&'static str>) { - let mut buff: String = String::new(); + ) -> (ShortString, Slip39Mask, Option<&'static str>) { + let mut buff = ShortString::new(); // Gradually appending encoded key digits to the buffer and checking if // have not already formed a final word. diff --git a/core/embed/rust/src/ui/model_tr/component/input_methods/number_input.rs b/core/embed/rust/src/ui/model_tr/component/input_methods/number_input.rs index 9d25e5db81..b033007fbc 100644 --- a/core/embed/rust/src/ui/model_tr/component/input_methods/number_input.rs +++ b/core/embed/rust/src/ui/model_tr/component/input_methods/number_input.rs @@ -1,14 +1,12 @@ use crate::{ - translations::TR, - ui::{ + strutil::ShortString, translations::TR, ui::{ component::{Component, Event, EventCtx}, geometry::Rect, shape::Renderer, - }, + } }; use super::super::{ButtonLayout, ChoiceFactory, ChoiceItem, ChoicePage}; -use heapless::String; struct ChoiceFactoryNumberInput { min: u32, @@ -31,7 +29,7 @@ impl ChoiceFactory for ChoiceFactoryNumberInput { fn get(&self, choice_index: usize) -> (Self::Item, Self::Action) { let num = self.min + choice_index as u32; - let text: String<10> = unwrap!(String::try_from(num)); + let text = unwrap!(ShortString::try_from(num)); let mut choice_item = ChoiceItem::new( text, ButtonLayout::arrow_armed_arrow(TR::buttons__select.into()), diff --git a/core/embed/rust/src/ui/model_tr/component/input_methods/passphrase.rs b/core/embed/rust/src/ui/model_tr/component/input_methods/passphrase.rs index 668e71038e..f71d30878e 100644 --- a/core/embed/rust/src/ui/model_tr/component/input_methods/passphrase.rs +++ b/core/embed/rust/src/ui/model_tr/component/input_methods/passphrase.rs @@ -273,7 +273,7 @@ pub struct PassphraseEntry { passphrase_dots: Child, show_plain_passphrase: bool, show_last_digit: bool, - textbox: TextBox, + textbox: TextBox, current_category: ChoiceCategory, } @@ -355,7 +355,7 @@ impl PassphraseEntry { } fn is_full(&self) -> bool { - self.textbox.is_full() + self.textbox.len() >= MAX_PASSPHRASE_LENGTH } /// Randomly choose an index in the current category diff --git a/core/embed/rust/src/ui/model_tr/component/input_methods/pin.rs b/core/embed/rust/src/ui/model_tr/component/input_methods/pin.rs index 2044d9ab57..63d7ecce7e 100644 --- a/core/embed/rust/src/ui/model_tr/component/input_methods/pin.rs +++ b/core/embed/rust/src/ui/model_tr/component/input_methods/pin.rs @@ -140,7 +140,7 @@ pub struct PinEntry<'a> { showing_real_prompt: bool, show_real_pin: bool, show_last_digit: bool, - textbox: TextBox, + textbox: TextBox, } impl<'a> PinEntry<'a> { @@ -236,7 +236,7 @@ impl<'a> PinEntry<'a> { } fn is_full(&self) -> bool { - self.textbox.is_full() + self.textbox.len() >= MAX_PIN_LENGTH } fn is_empty(&self) -> bool { diff --git a/core/embed/rust/src/ui/model_tr/component/input_methods/wordlist.rs b/core/embed/rust/src/ui/model_tr/component/input_methods/wordlist.rs index 2253034404..50f13c325d 100644 --- a/core/embed/rust/src/ui/model_tr/component/input_methods/wordlist.rs +++ b/core/embed/rust/src/ui/model_tr/component/input_methods/wordlist.rs @@ -158,7 +158,7 @@ impl ChoiceFactory for ChoiceFactoryWordlist { pub struct WordlistEntry { choice_page: ChoicePage, chosen_letters: Child, - textbox: TextBox, + textbox: TextBox, offer_words: bool, wordlist_type: WordlistType, /// Whether going back is allowed (is not on the very first word). @@ -196,7 +196,7 @@ impl WordlistEntry { .with_incomplete(true) .with_initial_page_counter(1), chosen_letters: Child::new(ChangingTextLine::center_mono(word, LINE_CAPACITY)), - textbox: TextBox::new(unwrap!(String::try_from(word))), + textbox: TextBox::new(word), offer_words: false, wordlist_type, can_go_back, diff --git a/core/embed/rust/src/ui/model_tt/component/keyboard/bip39.rs b/core/embed/rust/src/ui/model_tt/component/keyboard/bip39.rs index 2f29ae6525..f3efc15515 100644 --- a/core/embed/rust/src/ui/model_tt/component/keyboard/bip39.rs +++ b/core/embed/rust/src/ui/model_tt/component/keyboard/bip39.rs @@ -18,7 +18,6 @@ use crate::{ shape::Renderer, }, }; -use heapless::String; const MAX_LENGTH: usize = 8; @@ -26,7 +25,7 @@ pub struct Bip39Input { button: Button, // used only to keep track of suggestion text color button_suggestion: Button, - textbox: TextBox, + textbox: TextBox, multi_tap: MultiTapKeyboard, options_num: Option, suggested_word: Option<&'static str>, @@ -228,7 +227,7 @@ impl Bip39Input { // Styling the input to reflect already filled word Self { button: Button::with_icon(theme::ICON_LIST_CHECK).styled(theme::button_pin_confirm()), - textbox: TextBox::new(unwrap!(String::try_from(word))), + textbox: TextBox::new(word), multi_tap: MultiTapKeyboard::new(), options_num: bip39::options_num(word), suggested_word: bip39::complete_word(word), diff --git a/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs b/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs index 9b31d9fca8..9b652fe060 100644 --- a/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs +++ b/core/embed/rust/src/ui/model_tt/component/keyboard/passphrase.rs @@ -163,7 +163,7 @@ impl PassphraseKeyboard { /// We should disable the input when the passphrase has reached maximum /// length and we are not cycling through the characters. fn is_button_active(&self, key: usize) -> bool { - let textbox_not_full = !self.input.inner().textbox.is_full(); + let textbox_not_full = self.input.inner().textbox.len() < MAX_LENGTH; let key_is_pending = { if let Some(pending) = self.input.inner().multi_tap.pending_key() { pending == key @@ -329,7 +329,7 @@ impl Component for PassphraseKeyboard { struct Input { area: Rect, - textbox: TextBox, + textbox: TextBox, multi_tap: MultiTapKeyboard, } diff --git a/core/embed/rust/src/ui/model_tt/component/keyboard/pin.rs b/core/embed/rust/src/ui/model_tt/component/keyboard/pin.rs index 930965bcbe..8d1f4f6aa3 100644 --- a/core/embed/rust/src/ui/model_tt/component/keyboard/pin.rs +++ b/core/embed/rust/src/ui/model_tt/component/keyboard/pin.rs @@ -1,8 +1,8 @@ -use core::mem; +use core::{char::MAX, mem}; use heapless::String; use crate::{ - strutil::TString, + strutil::{ShortString, TString}, time::Duration, trezorhal::random, ui::{ @@ -14,11 +14,10 @@ use crate::{ event::TouchEvent, geometry::{Alignment, Alignment2D, Grid, Insets, Offset, Rect}, model_tt::component::{ - button::{Button, ButtonContent, ButtonMsg, ButtonMsg::Clicked}, + button::{Button, ButtonContent, ButtonMsg::{self, Clicked}}, theme, }, - shape, - shape::Renderer, + shape::{self, Renderer}, }, }; @@ -304,7 +303,7 @@ struct PinDots { area: Rect, pad: Pad, style: TextStyle, - digits: String, + digits: ShortString, display_digits: bool, } @@ -335,7 +334,7 @@ impl PinDots { } fn is_full(&self) -> bool { - self.digits.len() == self.digits.capacity() + self.digits.len() >= MAX_LENGTH } fn clear(&mut self, ctx: &mut EventCtx) { @@ -552,7 +551,7 @@ impl crate::trace::Trace for PinKeyboard<'_> { fn trace(&self, t: &mut dyn crate::trace::Tracer) { t.component("PinKeyboard"); // So that debuglink knows the locations of the buttons - let mut digits_order: String<10> = String::new(); + let mut digits_order = ShortString::new(); for btn in self.digit_btns.iter() { let btn_content = btn.inner().content(); diff --git a/core/embed/rust/src/ui/model_tt/component/keyboard/slip39.rs b/core/embed/rust/src/ui/model_tt/component/keyboard/slip39.rs index 0794d9c280..66f11e86cf 100644 --- a/core/embed/rust/src/ui/model_tt/component/keyboard/slip39.rs +++ b/core/embed/rust/src/ui/model_tt/component/keyboard/slip39.rs @@ -3,8 +3,7 @@ use core::iter; use heapless::String; use crate::{ - trezorhal::slip39, - ui::{ + strutil::ShortString, trezorhal::slip39, ui::{ component::{ text::common::{TextBox, TextEdit}, Component, Event, EventCtx, @@ -21,17 +20,16 @@ use crate::{ }, theme, }, - shape, - shape::Renderer, + shape::{self, Renderer}, util::ResultExt, - }, + } }; const MAX_LENGTH: usize = 8; pub struct Slip39Input { button: Button, - textbox: TextBox, + textbox: TextBox, multi_tap: MultiTapKeyboard, final_word: Option<&'static str>, input_mask: Slip39Mask, @@ -136,7 +134,7 @@ impl Component for Slip39Input { // To simplify things, we always copy the printed string here, even if it // wouldn't be strictly necessary. - let mut text: String = String::new(); + let mut text = ShortString::new(); if let Some(word) = self.final_word { // We're done with input, paint the full word. @@ -200,7 +198,7 @@ impl Component for Slip39Input { // To simplify things, we always copy the printed string here, even if it // wouldn't be strictly necessary. - let mut text: String = String::new(); + let mut text = ShortString::new(); if let Some(word) = self.final_word { // We're done with input, paint the full word. @@ -281,7 +279,7 @@ impl Slip39Input { Self { // Button has the same style the whole time button: Button::empty().styled(theme::button_pin_confirm()), - textbox: TextBox::new(buff), + textbox: TextBox::new(&buff), multi_tap: MultiTapKeyboard::new(), final_word, input_mask, @@ -290,8 +288,8 @@ impl Slip39Input { fn setup_from_prefilled_word( word: &str, - ) -> (String, Slip39Mask, Option<&'static str>) { - let mut buff: String = String::new(); + ) -> (ShortString, Slip39Mask, Option<&'static str>) { + let mut buff = ShortString::new(); // Gradually appending encoded key digits to the buffer and checking if // have not already formed a final word.