1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Handle temporary files and directories.

use std::{
    env,
    fs::{self, File, OpenOptions, ReadDir},
    io,
    path::{Path, PathBuf},
};

/// Handle a temporary directory.
///
/// The `path` parameter (present in some methods) must be
/// relative because it will be joined to the temporary
/// directory path.
///
/// When this struct is dropped, the temporary directory
/// itself is automatically deleted.
pub struct TmpDir(PathBuf);

impl TmpDir {
    /// Create a new TmpDir instance.
    pub fn new() -> Result<Self, io::Error> {
        let path = env::temp_dir().join(uuid::Uuid::new_v4().to_string());
        fs::create_dir(&path)?;
        Ok(TmpDir(path))
    }

    /// Path of the temporary directory.
    pub fn path(&self) -> PathBuf {
        self.0.clone()
    }

    /// Generate a new unique path in the temporary directory.
    pub fn unique_path(&self) -> PathBuf {
        self.0.join(uuid::Uuid::new_v4().to_string())
    }

    /// Write to a file (or create it if it doesn't exist)
    /// in the temporary directory. Akin to [`fs::write`].
    pub fn write_file<P, C>(&self, path: P, contents: C) -> io::Result<()>
    where
        P: AsRef<Path>,
        C: AsRef<[u8]>,
    {
        fs::write(self.0.join(path), contents)
    }

    /// Read a file in the temporary directory. Akin to
    /// [`fs::read`].
    pub fn read_file<P>(&self, path: P) -> io::Result<Vec<u8>>
    where
        P: AsRef<Path>,
    {
        fs::read(self.0.join(path))
    }

    /// Create a directory inside the temporary directory.
    /// Akin to [`fs::create_dir`].
    pub fn create_dir<P>(&self, path: P) -> io::Result<()>
    where
        P: AsRef<Path>,
    {
        fs::create_dir(self.0.join(path))
    }

    /// Create a directory and all of its parent components
    /// if they are missing. Akin to [`fs::create_dir_all`].
    pub fn create_dir_all<P>(&self, path: P) -> io::Result<()>
    where
        P: AsRef<Path>,
    {
        fs::create_dir_all(self.0.join(path))
    }

    /// Create a file in the temporary directory and open it
    /// in write-only mode. Akin to [`File::create`].
    pub fn create_file<P>(&self, path: P) -> io::Result<File>
    where
        P: AsRef<Path>,
    {
        File::create(self.0.join(path))
    }

    /// Open a file in the temporary directory in read-only
    /// mode. Akin to [`File::open`].
    pub fn open_readable<P>(&self, path: P) -> io::Result<File>
    where
        P: AsRef<Path>,
    {
        File::open(self.0.join(path))
    }

    /// Open a file in the temporary directory in write-only
    /// mode.
    pub fn open_writable<P>(&self, path: P) -> io::Result<File>
    where
        P: AsRef<Path>,
    {
        OpenOptions::new().write(true).open(self.0.join(path))
    }

    /// Open a file in the temporary directory using the
    /// provided OpenOptions. Akin to [`fs::OpenOptions::open`].
    pub fn open_with_opts<P>(&self, opts: &mut OpenOptions, path: P) -> io::Result<File>
    where
        P: AsRef<Path>,
    {
        opts.open(self.0.join(path))
    }

    /// Get metadata for the given path. Akin to [`fs::metadata`].
    pub fn metadata<P>(&self, path: P) -> io::Result<fs::Metadata>
    where
        P: AsRef<Path>,
    {
        self.0.join(path.as_ref()).metadata()
    }

    /// Check if a path exists in the current directory. Akin
    /// to [`Path::exists`].
    pub fn exists<P>(&self, path: P) -> bool
    where
        P: AsRef<Path>,
    {
        self.0.join(path.as_ref()).exists()
    }

    /// Read temporary directory. Akin to [`fs::read_dir`].
    pub fn read_dir(&self) -> io::Result<ReadDir> {
        fs::read_dir(&self.0)
    }
}

impl Drop for TmpDir {
    /// When TmpDir is dropped, the temporary directory
    /// itself is deleted.
    fn drop(&mut self) {
        fs::remove_dir_all(&self.0).ok();
    }
}

/// Handle a temporary file.
///
/// When this struct is dropped, the temporary file itself is
/// automatically deleted.
pub struct TmpFile(PathBuf);

impl TmpFile {
    /// Create a new TmpFile instance.
    pub fn new() -> Result<Self, io::Error> {
        let path = env::temp_dir().join(uuid::Uuid::new_v4().to_string());
        fs::write(&path, &[])?;
        Ok(TmpFile(path))
    }

    /// Path of the temporary file.
    pub fn path(&self) -> PathBuf {
        self.0.clone()
    }

    /// Write to the temporary file. Akin to [`fs::write`].
    pub fn write<C>(&self, contents: C) -> io::Result<()>
    where
        C: AsRef<[u8]>,
    {
        fs::write(&self.0, contents)
    }

    /// Read the temporary file. Akin to [`fs::read`].
    pub fn read(&self) -> io::Result<Vec<u8>> {
        fs::read(&self.0)
    }

    /// Open the temporary file in read-only mode. Akin to
    /// [`File::open`].
    pub fn open_readable(&self) -> io::Result<File> {
        File::open(&self.0)
    }

    /// Open the temporary file in write-only mode.
    pub fn open_writable(&self) -> io::Result<File> {
        OpenOptions::new().write(true).open(&self.0)
    }

    /// Open the temporary file using the provided OpenOptions.
    /// Akin to [`fs::OpenOptions::open`].
    pub fn open_with_opts(&self, opts: &mut OpenOptions) -> io::Result<File> {
        opts.open(&self.0)
    }
}

impl Drop for TmpFile {
    /// When TmpFile is dropped, the temporary file itself is
    /// deleted.
    fn drop(&mut self) {
        fs::remove_dir_all(&self.0).ok();
    }
}