From 60cc64999826747c22ae433c788f7c0bf6571224 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 16 Aug 2023 11:50:55 -0700 Subject: [PATCH] feat: add convenience methods for creating temp files with prefixes - `TempDir::with_prefix(prefix)` - `TempDir::with_prefix_in(prefix, directory)` - `TempFile::with_prefix(prefix)` - `TempFile::with_prefix_in(prefix, directory)` fixes #249 --- src/dir.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ src/file/mod.rs | 33 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/dir.rs b/src/dir.rs index 483b6d807..1b79be445 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::ffi::OsStr; use std::fs::remove_dir_all; use std::mem; use std::path::{self, Path, PathBuf}; @@ -264,6 +265,65 @@ impl TempDir { Builder::new().tempdir_in(dir) } + /// Attempts to make a temporary directory with the specified prefix inside of + /// `env::temp_dir()`. The directory and everything inside it will be automatically + /// deleted once the returned `TempDir` is destroyed. + /// + /// # Errors + /// + /// If the directory can not be created, `Err` is returned. + /// + /// # Examples + /// + /// ``` + /// use std::fs::{self, File}; + /// use std::io::Write; + /// use tempfile::TempDir; + /// + /// # use std::io; + /// # fn run() -> Result<(), io::Error> { + /// // Create a directory inside of the current directory + /// let tmp_dir = TempDir::with_prefix("foo-")?; + /// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap(); + /// assert!(tmp_name.starts_with("foo-")); + /// # Ok(()) + /// # } + /// ``` + pub fn with_prefix>(prefix: S) -> io::Result { + Builder::new().prefix(&prefix).tempdir() + } + + /// Attempts to make a temporary directory with the specified prefix inside + /// the specified directory. The directory and everything inside it will be + /// automatically deleted once the returned `TempDir` is destroyed. + /// + /// # Errors + /// + /// If the directory can not be created, `Err` is returned. + /// + /// # Examples + /// + /// ``` + /// use std::fs::{self, File}; + /// use std::io::Write; + /// use tempfile::TempDir; + /// + /// # use std::io; + /// # fn run() -> Result<(), io::Error> { + /// // Create a directory inside of the current directory + /// let tmp_dir = TempDir::with_prefix_in("foo-", ".")?; + /// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap(); + /// assert!(tmp_name.starts_with("foo-")); + /// # Ok(()) + /// # } + /// ``` + pub fn with_prefix_in, P: AsRef>( + prefix: S, + dir: P, + ) -> io::Result { + Builder::new().prefix(&prefix).tempdir_in(dir) + } + /// Accesses the [`Path`] to the temporary directory. /// /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html diff --git a/src/file/mod.rs b/src/file/mod.rs index d0aa1507a..aca44ced5 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -608,12 +608,45 @@ impl NamedTempFile { /// Create a new named temporary file in the specified directory. /// + /// This is equivalent to: + /// + /// ```ignore + /// Builder::new().prefix(&prefix).tempfile() + /// ``` + /// /// See [`NamedTempFile::new()`] for details. /// /// [`NamedTempFile::new()`]: #method.new pub fn new_in>(dir: P) -> io::Result { Builder::new().tempfile_in(dir) } + + /// Create a new named temporary file with the specified filename prefix. + /// + /// See [`NamedTempFile::new()`] for details. + /// + /// [`NamedTempFile::new()`]: #method.new + pub fn with_prefix>(prefix: S) -> io::Result { + Builder::new().prefix(&prefix).tempfile() + } + /// Create a new named temporary file with the specified filename prefix, + /// in the specified directory. + /// + /// This is equivalent to: + /// + /// ```ignore + /// Builder::new().prefix(&prefix).tempfile_in(directory) + /// ``` + /// + /// See [`NamedTempFile::new()`] for details. + /// + /// [`NamedTempFile::new()`]: #method.new + pub fn with_prefix_in, P: AsRef>( + prefix: S, + dir: P, + ) -> io::Result { + Builder::new().prefix(&prefix).tempfile_in(dir) + } } impl NamedTempFile {