From b00308b86779db4d26575ffdc84e7caae9a2ece1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Gro=C3=9F?= Date: Thu, 8 Jan 2026 00:25:21 +0100 Subject: [PATCH] use more `AsRef` --- src/braille.rs | 6 +++--- src/interface.rs | 34 ++++++++++++++++------------------ src/main.rs | 10 +++++----- src/navigate.rs | 2 +- src/prefs.rs | 2 +- src/shim_filesystem.rs | 2 +- src/speech.rs | 2 +- tests/braille/Nemeth/other.rs | 2 +- tests/common/mod.rs | 8 ++++---- 9 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/braille.rs b/src/braille.rs index 1e481a68..88817371 100644 --- a/src/braille.rs +++ b/src/braille.rs @@ -3079,7 +3079,7 @@ mod tests { "; crate::interface::set_rules_dir(super::super::abs_rules_dir_path()).unwrap(); - set_mathml(mathml_str.to_string()).unwrap(); + set_mathml(mathml_str).unwrap(); set_preference("BrailleCode", "UEB").unwrap(); set_preference("BrailleNavHighlight", "All").unwrap(); let braille = get_braille("id-2")?; @@ -3135,7 +3135,7 @@ mod tests { "; crate::interface::set_rules_dir(super::super::abs_rules_dir_path()).unwrap(); - set_mathml(mathml_str.to_string()).unwrap(); + set_mathml(mathml_str).unwrap(); set_preference("BrailleNavHighlight", "Off").unwrap(); set_preference("BrailleCode", "Nemeth").unwrap(); @@ -3187,7 +3187,7 @@ mod tests { fn test_UEB_start_mode() -> Result<()> { let mathml_str = "xn"; crate::interface::set_rules_dir(super::super::abs_rules_dir_path()).unwrap(); - set_mathml(mathml_str.to_string()).unwrap(); + set_mathml(mathml_str).unwrap(); set_preference("BrailleCode", "UEB").unwrap(); set_preference("UEB_START_MODE", "Grade2").unwrap(); let braille = get_braille("")?; diff --git a/src/interface.rs b/src/interface.rs index 665e1e0d..2d92c2d0 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -66,17 +66,14 @@ fn init_mathml_instance() -> RefCell { /// Set the Rules directory /// IMPORTANT: this should be the very first call to MathCAT. If 'dir' is an empty string, the environment var 'MathCATRulesDir' is tried. -pub fn set_rules_dir(dir: String) -> Result<()> { +pub fn set_rules_dir(dir: impl AsRef) -> Result<()> { enable_logs(); use std::path::PathBuf; + let dir = dir.as_ref(); let dir = if dir.is_empty() { - std::env::var_os("MathCATRulesDir") - .unwrap_or_default() - .to_str() - .unwrap() - .to_string() + std::env::var_os("MathCATRulesDir").unwrap_or_default() } else { - dir + std::ffi::OsString::from(dir) }; let pref_manager = crate::prefs::PreferenceManager::get(); return pref_manager.borrow_mut().initialize(PathBuf::from(dir)); @@ -92,7 +89,7 @@ pub fn get_version() -> String { /// This will override any previous MathML that was set. /// This returns canonical MathML with 'id's set on any node that doesn't have an id. /// The ids can be used for sync highlighting if the `Bookmark` API preference is true. -pub fn set_mathml(mathml_str: String) -> Result { +pub fn set_mathml(mathml_str: impl AsRef) -> Result { enable_logs(); lazy_static! { // if these are present when resent to MathJaX, MathJaX crashes (https://github.com/mathjax/MathJax/issues/2822) @@ -111,6 +108,7 @@ pub fn set_mathml(mathml_str: String) -> Result { // This call reads all of them for the current preferences, but that's ok since they will likely be used crate::speech::SPEECH_RULES.with(|rules| rules.borrow_mut().read_files())?; + let mathml_str = mathml_str.as_ref(); return MATHML_INSTANCE.with(|old_package| { static HTML_ENTITIES_MAPPING: phf::Map<&str, &str> = include!("entities.in"); @@ -535,10 +533,10 @@ pub fn get_supported_languages() -> Vec { return language_paths; } - pub fn get_supported_speech_styles(lang: String) -> Vec { + pub fn get_supported_speech_styles(lang: impl AsRef) -> Vec { enable_logs(); let rules_dir = crate::prefs::PreferenceManager::get().borrow().get_rules_dir(); - let lang_dir = rules_dir.join("Languages").join(lang); + let lang_dir = rules_dir.join("Languages").join(lang.as_ref()); let mut speech_styles = find_files_in_dir_that_ends_with_shim(&lang_dir, "_Rules.yaml"); for file_name in &mut speech_styles { file_name.truncate(file_name.len() - "_Rules.yaml".len()) @@ -1103,9 +1101,9 @@ mod tests { // this forces initialization set_rules_dir(super::super::abs_rules_dir_path()).unwrap(); - let entity_str = set_mathml("𝕞".to_string()).unwrap(); + let entity_str = set_mathml("𝕞").unwrap(); let converted_str = - set_mathml("𝕞".to_string()).unwrap(); + set_mathml("𝕞").unwrap(); // need to remove unique ids lazy_static! { @@ -1116,19 +1114,19 @@ mod tests { assert_eq!(entity_str, converted_str, "normal entity test failed"); let entity_str = set_mathml( - "XXX".to_string(), + "XXX", ) .unwrap(); let converted_str = - set_mathml("XXX".to_string()).unwrap(); + set_mathml("XXX").unwrap(); let entity_str = ID_MATCH.replace_all(&entity_str, ""); let converted_str = ID_MATCH.replace_all(&converted_str, ""); assert_eq!(entity_str, converted_str, "special entities quote test failed"); let entity_str = - set_mathml("<>&lt;".to_string()).unwrap(); + set_mathml("<>&lt;").unwrap(); let converted_str = - set_mathml("<>&lt;".to_string()) + set_mathml("<>&lt;") .unwrap(); let entity_str = ID_MATCH.replace_all(&entity_str, ""); let converted_str = ID_MATCH.replace_all(&converted_str, ""); @@ -1140,13 +1138,13 @@ mod tests { use std::env; // MathCAT will check the env var "MathCATRulesDir" as an override, so the following test might succeed if we don't override the env var env::set_var("MathCATRulesDir", "MathCATRulesDir"); - assert!(set_rules_dir("someInvalidRulesDir".to_string()).is_err()); + assert!(set_rules_dir("someInvalidRulesDir").is_err()); assert!( set_rules_dir(super::super::abs_rules_dir_path()).is_ok(), "\nset_rules_dir to '{}' failed", super::super::abs_rules_dir_path() ); - assert!(set_mathml("1".to_string()).is_ok()); + assert!(set_mathml("1").is_ok()); } #[test] diff --git a/src/main.rs b/src/main.rs index 99b4f4d8..695ae82b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -224,11 +224,11 @@ fn main() { set_preference("Bookmark", "false").unwrap(); set_preference("SpeechStyle", "ClearSpeak").unwrap(); info!("Languages: {}", libmathcat::interface::get_supported_languages().join(", ")); - info!("Speech styles: {}", libmathcat::interface::get_supported_speech_styles("ClearSpeak".to_string()).join(", ")); + info!("Speech styles: {}", libmathcat::interface::get_supported_speech_styles("ClearSpeak").join(", ")); info!("BrailleCodes: {}", libmathcat::interface::get_supported_braille_codes().join(", ")); // set_preference("DecimalSeparators", ",").unwrap(); // set_preference("BlockSeparators", ". ").unwrap(); - if let Err(e) = set_mathml(expr.to_string()) { + if let Err(e) = set_mathml(expr) { panic!("Error: exiting -- {}", errors_to_string(&e)); }; @@ -308,7 +308,7 @@ fn timing_test(expr: &str, n_loops: usize) { let n_loops_float = n_loops as f64; let instant = Instant::now(); for _ in 0..n_loops { - if let Err(e) = set_mathml(expr.to_string()) { + if let Err(e) = set_mathml(expr) { panic!("Error: exiting -- {}", errors_to_string(&e)); }; match get_spoken_text() { @@ -324,7 +324,7 @@ fn timing_test(expr: &str, n_loops: usize) { let instant = Instant::now(); for _ in 0..n_loops { - if let Err(e) = set_mathml(expr.to_string()) { + if let Err(e) = set_mathml(expr) { panic!("Error: exiting -- {}", errors_to_string(&e)); }; } @@ -350,7 +350,7 @@ fn timing_test(expr: &str, n_loops: usize) { } info!("Time taken (time for {} braille averaged over {} loops): {}ms", get_preference("BrailleCode").unwrap(), n_loops, instant.elapsed().as_millis() as f64/n_loops_float); - if let Err(e) = set_mathml(expr.to_string()) { + if let Err(e) = set_mathml(expr) { panic!("Error: exiting -- {}", errors_to_string(&e)); }; set_preference("BrailleCode", "Nemeth").unwrap(); diff --git a/src/navigate.rs b/src/navigate.rs index 77cef18e..0fc11ca1 100644 --- a/src/navigate.rs +++ b/src/navigate.rs @@ -1025,7 +1025,7 @@ mod tests { set_preference("SpeechStyle", "SimpleSpeak").unwrap(); set_preference("Verbosity", "Medium").unwrap(); set_preference("Overview", "False").unwrap(); - set_mathml(mathml.to_string()).unwrap(); + set_mathml(mathml).unwrap(); } #[test] diff --git a/src/prefs.rs b/src/prefs.rs index f73cf89f..7443511e 100644 --- a/src/prefs.rs +++ b/src/prefs.rs @@ -1165,7 +1165,7 @@ cfg_if::cfg_if! {if #[cfg(not(feature = "include-zip"))] { assert_eq!(&pref_manager.pref_to_string("SpeechStyle"), "ClearSpeak"); assert_eq!(rel_path(&pref_manager.rules_dir, pref_manager.speech.as_path()), PathBuf::from("Languages/zz/ClearSpeak_Rules.yaml")); }); - interface::set_mathml("+10".to_string()).unwrap(); + interface::set_mathml("+10").unwrap(); assert_eq!(interface::get_spoken_text().unwrap(), "ClearSpeak positive from zz 10"); let mut file_path = PathBuf::default(); diff --git a/src/shim_filesystem.rs b/src/shim_filesystem.rs index 36722b6c..71e02de5 100644 --- a/src/shim_filesystem.rs +++ b/src/shim_filesystem.rs @@ -274,7 +274,7 @@ cfg_if! { // file_name should be path name starting at Rules dir: e.g, "Rules/en/navigate.yaml" OVERRIDE_FILE_NAME.with(|name| *name.borrow_mut() = file_name.to_string().replace("/", "\\")); OVERRIDE_FILE_CONTENTS.with(|contents| *contents.borrow_mut() = file_contents.to_string()); - crate::interface::set_rules_dir("Rules".to_string()).unwrap(); // force reinitialization after the change + crate::interface::set_rules_dir("Rules").unwrap(); // force reinitialization after the change } } else { pub fn is_file_shim(path: &Path) -> bool { diff --git a/src/speech.rs b/src/speech.rs index 091528db..be0b40cf 100644 --- a/src/speech.rs +++ b/src/speech.rs @@ -2887,7 +2887,7 @@ cfg_if::cfg_if! {if #[cfg(not(feature = "include-zip"))] { set_rules_dir(super::super::abs_rules_dir_path()).unwrap(); set_preference("Language", "zz-aa").unwrap(); // not much is support in zz - if let Err(e) = set_mathml("x".to_string()) { + if let Err(e) = set_mathml("x") { error!("{}", crate::errors_to_string(&e)); panic!("Should not be an error in setting MathML") } diff --git a/tests/braille/Nemeth/other.rs b/tests/braille/Nemeth/other.rs index fd14bb2f..6db1e1ea 100644 --- a/tests/braille/Nemeth/other.rs +++ b/tests/braille/Nemeth/other.rs @@ -81,7 +81,7 @@ fn find_baseline_indicator_bug_364() { set_rules_dir(abs_rules_dir_path()).unwrap(); set_preference("BrailleNavHighlight", "Off").unwrap(); set_preference("BrailleCode", "Nemeth").unwrap(); - if let Err(e) = set_mathml(expr.to_string()) { + if let Err(e) = set_mathml(expr) { panic!("{}", errors_to_string(&e)); }; match get_navigation_node_from_braille_position(4) { diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 736e2b84..482e588a 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -43,7 +43,7 @@ fn strip_spaces(str: &str) -> String { #[allow(dead_code)] // used in testing fn check_answer(test: &str, target: &str, failure_message: &str) { - if let Err(e) = set_mathml(test.to_string()) { + if let Err(e) = set_mathml(test) { panic!("{}", errors_to_string(&e)); }; match get_spoken_text() { @@ -123,7 +123,7 @@ pub fn test_braille(code: &str, mathml: &str, braille: &str) { "CMU" => set_preference("Language", "es").unwrap(), "UEB" | "Nemeth" | _ => set_preference("Language", "en").unwrap(), } - if let Err(e) = set_mathml(mathml.to_string()) { + if let Err(e) = set_mathml(mathml) { panic!("{}", errors_to_string(&e)); }; match get_braille("") { @@ -151,7 +151,7 @@ pub fn test_braille_prefs(code: &str, test_prefs: Vec<(&str, &str)>, mathml: &st set_preference(pref_name, pref_value).unwrap(); }; - if let Err(e) = set_mathml(mathml.to_string()) { + if let Err(e) = set_mathml(mathml) { panic!("{}", errors_to_string(&e)); }; match get_braille("") { @@ -240,7 +240,7 @@ pub fn test_from_braille(code: &str, mathml: &str, braille: &str) { "CMU" => set_preference("Language", "es").unwrap(), "UEB" | "Nemeth" | _ => set_preference("Language", "en").unwrap(), } - if let Err(e) = set_mathml(mathml.to_string()) { + if let Err(e) = set_mathml(mathml) { panic!("{}", errors_to_string(&e)); };