MCPcopy Create free account
hub / github.com/daisy/MathCAT / set_preference

Function set_preference

src/interface.rs:188–239  ·  view source on GitHub ↗

Set a MathCAT preference. The preference name should be a known preference name. The value should either be a string or a number (depending upon the preference being set) The list of known user preferences is in the MathCAT user documentation. Here are common preferences set by programs (not settable by the user): TTS -- SSML, SAPI5, None Pitch -- normalized at '1.0' Rate -- words per minute (shou

(name: String, value: String)

Source from the content-addressed store, hash-verified

186///
187/// Be careful setting preferences -- these potentially override user settings, so only preferences that really need setting should be set.
188pub fn set_preference(name: String, value: String) -> Result<()> {
189 let old_value = get_preference(name.clone())?; // make sure it is a valid preference
190
191 if name == "Language" {
192 // check the format
193 if !( value == "Auto" ||
194 value.len() == 2 ||
195 (value.len() == 5 && value.as_bytes()[2] == b'-') ) {
196 bail!("Improper format for 'Language' preference '{}'. Should be of form 'en' or 'en-gb'", value);
197 }
198 }
199
200 crate::speech::SPEECH_RULES.with(|rules| {
201 let rules = rules.borrow_mut();
202 if let Some(error_string) = rules.get_error() {
203 bail!("{}", error_string);
204 }
205
206 // we set the value even if it was the same as the old value because this might override a potentially changed future user value
207 let mut pref_manager = rules.pref_manager.borrow_mut();
208 let lower_case_value = value.to_lowercase();
209 if lower_case_value == "true" || lower_case_value == "false" {
210 pref_manager.set_api_boolean_pref(&name, value.to_lowercase()=="true");
211 } else {
212 match name.as_str() {
213 "Pitch" | "Rate" | "Volume" | "CapitalLetters_Pitch"=> {
214 pref_manager.set_api_float_pref(&name, to_float(&name, &value)?);
215 },
216 _ => {
217 pref_manager.set_api_string_pref(&name, &value);
218 },
219 }
220 };
221 return Ok::<(), Error>( () );
222 })?;
223
224 if old_value == value {
225 return Ok( () ); // nothing changed
226 }
227
228 if let Some(changed) = FilesChanged::new(&name) {
229 crate::speech::SpeechRules::invalidate(changed);
230 }
231 return Ok( () );
232
233 fn to_float(name: &str, value: &str) -> Result<f64> {
234 return match value.parse::<f64>() {
235 Ok(val) => Ok(val),
236 Err(_) => bail!("SetPreference: preference'{}'s value '{}' must be a float", name, value),
237 };
238 }
239}
240
241/// Get the braille associated with the MathML that was set by [`set_mathml`].
242/// The braille returned depends upon the preference for the `code` preference (default `Nemeth`).

Callers 15

test_intentFunction · 0.85
mainFunction · 0.85
timing_testFunction · 0.85
test_language_changeFunction · 0.85
ueb_highlight_24Function · 0.85
test_UEB_start_modeFunction · 0.85
init_default_prefsFunction · 0.85
zoom_in_parensFunction · 0.85
move_start_endFunction · 0.85
move_line_start_endFunction · 0.85
move_right_supFunction · 0.85

Calls 8

get_preferenceFunction · 0.85
to_floatFunction · 0.85
lenMethod · 0.80
set_api_boolean_prefMethod · 0.80
as_strMethod · 0.80
set_api_float_prefMethod · 0.80
set_api_string_prefMethod · 0.80
get_errorMethod · 0.45

Tested by 15

test_intentFunction · 0.68
test_language_changeFunction · 0.68
ueb_highlight_24Function · 0.68
test_UEB_start_modeFunction · 0.68
zoom_in_parensFunction · 0.68
move_start_endFunction · 0.68
move_line_start_endFunction · 0.68
move_right_supFunction · 0.68
move_simple_no_timesFunction · 0.68
where_am_i_allFunction · 0.68
auto_zoom_out_mrowFunction · 0.68
auto_zoom_out_fractionFunction · 0.68