use std::ffi::OsStr; use std::path::Path; use crate::error::*; const IGNORED_SUFFIXES: [&str; 13] = [ // Editor etc backups "~", ".bak", ".old", ".orig", // Debian and derivatives apt/dpkg/ucf backups ".dpkg-dist", ".dpkg-old", ".ucf-dist", ".ucf-new", ".ucf-old", // Red Hat and derivatives rpm backups ".rpmnew", ".rpmorig", ".rpmsave", // Build system input/template files ".in", ]; /// If we find an ignored suffix on the file name, e.g. '~', we strip it and /// then try again without it. pub fn try_with_stripped_suffix(file_name: &OsStr, func: F) -> Result> where F: Fn(&OsStr) -> Result>, { let mut from_stripped = None; if let Some(file_str) = Path::new(file_name).to_str() { for suffix in &IGNORED_SUFFIXES { if let Some(stripped_filename) = file_str.strip_suffix(suffix) { from_stripped = func(OsStr::new(stripped_filename))?; break; } } } Ok(from_stripped) }