1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-05-17 06:18:48 +00:00

style(core): simplify Rust translation conversions

[no changelog]
This commit is contained in:
Roman Zeyde 2025-05-06 22:41:21 +03:00 committed by Roman Zeyde
parent 7661c13a22
commit 2e9dfab8d1
2 changed files with 5 additions and 21 deletions

View File

@ -2,7 +2,7 @@ use crate::{
error::Error, error::Error,
io::InputStream, io::InputStream,
micropython::{ micropython::{
buffer::{get_buffer, StrBuffer}, buffer::get_buffer,
ffi, ffi,
macros::{ macros::{
attr_tuple, obj_dict, obj_fn_0, obj_fn_1, obj_fn_2, obj_map, obj_module, obj_type, 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; use super::translated_string::TranslatedString;
impl TryFrom<TranslatedString> for StrBuffer {
type Error = Error;
fn try_from(value: TranslatedString) -> Result<Self, Self::Error> {
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<Obj, Error> {
translation
.translate(super::flash::get()?.as_ref())
.try_into()
}
// SAFETY: Caller is supposed to be MicroPython, or copy MicroPython contracts // SAFETY: Caller is supposed to be MicroPython, or copy MicroPython contracts
// about the meaning of arguments. // about the meaning of arguments.
unsafe extern "C" fn tr_attr_fn(_self_in: Obj, attr: ffi::qstr, dest: *mut Obj) { 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 attr = Qstr::from_u16(attr as u16);
let result = if let Some(translation) = TranslatedString::from_qstr(attr) { 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 { } else {
return Err(Error::AttributeError(attr)); return Err(Error::AttributeError(attr));
}; };

View File

@ -4,7 +4,7 @@ use super::blob::Translations;
pub use super::generated::translated_string::TranslatedString; pub use super::generated::translated_string::TranslatedString;
impl 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 source
.and_then(|s| s.translation(self as _)) .and_then(|s| s.translation(self as _))
.unwrap_or(self.untranslated()) .unwrap_or(self.untranslated())