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

Method initialize

src/prefs.rs:411–450  ·  view source on GitHub ↗

Initialize (the) PreferenceManager (a global var). 'rules_dir' is the path to "Rules" unless the env var MathCATRulesDir is set If rules_dir is an empty PathBuf, the existing rules_dir is used (an error if it doesn't exist)

(&mut self, rules_dir: PathBuf)

Source from the content-addressed store, hash-verified

409 ///
410 /// If rules_dir is an empty PathBuf, the existing rules_dir is used (an error if it doesn't exist)
411 pub fn initialize(&mut self, rules_dir: PathBuf) -> Result<()> {
412 let mut rules_dir = rules_dir;
413 if rules_dir.as_os_str().is_empty() {
414 assert!(self.rules_dir.is_some());
415 rules_dir = self.rules_dir.clone().unwrap();
416 }
417
418 // first, read in the preferences -- need to determine which files to read next
419 // the prefs files are in the rules dir and the user dir; differs from other files
420 if self.api_prefs.prefs.is_empty() {
421 self.api_prefs = Preferences{ prefs: DEFAULT_API_PREFERENCES.with(|defaults| defaults.prefs.clone()) };
422 }
423
424 let pref_files;
425 if self.user_prefs.prefs.is_empty() || !PreferenceManager::is_file_up_to_date(&self.pref_files) {
426 let (user_prefs, pref_files_internal) = Preferences::from_file(&rules_dir)?;
427 self.user_prefs = user_prefs;
428 pref_files = Some(pref_files_internal);
429 } else {
430 pref_files = None;
431 }
432
433 match PreferenceManager::find_rules_dir(&rules_dir) {
434 Ok(rules_dir) => {
435 #[allow(clippy::unnecessary_unwrap)] // using 'if let...' is messy here
436 let pref_files = if pref_files.is_some() {pref_files.unwrap()} else {Preferences::get_prefs_file_and_time(&rules_dir)};
437 match self.set_all_files(&rules_dir, pref_files) {
438 Ok(_) => {
439 self.error = String::new();
440 return Ok(())
441 },
442 Err(e) => self.error = errors_to_string(&e),
443 }
444 },
445 Err(e) => {
446 self.error = errors_to_string(&e);
447 },
448 };
449 bail!("{}", self.error);
450 }
451
452 pub fn get() -> Rc<RefCell<PreferenceManager>> {
453 return PREF_MANAGER.with( |pm| pm.clone() );

Callers 13

updateMethod · 0.80
find_simple_styleFunction · 0.80
find_unicode_filesFunction · 0.80
found_all_filesFunction · 0.80
file_found_orderFunction · 0.80
test_prefsFunction · 0.80
test_language_changeFunction · 0.80
test_speech_style_changeFunction · 0.80
test_some_changesFunction · 0.80
test_up_to_dateFunction · 0.80

Calls 3

errors_to_stringFunction · 0.85
is_emptyMethod · 0.80
set_all_filesMethod · 0.80

Tested by 11

find_simple_styleFunction · 0.64
find_unicode_filesFunction · 0.64
found_all_filesFunction · 0.64
file_found_orderFunction · 0.64
test_prefsFunction · 0.64
test_language_changeFunction · 0.64
test_speech_style_changeFunction · 0.64
test_some_changesFunction · 0.64
test_up_to_dateFunction · 0.64