Skip to main content

guinea_codegen/
util.rs

1use std::{fs, path::Path};
2use strsim::jaro_winkler;
3
4pub fn suggest_closest<'a>(
5    query: &str,
6    candidates: impl Iterator<Item = &'a str>,
7) -> Option<&'a str> {
8    candidates
9        .map(|cand| (cand, jaro_winkler(query, cand)))
10        .filter(|(_, sim)| *sim > 0.7)
11        .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
12        .map(|(cand, _)| cand)
13}
14
15pub fn write_if_changed(path: &Path, content: &str) {
16    let existing = fs::read_to_string(path).unwrap_or_default();
17    if existing != content {
18        if let Some(p) = path.parent() {
19            let _ = fs::create_dir_all(p);
20        }
21        fs::write(path, content).ok();
22    }
23}