Skip to content

Commit b4a2f64

Browse files
authored
feat: add convenience methods for creating temp files with prefixes (#250)
- `TempDir::with_prefix(prefix)` - `TempDir::with_prefix_in(prefix, directory)` - `TempFile::with_prefix(prefix)` - `TempFile::with_prefix_in(prefix, directory)` fixes #249
1 parent f994e9f commit b4a2f64

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

src/dir.rs

+60
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::ffi::OsStr;
1112
use std::fs::remove_dir_all;
1213
use std::mem;
1314
use std::path::{self, Path, PathBuf};
@@ -264,6 +265,65 @@ impl TempDir {
264265
Builder::new().tempdir_in(dir)
265266
}
266267

268+
/// Attempts to make a temporary directory with the specified prefix inside of
269+
/// `env::temp_dir()`. The directory and everything inside it will be automatically
270+
/// deleted once the returned `TempDir` is destroyed.
271+
///
272+
/// # Errors
273+
///
274+
/// If the directory can not be created, `Err` is returned.
275+
///
276+
/// # Examples
277+
///
278+
/// ```
279+
/// use std::fs::{self, File};
280+
/// use std::io::Write;
281+
/// use tempfile::TempDir;
282+
///
283+
/// # use std::io;
284+
/// # fn run() -> Result<(), io::Error> {
285+
/// // Create a directory inside of the current directory
286+
/// let tmp_dir = TempDir::with_prefix("foo-")?;
287+
/// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
288+
/// assert!(tmp_name.starts_with("foo-"));
289+
/// # Ok(())
290+
/// # }
291+
/// ```
292+
pub fn with_prefix<S: AsRef<OsStr>>(prefix: S) -> io::Result<TempDir> {
293+
Builder::new().prefix(&prefix).tempdir()
294+
}
295+
296+
/// Attempts to make a temporary directory with the specified prefix inside
297+
/// the specified directory. The directory and everything inside it will be
298+
/// automatically deleted once the returned `TempDir` is destroyed.
299+
///
300+
/// # Errors
301+
///
302+
/// If the directory can not be created, `Err` is returned.
303+
///
304+
/// # Examples
305+
///
306+
/// ```
307+
/// use std::fs::{self, File};
308+
/// use std::io::Write;
309+
/// use tempfile::TempDir;
310+
///
311+
/// # use std::io;
312+
/// # fn run() -> Result<(), io::Error> {
313+
/// // Create a directory inside of the current directory
314+
/// let tmp_dir = TempDir::with_prefix_in("foo-", ".")?;
315+
/// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap();
316+
/// assert!(tmp_name.starts_with("foo-"));
317+
/// # Ok(())
318+
/// # }
319+
/// ```
320+
pub fn with_prefix_in<S: AsRef<OsStr>, P: AsRef<Path>>(
321+
prefix: S,
322+
dir: P,
323+
) -> io::Result<TempDir> {
324+
Builder::new().prefix(&prefix).tempdir_in(dir)
325+
}
326+
267327
/// Accesses the [`Path`] to the temporary directory.
268328
///
269329
/// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html

src/file/mod.rs

+33
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,45 @@ impl NamedTempFile<File> {
608608

609609
/// Create a new named temporary file in the specified directory.
610610
///
611+
/// This is equivalent to:
612+
///
613+
/// ```ignore
614+
/// Builder::new().prefix(&prefix).tempfile()
615+
/// ```
616+
///
611617
/// See [`NamedTempFile::new()`] for details.
612618
///
613619
/// [`NamedTempFile::new()`]: #method.new
614620
pub fn new_in<P: AsRef<Path>>(dir: P) -> io::Result<NamedTempFile> {
615621
Builder::new().tempfile_in(dir)
616622
}
623+
624+
/// Create a new named temporary file with the specified filename prefix.
625+
///
626+
/// See [`NamedTempFile::new()`] for details.
627+
///
628+
/// [`NamedTempFile::new()`]: #method.new
629+
pub fn with_prefix<S: AsRef<OsStr>>(prefix: S) -> io::Result<NamedTempFile> {
630+
Builder::new().prefix(&prefix).tempfile()
631+
}
632+
/// Create a new named temporary file with the specified filename prefix,
633+
/// in the specified directory.
634+
///
635+
/// This is equivalent to:
636+
///
637+
/// ```ignore
638+
/// Builder::new().prefix(&prefix).tempfile_in(directory)
639+
/// ```
640+
///
641+
/// See [`NamedTempFile::new()`] for details.
642+
///
643+
/// [`NamedTempFile::new()`]: #method.new
644+
pub fn with_prefix_in<S: AsRef<OsStr>, P: AsRef<Path>>(
645+
prefix: S,
646+
dir: P,
647+
) -> io::Result<NamedTempFile> {
648+
Builder::new().prefix(&prefix).tempfile_in(dir)
649+
}
617650
}
618651

619652
impl<F> NamedTempFile<F> {

tests/namedtempfile.rs

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ fn exists<P: AsRef<Path>>(path: P) -> bool {
1111
std::fs::metadata(path.as_ref()).is_ok()
1212
}
1313

14+
#[test]
15+
fn test_prefix() {
16+
let tmpfile = NamedTempFile::with_prefix("prefix").unwrap();
17+
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
18+
assert!(name.starts_with("prefix"));
19+
}
20+
1421
#[test]
1522
fn test_basic() {
1623
let mut tmpfile = NamedTempFile::new().unwrap();

tests/tempdir.rs

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ fn test_tempdir() {
5151
assert!(!path.exists());
5252
}
5353

54+
fn test_prefix() {
55+
let tmpfile = TempDir::with_prefix_in("prefix", ".").unwrap();
56+
let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
57+
assert!(name.starts_with("prefix"));
58+
}
59+
5460
fn test_customnamed() {
5561
let tmpfile = Builder::new()
5662
.prefix("prefix")
@@ -184,6 +190,7 @@ pub fn pass_as_asref_path() {
184190
#[test]
185191
fn main() {
186192
in_tmpdir(test_tempdir);
193+
in_tmpdir(test_prefix);
187194
in_tmpdir(test_customnamed);
188195
in_tmpdir(test_rm_tempdir);
189196
in_tmpdir(test_rm_tempdir_close);

0 commit comments

Comments
 (0)