use std::path::PathBuf;
#[macro_export]
macro_rules! no_window {
($cmd:expr) => {
#[cfg(target_os = "windows")]
{
use std::os::windows::process::CommandExt;
$cmd.creation_flags(0x08000000);
}
};
}
pub fn replace_tilde(path: &str) -> PathBuf {
#[cfg(target_family = "unix")]
let path = if let Some(stripped) = path.strip_prefix("~") {
if let Ok(home) = std::env::var("HOME") {
home + stripped
} else {
path.to_string()
}
} else {
path.to_string()
};
PathBuf::from(path)
}
pub struct CorruptionGuard<F: FnOnce()> {
f: Option<F>,
is_successful: bool,
}
impl<F: FnOnce()> CorruptionGuard<F> {
pub fn new(f: F) -> Self {
Self {
f: Some(f),
is_successful: false,
}
}
pub fn succeed(&mut self) {
self.is_successful = true;
}
}
impl<F: FnOnce()> Drop for CorruptionGuard<F> {
fn drop(&mut self) {
if !self.is_successful
&& let Some(f) = self.f.take()
{
f();
}
}
}