mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-30 07:28:24 +01:00
43 lines
1.0 KiB
Rust
43 lines
1.0 KiB
Rust
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<T, F>(file_name: &OsStr, func: F) -> Result<Option<T>>
|
|
where
|
|
F: Fn(&OsStr) -> Result<Option<T>>,
|
|
{
|
|
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)
|
|
}
|