Skip to content

Commit

Permalink
Refactoed cloze field logic for answer template into closure
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnelmore committed Jun 24, 2024
1 parent e18ecfb commit 394cba1
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions rslib/src/notetype/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,17 +576,21 @@ impl Notetype {
}
};

// Update main templates
for (idx, (q_opt, a_opt)) in parsed.iter_mut().enumerate() {
q_update_fields(q_opt, &mut self.templates[idx].config.q_format);

let a_update_fields = |a_opt: &mut Option<ParsedTemplate>, template_target: &mut String| {
if let Some(a) = a_opt {
a.rename_and_remove_fields(&fields);
if is_cloze && !a.contains_cloze_replacement() {
a.add_missing_field_replacement(first_remaining_field_name, is_cloze);
}
self.templates[idx].config.a_format = a.template_to_string();
*template_target = a.template_to_string();
}
};

// Update main templates
for (idx, (q_opt, a_opt)) in parsed.iter_mut().enumerate() {
q_update_fields(q_opt, &mut self.templates[idx].config.q_format);

a_update_fields(a_opt, &mut self.templates[idx].config.a_format);
}

// Update browser templates, if they exist
Expand All @@ -596,13 +600,10 @@ impl Notetype {
&mut self.templates[idx].config.q_format_browser,
);

if let Some(a_browser) = a_browser_opt {
a_browser.rename_and_remove_fields(&fields);
if is_cloze && !a_browser.contains_cloze_replacement() {
a_browser.add_missing_field_replacement(first_remaining_field_name, is_cloze);
}
self.templates[idx].config.a_format_browser = a_browser.template_to_string();
}
a_update_fields(
a_browser_opt,
&mut self.templates[idx].config.a_format_browser,
);
}
}

Expand Down

0 comments on commit 394cba1

Please sign in to comment.