Skip to content

Commit 1c570fa

Browse files
committed
refactor: Migrate replace_with_links to use Rust convenience function
Use load_thesaurus_from_json_and_replace() instead of manually loading thesaurus and calling replace_matches(). This is cleaner, more efficient, and aligns with the Rust API design. Benefits: - Reduced code duplication - Single source of truth for load-and-replace logic - Better alignment with Rust crate API - Slightly improved performance (one less allocation)
1 parent 718f33c commit 1c570fa

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

  • crates/terraphim_automata_py/src

crates/terraphim_automata_py/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use ::terraphim_automata::autocomplete::{
66
AutocompleteResult,
77
};
88
use ::terraphim_automata::matcher::{
9-
extract_paragraphs_from_automata, find_matches, replace_matches, LinkType, Matched,
9+
extract_paragraphs_from_automata, find_matches, LinkType, Matched,
1010
};
11-
use ::terraphim_automata::load_thesaurus_from_json;
11+
use ::terraphim_automata::{load_thesaurus_from_json, load_thesaurus_from_json_and_replace};
1212

1313
/// Python wrapper for AutocompleteIndex
1414
#[pyclass(name = "AutocompleteIndex")]
@@ -297,9 +297,6 @@ fn find_all_matches(
297297
/// >>> result = replace_with_links(text, json_str, "markdown")
298298
#[pyfunction]
299299
fn replace_with_links(text: &str, json_str: &str, link_type: &str) -> PyResult<String> {
300-
let thesaurus = load_thesaurus_from_json(json_str)
301-
.map_err(|e| PyValueError::new_err(format!("Failed to load thesaurus: {}", e)))?;
302-
303300
let link_type_enum = match link_type.to_lowercase().as_str() {
304301
"wiki" => LinkType::WikiLinks,
305302
"html" => LinkType::HTMLLinks,
@@ -313,7 +310,8 @@ fn replace_with_links(text: &str, json_str: &str, link_type: &str) -> PyResult<S
313310
}
314311
};
315312

316-
let result = replace_matches(text, thesaurus, link_type_enum)
313+
// Use the Rust convenience function that loads and replaces in one step
314+
let result = load_thesaurus_from_json_and_replace(json_str, text, link_type_enum)
317315
.map_err(|e| PyRuntimeError::new_err(format!("Failed to replace matches: {}", e)))?;
318316

319317
String::from_utf8(result)

0 commit comments

Comments
 (0)