guinea-codegen 0.13.3

Build-script helpers for guinea applications: generated files written only when they change
Documentation
use std::{fs, path::Path};
use strsim::jaro_winkler;

pub fn suggest_closest<'a>(
    query: &str,
    candidates: impl Iterator<Item = &'a str>,
) -> Option<&'a str> {
    candidates
        .map(|cand| (cand, jaro_winkler(query, cand)))
        .filter(|(_, sim)| *sim > 0.7)
        .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
        .map(|(cand, _)| cand)
}

pub fn write_if_changed(path: &Path, content: &str) {
    let existing = fs::read_to_string(path).unwrap_or_default();
    if existing != content {
        if let Some(p) = path.parent() {
            let _ = fs::create_dir_all(p);
        }
        fs::write(path, content).ok();
    }
}