From 2e9dfab8d1677341e1fb6f95d2d57b86e6917c15 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 6 May 2025 22:41:21 +0300 Subject: [PATCH] style(core): simplify Rust translation conversions [no changelog] --- core/embed/rust/src/translations/obj.rs | 24 ++++--------------- .../src/translations/translated_string.rs | 2 +- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/core/embed/rust/src/translations/obj.rs b/core/embed/rust/src/translations/obj.rs index 9b341abdc7..c608c17a39 100644 --- a/core/embed/rust/src/translations/obj.rs +++ b/core/embed/rust/src/translations/obj.rs @@ -2,7 +2,7 @@ use crate::{ error::Error, io::InputStream, micropython::{ - buffer::{get_buffer, StrBuffer}, + buffer::get_buffer, ffi, macros::{ attr_tuple, obj_dict, obj_fn_0, obj_fn_1, obj_fn_2, obj_map, obj_module, obj_type, @@ -20,24 +20,6 @@ use crate::{ use super::translated_string::TranslatedString; -impl TryFrom for StrBuffer { - type Error = Error; - - fn try_from(value: TranslatedString) -> Result { - let blob = super::flash::get()?; - let translated = value.translate(blob.as_ref()); - StrBuffer::alloc(translated) - // TODO fall back to English (which is static and can be converted - // infallibly) if the allocation fails? - } -} - -fn translate(translation: TranslatedString) -> Result { - translation - .translate(super::flash::get()?.as_ref()) - .try_into() -} - // SAFETY: Caller is supposed to be MicroPython, or copy MicroPython contracts // about the meaning of arguments. unsafe extern "C" fn tr_attr_fn(_self_in: Obj, attr: ffi::qstr, dest: *mut Obj) { @@ -49,7 +31,9 @@ unsafe extern "C" fn tr_attr_fn(_self_in: Obj, attr: ffi::qstr, dest: *mut Obj) } let attr = Qstr::from_u16(attr as u16); let result = if let Some(translation) = TranslatedString::from_qstr(attr) { - translate(translation)? + translation.map_translated(|t| t.try_into())? + // TODO fall back to English (which is static and can be converted + // infallibly) if the allocation fails? } else { return Err(Error::AttributeError(attr)); }; diff --git a/core/embed/rust/src/translations/translated_string.rs b/core/embed/rust/src/translations/translated_string.rs index b10917e2a4..c9c8a860ae 100644 --- a/core/embed/rust/src/translations/translated_string.rs +++ b/core/embed/rust/src/translations/translated_string.rs @@ -4,7 +4,7 @@ use super::blob::Translations; pub use super::generated::translated_string::TranslatedString; impl TranslatedString { - pub(super) fn translate<'a>(self, source: Option<&'a Translations>) -> &'a str { + pub fn translate<'a>(self, source: Option<&'a Translations>) -> &'a str { source .and_then(|s| s.translation(self as _)) .unwrap_or(self.untranslated())