51 lines
1.1 KiB
Rust
51 lines
1.1 KiB
Rust
use std::{
|
|
env, fs, io,
|
|
path::{Path, PathBuf},
|
|
};
|
|
|
|
use clap::Parser;
|
|
use rand::distributions::{Alphanumeric, DistString};
|
|
|
|
fn rename_or_copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<()> {
|
|
match fs::rename(&from, &to) {
|
|
Ok(result) => Ok(result),
|
|
Err(_) => {
|
|
fs::copy(&from, &to)?;
|
|
fs::remove_file(from)?;
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
fn rand_string(len: usize) -> String {
|
|
Alphanumeric.sample_string(&mut rand::thread_rng(), len)
|
|
}
|
|
|
|
fn mktemp(len: usize) -> PathBuf {
|
|
let mut temp = env::temp_dir();
|
|
temp.push(format!("tmp.{}", rand_string(len)));
|
|
temp
|
|
}
|
|
|
|
#[derive(Parser, Debug)]
|
|
#[command(version, about, long_about = None)]
|
|
struct Args {
|
|
first: String,
|
|
second: String,
|
|
}
|
|
|
|
fn main() -> io::Result<()> {
|
|
let args = Args::parse();
|
|
let temp = mktemp(10);
|
|
let cwd = env::current_dir()?;
|
|
|
|
let first = Path::join(&cwd, args.first);
|
|
let second = Path::join(&cwd, args.second);
|
|
|
|
rename_or_copy(&first, &temp)?;
|
|
rename_or_copy(&second, &first)?;
|
|
rename_or_copy(&temp, &second)?;
|
|
|
|
Ok(())
|
|
}
|