Skip to main content

file_storage/path/construct/
temp.rs

1use crate::{Error, FilePath, FolderPath, StoragePath};
2use std::io;
3use tempfile::NamedTempFile;
4
5impl StoragePath {
6    //! Temp
7
8    /// Creates a temp path.
9    pub fn temp() -> Result<StoragePath, io::Error> {
10        let temp_file: NamedTempFile = NamedTempFile::new()?;
11        if let Some(path) = temp_file.path().to_str() {
12            Ok(StoragePath::parse(path)?)
13        } else {
14            Err(Error::path_not_utf8(temp_file.path()))
15        }
16    }
17}
18
19impl FilePath {
20    //! Temp
21
22    /// Creates a temp file path. (does not create the file)
23    pub fn temp() -> Result<FilePath, io::Error> {
24        StoragePath::temp()?.to_file().map_err(Into::into)
25    }
26}
27
28impl FolderPath {
29    //! Temp
30
31    /// Creates a temp folder path. (does not create the folder)
32    pub fn temp() -> Result<FolderPath, io::Error> {
33        Ok(StoragePath::temp()?.make_folder())
34    }
35}