Skip to content

Commit 893eb99

Browse files
committed
Change as_opt_str!(...) to be ....as_deref(). I think it is a little cleaner. They end up being identical.
1 parent 7599614 commit 893eb99

9 files changed

Lines changed: 138 additions & 140 deletions

File tree

src/braille.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use strum_macros::Display;
33
use sxd_document::dom::{Element, ChildOfElement};
44
use sxd_document::Package;
5-
use sxd_document::{as_str, as_opt_str};
5+
use sxd_document::as_str;
66
use crate::definitions::SPEECH_DEFINITIONS;
77
use crate::errors::*;
88
use crate::pretty_print::mml_to_string;
@@ -328,7 +328,7 @@ pub fn get_navigation_node_from_braille_position(mathml: Element, position: usiz
328328
/// 'node' is the current node we are on inside of 'mathml'
329329
fn find_navigation_node<'e>(mathml: Element<'e>, node: Element<'e>, target_position: usize) -> Result<SearchState<'e>> {
330330
let raw_node_id = node.attribute_value("id");
331-
let node_id = match as_opt_str!(raw_node_id) {
331+
let node_id = match raw_node_id.as_deref() {
332332
Some(id) => id,
333333
None => bail!("'id' is not present on mathml: {}", mml_to_string(node)),
334334
};
@@ -2349,7 +2349,7 @@ impl BrailleChars {
23492349
Regex::new(r"(?P<face>[SB𝔹TIR]*)(?P<lang>[EDGVHU]?)(?P<cap>C?)(?P<letter>L?)(?P<num>[N]?)(?P<char>.)").unwrap()
23502350
});
23512351
let raw_math_variant = node.attribute_value("mathvariant");
2352-
let math_variant = as_opt_str!(raw_math_variant);
2352+
let math_variant = raw_math_variant.as_deref();
23532353
let attr_typeface = match math_variant {
23542354
None => "R",
23552355
Some(variant) => match variant {
@@ -2425,7 +2425,7 @@ impl BrailleChars {
24252425
});
24262426

24272427
let raw_math_variant = node.attribute_value("mathvariant");
2428-
let math_variant = as_opt_str!(raw_math_variant);
2428+
let math_variant = raw_math_variant.as_deref();
24292429
let text = BrailleChars::substring(as_str!(as_text(node)), &text_range);
24302430
let mut braille_chars = braille_replace_chars(&text, node)?;
24312431

@@ -2479,7 +2479,7 @@ impl BrailleChars {
24792479
});
24802480

24812481
let raw_math_variant = node.attribute_value("mathvariant");
2482-
let math_variant = as_opt_str!(raw_math_variant);
2482+
let math_variant = raw_math_variant.as_deref();
24832483
let text = BrailleChars::substring(as_str!(as_text(node)), &text_range);
24842484
let text = add_separator(text);
24852485

@@ -2635,7 +2635,7 @@ impl BrailleChars {
26352635
true
26362636
},
26372637
"menclose" => {
2638-
if let Some(notation) = as_opt_str!(node.attribute_value("notation")) {
2638+
if let Some(notation) = node.attribute_value("notation").as_deref() {
26392639
if notation != "bottom" || notation != "box" {
26402640
return false;
26412641
}

src/canonicalize.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::rc::Rc;
1111
use std::cell::RefCell;
1212
use sxd_document::dom::{Element, Document, ChildOfElement, Attribute};
1313
use sxd_document::QName;
14-
use sxd_document::{as_str, as_opt_str, as_qname};
14+
use sxd_document::{as_str, as_qname};
1515

1616
#[cfg(not(feature = "no-unsafe"))]
1717
pub type NameStr<'a> = &'a str;
@@ -298,7 +298,7 @@ impl<'a, 'op:'a> StackInfo<'a, 'op> {
298298
let children = self.mrow.children();
299299
for &child in children.iter().rev() {
300300
let child = as_element(child);
301-
if let Some(value) = as_opt_str!(child.attribute_value(CHANGED_ATTR))
301+
if let Some(value) = child.attribute_value(CHANGED_ATTR).as_deref()
302302
&& value == "empty_content" {
303303
continue;
304304
}
@@ -398,7 +398,7 @@ pub fn get_presentation_element(element: Element) -> (usize, Element) {
398398
assert_eq!(name(element), "semantics");
399399
let children = element.children();
400400
if let Some( (i, child) ) = children.iter().enumerate().find(|&(_, &child)|
401-
if let Some(encoding) = as_opt_str!(as_element(child).attribute_value("encoding")) {
401+
if let Some(encoding) = as_element(child).attribute_value("encoding").as_deref() {
402402
encoding == "MathML-Presentation"
403403
} else {
404404
false
@@ -866,7 +866,7 @@ impl CanonicalizeContext {
866866
}
867867
return Some(mathml);
868868
} else if OPERATORS.get(as_str!(text)).is_some() {
869-
if let Some(intent_value) = as_opt_str!(mathml.attribute_value(INTENT_ATTR)) {
869+
if let Some(intent_value) = mathml.attribute_value(INTENT_ATTR).as_deref() {
870870
// if it is a unit, it might be seconds, minutes, feet, ... not an operator
871871
if intent_value.contains(":unit") {
872872
return Some(mathml);
@@ -1059,7 +1059,7 @@ impl CanonicalizeContext {
10591059

10601060
// normalize width ems
10611061
let width_raw = mathml.attribute_value("width");
1062-
let width = as_opt_str!(width_raw).unwrap_or("0em");
1062+
let width = width_raw.as_deref().unwrap_or("0em");
10631063
let normalized_width = crate::xpath_functions::FontSizeGuess::em_from_value(width);
10641064
mathml.set_attribute_value("data-width", &normalized_width.to_string());
10651065
return Some(mathml);
@@ -1243,7 +1243,7 @@ impl CanonicalizeContext {
12431243
if child == new_presentation {
12441244
continue;
12451245
}
1246-
let attr_name = match as_opt_str!(child.attribute_value("encoding")) {
1246+
let attr_name = match child.attribute_value("encoding").as_deref() {
12471247
Some(encoding_name) => format!("data-{}-{}", child_name, encoding_name.replace('/', "_slash_")),
12481248
None => format!("data-{child_name}"), // probably shouldn't happen
12491249
};
@@ -1272,12 +1272,12 @@ impl CanonicalizeContext {
12721272
return false;
12731273
}
12741274

1275-
if let Some(intent_value) = as_opt_str!(mathml.attribute_value(INTENT_ATTR))
1275+
if let Some(intent_value) = mathml.attribute_value(INTENT_ATTR).as_deref()
12761276
&& (intent_value != "ratio" || !intent_value.starts_with('_')) {
12771277
return false;
12781278
}
12791279

1280-
if let Some(value) = as_opt_str!(mathml.attribute_value("data-mjx-texclass"))
1280+
if let Some(value) = mathml.attribute_value("data-mjx-texclass").as_deref()
12811281
&& value == "PUNCT" {
12821282
mathml.remove_attribute("data-mjx-texclass");
12831283
mathml.set_attribute_value(SPACE_AFTER, "true"); // signal to at least Nemeth rules that this is punctuation
@@ -1359,7 +1359,7 @@ impl CanonicalizeContext {
13591359
} else {
13601360
mathml
13611361
};
1362-
if let Some(width) = as_opt_str!(mpadded.attribute_value("width")) {
1362+
if let Some(width) = mpadded.attribute_value("width").as_deref() {
13631363
if width != "0" {
13641364
return false;
13651365
}
@@ -1801,12 +1801,12 @@ impl CanonicalizeContext {
18011801
fn convert_mfenced_to_mrow(mfenced: Element) -> Element {
18021802
// The '<'/'>' replacements are because WIRIS uses them out instead of the correct chars in its template
18031803
let open_raw = mfenced.attribute_value("open");
1804-
let open = as_opt_str!(open_raw).unwrap_or("(").replace('<', "⟨");
1804+
let open = open_raw.as_deref().unwrap_or("(").replace('<', "⟨");
18051805
let close_raw = mfenced.attribute_value("close");
1806-
let close = as_opt_str!(close_raw).unwrap_or(")").replace('>', "⟩");
1806+
let close = close_raw.as_deref().unwrap_or(")").replace('>', "⟩");
18071807
// debug!("open={}, close={}", open, close);
18081808
let separators_raw = mfenced.attribute_value("separators");
1809-
let mut separators = as_opt_str!(separators_raw).unwrap_or(",").chars();
1809+
let mut separators = separators_raw.as_deref().unwrap_or(",").chars();
18101810
set_mathml_name(mfenced, "mrow");
18111811
mfenced.remove_attribute("open");
18121812
mfenced.remove_attribute("close");
@@ -1956,7 +1956,7 @@ impl CanonicalizeContext {
19561956
// i, whitespace, previous_mtext_with_width.is_some(), mml_to_string(child));
19571957
if is_child_whitespace {
19581958
// update the running total of whitespace
1959-
let child_width = as_opt_str!(child.attribute_value("data-width")).unwrap_or("0")
1959+
let child_width = child.attribute_value("data-width").as_deref().unwrap_or("0")
19601960
.parse::<f64>().unwrap_or(0.0) ;
19611961
whitespace = match whitespace {
19621962
None => Some(child_width),
@@ -1993,7 +1993,7 @@ impl CanonicalizeContext {
19931993
if children.len() == 1 {
19941994
// only child -- check to see if we need to set the space-width
19951995
let child = as_element(children[0]);
1996-
let child_width = as_opt_str!(child.attribute_value("data-width")).unwrap_or("0").parse::<f64>().unwrap_or(0.0);
1996+
let child_width = child.attribute_value("data-width").as_deref().unwrap_or("0").parse::<f64>().unwrap_or(0.0);
19971997
if (child_width - ws).abs() > 0.001 {
19981998
ws += child_width;
19991999
child.set_attribute_value("data-following-space-width", ws.to_string().as_str());
@@ -3156,7 +3156,7 @@ impl CanonicalizeContext {
31563156

31573157
let mi_text = as_str!(as_text(mi));
31583158
let variant_raw = mi.attribute_value("mathvariant");
3159-
let variant = as_opt_str!(variant_raw);
3159+
let variant = variant_raw.as_deref();
31603160

31613161
if names.contains(mi_text) {
31623162
return mi; // avoid mapping mathvariant for function names
@@ -3490,7 +3490,7 @@ impl CanonicalizeContext {
34903490

34913491
// if a form has been given, that takes precedence
34923492
let form_raw = mo_node.attribute_value("form");
3493-
let form = as_opt_str!(form_raw);
3493+
let form = form_raw.as_deref();
34943494
let op_type = match form {
34953495
None => match context {
34963496
None => OperatorTypes::POSTFIX, // what compute_type_from_position returns when the other args to this are all None
@@ -3694,7 +3694,7 @@ impl CanonicalizeContext {
36943694

36953695
// debug!(" in is_likely_chemical_state: '{}'?",element_summary(node));
36963696
let node_chem_raw = node.attribute_value(MAYBE_CHEMISTRY);
3697-
let node_chem_likelihood = as_opt_str!(node_chem_raw);
3697+
let node_chem_likelihood = node_chem_raw.as_deref();
36983698
if node.attribute(MAYBE_CHEMISTRY).is_none() {
36993699
return FunctionNameCertainty::True;
37003700
}
@@ -4401,7 +4401,7 @@ impl CanonicalizeContext {
44014401
} else {
44024402
OperatorPair{ ch: op_ch("\u{2062}"), op: *IMPLIED_TIMES, _phantom: PhantomData }
44034403
};
4404-
if let Some(attr_val) = as_opt_str!(base_of_child.attribute_value(CHANGED_ATTR))
4404+
if let Some(attr_val) = base_of_child.attribute_value(CHANGED_ATTR).as_deref()
44054405
&& attr_val == "data-was-mo" {
44064406
// it really should be an operator
44074407
base_of_child.remove_attribute(CHANGED_ATTR);

src/chemistry.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
use sxd_document::dom::{Element, Document, ChildOfElement};
3939
#[allow(unused_imports)]
40-
use sxd_document::{as_str, as_opt_str, as_qname};
40+
use sxd_document::{as_str, as_qname};
4141
use crate::canonicalize::*;
4242
use crate::canonicalize::NameStr;
4343
use crate::pretty_print::mml_to_string;
@@ -90,7 +90,7 @@ fn is_chem_equation_arrow(ch: char) -> bool {
9090

9191
// Returns true if the 'property' (should have ":") is in the intent
9292
fn has_chem_intent(mathml: Element, property: &str) -> bool {
93-
if let Some(intent) = as_opt_str!(mathml.attribute_value(INTENT_ATTR)) {
93+
if let Some(intent) = mathml.attribute_value(INTENT_ATTR).as_deref() {
9494
let head = intent.split('(').next().unwrap();
9595
return head.contains(property);
9696
}
@@ -212,7 +212,7 @@ fn clean_mrow_children_restructure_pass<'a>(old_children: &[Element<'a>]) -> Opt
212212
likely_chem_equation_operator(child); // need to mark MAYBE_CHEMISTRY for CHEMICAL_BOND tests
213213
}
214214
} else if child_name == "mrow" &&
215-
let Some(latex_value) = as_opt_str!(child.attribute_value("data-latex")) &&
215+
let Some(latex_value) = child.attribute_value("data-latex").as_deref() &&
216216
latex_value == r"\mathrel{\longrightleftharpoons}" {
217217
child.set_attribute_value("data-unicode", "\u{1f8d2}");
218218
child.set_attribute_value(MAYBE_CHEMISTRY, "2"); // same as is_hack_for_missing_arrows()
@@ -358,7 +358,7 @@ pub fn convert_leaves_to_chem_elements(mathml: Element) -> Option<Vec<Element>>
358358
let chem_token_string = vec![token_string.as_bytes()[0], second_element_text.as_bytes()[0]];
359359
if let Some(chem_element) = get_chem_element(doc, &chem_token_string, 2) {
360360
chem_element.set_text(as_str!(as_text(chem_element)));
361-
chem_element.set_attribute_value(MAYBE_CHEMISTRY, as_opt_str!(chem_element.attribute_value(MAYBE_CHEMISTRY)).unwrap());
361+
chem_element.set_attribute_value(MAYBE_CHEMISTRY, chem_element.attribute_value(MAYBE_CHEMISTRY).as_deref().unwrap());
362362
chem_element.set_attribute_value(MERGED_TOKEN, "true");
363363
second_element.remove_from_parent();
364364
return Some(vec![chem_element]);
@@ -437,7 +437,7 @@ pub fn scan_and_mark_chemistry(mathml: Element) -> bool {
437437
let child = as_element(mathml.children()[0]);
438438
// debug!("scan_and_mark_chemistry:\n{}", mml_to_string(child));
439439
assert_eq!(name(mathml), "math");
440-
let is_chemistry = if let Some(latex) = as_opt_str!(mathml.attribute_value("data-latex")) {
440+
let is_chemistry = if let Some(latex) = mathml.attribute_value("data-latex").as_deref() {
441441
// MathJax v4 includes this really useful info -- if it starts \ce -- we have Chemistry
442442
// need to determine if it is an equation or a formula
443443
latex.trim_start().starts_with(r"\ce")
@@ -476,7 +476,7 @@ pub fn scan_and_mark_chemistry(mathml: Element) -> bool {
476476

477477
// returns the marked attr value or None
478478
fn get_marked_value(mathml: Element) -> Option<isize> {
479-
if let Some(value) = as_opt_str!(mathml.attribute_value(MAYBE_CHEMISTRY)) {
479+
if let Some(value) = mathml.attribute_value(MAYBE_CHEMISTRY).as_deref() {
480480
return Some(value.parse().unwrap());
481481
} else {
482482
return None;
@@ -572,7 +572,7 @@ fn is_changed_after_unmarking_chemistry(mathml: Element) -> bool {
572572
// let parent = get_parent(mathml);
573573
// debug!("After merge_element: -- parent{}", mml_to_string(parent));
574574

575-
} else if let Some(changed_value) = as_opt_str!(mathml.attribute_value(CHANGED_ATTR)) &&
575+
} else if let Some(changed_value) = mathml.attribute_value(CHANGED_ATTR).as_deref() &&
576576
changed_value == ADDED_ATTR_VALUE &&
577577
name(mathml) != "mtext" { // a hack fix for #477 (chem never modifies mtext, so this is ok)
578578
mathml.remove_from_parent();
@@ -655,7 +655,7 @@ fn is_changed_after_unmarking_chemistry(mathml: Element) -> bool {
655655
}
656656
}
657657
if name(mathml) == "mrow" {
658-
if let Some(changed_value) = as_opt_str!(mathml.attribute_value(CHANGED_ATTR)) {
658+
if let Some(changed_value) = mathml.attribute_value(CHANGED_ATTR).as_deref() {
659659
// we added an mrow, we can remove it -- but this might be already processed which is the case if "data-id-added" is true (exists)
660660
if changed_value == ADDED_ATTR_VALUE && mathml.attribute("data-id-added").is_none() {
661661
// mrows get added for several reasons. One of them is to canonicalize elements like msqrt that can have 1 or more children;
@@ -707,7 +707,7 @@ fn is_changed_after_unmarking_chemistry(mathml: Element) -> bool {
707707
if preceding_children.is_empty() ||
708708
!(name(parent) == "mmultiscripts" ||
709709
(name(parent) == "mrow" && parent.attribute_value(CHANGED_ATTR).is_some() &&
710-
as_opt_str!(parent.attribute_value(CHANGED_ATTR)).unwrap() == ADDED_ATTR_VALUE)) {
710+
parent.attribute_value(CHANGED_ATTR).as_deref().unwrap() == ADDED_ATTR_VALUE)) {
711711
bail!("Internal error: {} should not have been split'", mml_to_string(mathml));
712712
}
713713
}
@@ -1711,7 +1711,7 @@ pub fn likely_chem_element(mathml: Element) -> isize {
17111711
} else if is_chemical_element(mathml) {
17121712
// single letter = 1; single letter with mathvarinat="normal" = 2; double = 3 -- all elements are ASCII
17131713
return (if text.len() == 1 {
1714-
if as_opt_str!(mathml.attribute_value("mathvariant")).unwrap_or_default() == "normal" {2} else {1}
1714+
if mathml.attribute_value("mathvariant").as_deref().unwrap_or_default() == "normal" {2} else {1}
17151715
} else {
17161716
3
17171717
}) as isize;

src/infer_intent.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![allow(clippy::needless_return)]
77

88
use sxd_document::dom::{Element, Document, ChildOfElement};
9-
use sxd_document::{as_str, as_opt_str};
9+
use sxd_document::as_str;
1010
use crate::prefs::PreferenceManager;
1111
use crate::speech::SpeechRulesWithContext;
1212
use crate::canonicalize::{as_element, as_text, name, create_mathml_element, set_mathml_name, INTENT_ATTR, MATHML_FROM_NAME_ATTR};
@@ -94,7 +94,7 @@ pub fn simplify_fixity_properties(properties: &str) -> String {
9494
/// Given the intent add the fixity property for the intent if it isn't given (and one exists)
9595
fn add_fixity(intent: Element) {
9696
let raw_properties = intent.attribute_value(INTENT_PROPERTY);
97-
let properties = as_opt_str!(raw_properties).unwrap_or_default();
97+
let properties = raw_properties.as_deref().unwrap_or_default();
9898
if properties.split(":").all(|property| !FIXITIES.contains(property)) {
9999
let intent_name = name(intent);
100100
crate::definitions::SPEECH_DEFINITIONS.with(|definitions| {
@@ -132,17 +132,17 @@ pub fn add_fixity_children(intent: Element) -> Element {
132132
return mathml;
133133
}
134134
// we also exclude fixity on mtable because they mess up the counts (see 'en::mtable::unknown_mtable_property')
135-
if as_opt_str!(mathml.attribute_value(MATHML_FROM_NAME_ATTR)).unwrap_or_default() == "mtable" {
135+
if mathml.attribute_value(MATHML_FROM_NAME_ATTR).as_deref().unwrap_or_default() == "mtable" {
136136
return mathml;
137137
}
138138
let doc = mathml.document();
139139
let raw_properties = mathml.attribute_value(INTENT_PROPERTY);
140-
let properties = as_opt_str!(raw_properties).unwrap_or_default();
140+
let properties = raw_properties.as_deref().unwrap_or_default();
141141
let fixity = properties.rsplit(':').find(|&property| FIXITIES.contains(property)).unwrap_or_default();
142142
let intent_name = as_str!(name(mathml));
143143

144144
let raw_op_id = mathml.attribute_value("id");
145-
let op_name_id = as_opt_str!(raw_op_id).unwrap_or("new-id");
145+
let op_name_id = raw_op_id.as_deref().unwrap_or("new-id");
146146
match fixity {
147147
"infix" => {
148148
let mut new_children = Vec::with_capacity(2*children.len()-1);
@@ -427,7 +427,7 @@ fn build_intent<'b, 'r, 'c, 's:'c, 'm:'c>(rules_with_context: &'r mut SpeechRule
427427
let mathml_name = name(mathml);
428428
let raw_intent_attr = mathml.attribute_value(INTENT_ATTR);
429429
intent.set_attribute_value(MATHML_FROM_NAME_ATTR,
430-
if word == as_opt_str!(raw_intent_attr).unwrap_or_default() {as_str!(mathml_name)} else {leaf_name});
430+
if word == raw_intent_attr.as_deref().unwrap_or_default() {as_str!(mathml_name)} else {leaf_name});
431431
intent.set_text(word); // '-' and '_' get removed by the rules.
432432
if let Some(id) = mathml.attribute_value("id") {
433433
intent.set_attribute_value("id", &format!("{}-literal-{}", id, intent_offset));
@@ -558,7 +558,7 @@ fn lift_function_name<'m>(doc: Document<'m>, function_name: Element<'m>, childre
558558
function_name.set_text("");
559559
function_name.replace_children(children);
560560
if name(function_name).find(|ch: char| ch!='_' && ch!='-').is_none() {
561-
let properties = as_opt_str!(function_name.attribute_value(INTENT_PROPERTY)).unwrap_or(":").to_owned();
561+
let properties = function_name.attribute_value(INTENT_PROPERTY).as_deref().unwrap_or(":").to_owned();
562562
function_name.set_attribute_value(INTENT_PROPERTY, &(properties + "silent:"));
563563
}
564564
return function_name;

src/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use regex::{Captures, Regex};
1212
use sxd_document::dom::{Element, Document, ChildOfRoot, ChildOfElement, Attribute};
1313
use sxd_document::parser;
1414
use sxd_document::Package;
15-
use sxd_document::{as_str, as_opt_str, as_qname};
15+
use sxd_document::{as_str, as_qname};
1616

1717
use crate::canonicalize::{as_element, name};
1818
use crate::shim_filesystem::{find_all_dirs_shim, find_files_in_dir_that_ends_with_shim};
@@ -836,7 +836,7 @@ pub fn trim_element(e: Element, allow_structure_in_leaves: bool) {
836836
let child_text = match child {
837837
ChildOfElement::Element(child) => {
838838
if name(child) == "mglyph" {
839-
as_opt_str!(child.attribute_value("alt")).unwrap_or("").to_string()
839+
child.attribute_value("alt").as_deref().unwrap_or("").to_string()
840840
} else {
841841
gather_text(child)
842842
}

0 commit comments

Comments
 (0)