Skip to main content

libmathcat/
lib.rs

1#![allow(clippy::needless_return)]
2
3//! A library for generating speech and braille from MathML
4//! 
5//! Typical usage is:
6//! 1. Set the rules directory [`set_rules_dir`]
7//! 2. Set whatever preferences are need with repeated calls to [`set_preference`].
8//! 3. Set MathML via [`set_mathml`]
9//!    A string representing the cleaned up MathML along with `id`s on each node is returned for highlighting if desired
10//! 4. Get the speech [`get_spoken_text`] or (Unicode) braille [`get_braille`].
11//!
12//! The expression can be navigated also.
13//! This is done in one of two ways:
14//! 1. Pass key strokes to allow a user to navigate the MathML by calling [`do_navigate_keypress`]; the speech is returned.
15//! 2. Pass the MathCAT navigation command directory by called [`do_navigate_command`]; the speech is return returned.
16//! 
17//! To get the MathML associated with the current navigation node, call [`get_navigation_mathml`].
18//! To just get the `id` and offset from the id of the current navigation node, call [`get_navigation_mathml_id`].
19///
20/// This module re-exports anyhow types. Use `bail!` for early returns and
21/// `context()`/`with_context()` on Result to add context (replacing old `chain_err()`).
22pub mod errors {
23    pub use anyhow::{anyhow, bail, Error, Result, Context};
24}
25
26pub mod interface;
27#[cfg(feature = "include-zip")]
28pub use shim_filesystem::ZIPPED_RULE_FILES;
29
30mod canonicalize;
31mod infer_intent;
32pub mod speech;
33mod braille;
34mod navigate;
35mod prefs;
36mod tts;
37mod xpath_functions;
38mod definitions;
39pub mod pretty_print;
40mod chemistry;
41
42pub mod shim_filesystem; // really just for override_file_for_debugging_rules, but the config seems to throw it off
43pub use interface::*;
44use crate::errors::{bail, Result};
45
46#[cfg(test)]
47pub fn init_logger() {
48    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("debug"))
49        .is_test(true)
50        .format_timestamp(None)
51        .format_module_path(false)
52        .format_indent(None)
53        .format_level(false)
54        .init();
55}
56
57/// Build Absolute path to rules dir for testing
58pub fn abs_rules_dir_path() -> String {
59    cfg_if::cfg_if! {
60    if #[cfg(feature = "include-zip")] {
61          return "Rules".to_string();
62    } else {
63        return std::env::current_exe().unwrap().parent().unwrap()
64                    .join("../../../Rules")
65                    .to_str().unwrap().to_string();
66        }
67    }
68}
69
70pub fn are_strs_canonically_equal_with_locale(test: &str, target: &str, ignore_attrs: &[&str], block_separators: &str, decimal_separators: &str) -> Result<()> {
71    use crate::{interface::*, pretty_print::mml_to_string};
72    use sxd_document::parser;
73    use crate::canonicalize::canonicalize;
74    use std::panic::{catch_unwind, AssertUnwindSafe};
75
76    crate::interface::init_panic_handler();
77    let result = catch_unwind(AssertUnwindSafe(|| {
78        // this forces initialization
79        crate::interface::set_rules_dir(abs_rules_dir_path()).unwrap();
80        crate::speech::SPEECH_RULES.with(|rules|  rules.borrow_mut().read_files().unwrap());
81        set_preference("Language", "en").unwrap();
82        set_preference("BlockSeparators", block_separators).unwrap();
83        set_preference("DecimalSeparators", decimal_separators).unwrap();
84
85        let package1 = &parser::parse(test).expect("Failed to parse test input");
86        let mathml = get_element(package1);
87        trim_element(mathml, false);
88        let mathml_test = canonicalize(mathml).unwrap();
89
90        let package2 = &parser::parse(target).expect("Failed to parse target input");
91        let mathml_target = get_element(package2);
92        trim_element(mathml_target, false);
93
94        match is_same_element(mathml_test, mathml_target, ignore_attrs) {
95            Ok(_) => Ok( () ),
96            Err(e) => {
97                bail!("{}\nResult:\n{}\nTarget:\n{}", e, mml_to_string(mathml_test), mml_to_string(mathml_target));
98            },
99        }
100    }));
101    match crate::interface::report_any_panic(result) {
102        Ok(()) => Ok(()),
103        Err(e) => {
104            eprintln!("{}", e);
105            Err(e)
106        }
107    }
108}
109
110/// sets locale to be US standard
111pub fn are_strs_canonically_equal(test: &str, target: &str, ignore_attrs: &[&str]) -> bool {
112    are_strs_canonically_equal_with_locale(test, target, ignore_attrs, ", \u{00A0}\u{202F}", ".").is_ok()
113}
114
115/// Like `are_strs_canonically_equal` but returns `Result` for use in `#[test]` functions that return `Result<()>`.
116pub fn are_strs_canonically_equal_result(test: &str, target: &str, ignore_attrs: &[&str]) -> Result<()> {
117    are_strs_canonically_equal_with_locale(test, target, ignore_attrs, ", \u{00A0}\u{202F}", ".")
118}