use std::io;
use std::path::Path;
use tempfile::{Builder, NamedTempFile};
pub fn temp_path_for(dest: &Path) -> io::Result<NamedTempFile> {
let parent = dest.parent().unwrap_or_else(|| Path::new("."));
let filename = dest.file_name().unwrap_or_default();
Builder::new()
.prefix(&format!(".{}.part-", filename.to_string_lossy()))
.suffix(".tmp")
.tempfile_in(parent)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_temp_path_creation() -> io::Result<()> {
let dir = std::env::temp_dir();
let dest = dir.join("testfile.txt");
let temp_file = temp_path_for(&dest)?;
assert!(temp_file.path().exists());
assert!(temp_file.path().parent() == Some(dir.as_path()));
assert!(temp_file
.path()
.to_string_lossy()
.contains("testfile.txt.part-"));
Ok(())
}
}