shared_files/
temp_file.rs

1//! Implementations for [`TempFile`](TempFile).
2
3use crate::errors::CompleteWritingError;
4use crate::{
5    AsyncNewFile, FilePath, SharedFile, SharedFileReader, SharedFileType, SharedFileWriter,
6};
7use async_tempfile::{Ownership, TempFile};
8use std::ops::Deref;
9use std::path::PathBuf;
10use tokio::fs::File;
11use uuid::Uuid;
12
13/// A type alias for a [`SharedFile`] wrapping a [`TempFile`].
14pub type SharedTemporaryFile = SharedFile<TempFile>;
15
16/// A type alias for a [`SharedFileReader`] wrapping a [`TempFile`].
17pub type SharedTemporaryFileReader = SharedFileReader<TempFile>;
18
19/// A type alias for a [`SharedFileWriter`] wrapping a [`TempFile`].
20pub type SharedTemporaryFileWriter = SharedFileWriter<TempFile>;
21
22#[async_trait::async_trait]
23impl SharedFileType for TempFile {
24    type Type = TempFile;
25    type OpenError = async_tempfile::Error;
26    type SyncError = CompleteWritingError;
27
28    async fn open_ro(&self) -> Result<Self::Type, Self::OpenError> {
29        self.open_ro().await
30    }
31
32    async fn open_rw(&self) -> Result<Self::Type, Self::OpenError> {
33        self.open_rw().await
34    }
35
36    async fn sync_all(&self) -> Result<(), Self::SyncError> {
37        let file: &File = self.deref();
38        Ok(file.sync_all().await?)
39    }
40
41    async fn sync_data(&self) -> Result<(), Self::SyncError> {
42        let file: &File = self.deref();
43        Ok(file.sync_data().await?)
44    }
45}
46
47#[async_trait::async_trait]
48impl AsyncNewFile for TempFile {
49    type Target = TempFile;
50    type Error = async_tempfile::Error;
51
52    async fn new_async() -> Result<Self::Target, Self::Error> {
53        TempFile::new().await
54    }
55}
56
57impl FilePath for TempFile {
58    fn file_path(&self) -> &PathBuf {
59        self.file_path()
60    }
61}
62
63impl SharedTemporaryFile {
64    /// Creates a new temporary file in the default location.
65    /// Convenience wrapper around [`TempFile::new_with_uuid`] and [`SharedFile::from`].
66    ///
67    /// ## Arguments
68    ///
69    /// * `uuid` - A UUID to use as a suffix to the file name.
70    pub async fn new_with_uuid(uuid: Uuid) -> Result<Self, async_tempfile::Error> {
71        let file = TempFile::new_with_uuid(uuid).await?;
72        Ok(Self::from(file))
73    }
74
75    /// Wraps a new instance of this type around an existing file. This is a convenience
76    /// wrapper around [`TempFile::from_existing`] and [`SharedFile::from`].
77    ///
78    /// If `ownership` is set to [`Ownership::Borrowed`], this method does not take ownership of
79    /// the file, i.e. the file will not be deleted when the instance is dropped.
80    ///
81    /// ## Arguments
82    ///
83    /// * `path` - The path of the file to wrap.
84    /// * `ownership` - The ownership of the file.
85    pub async fn from_existing(
86        path: PathBuf,
87        ownership: Ownership,
88    ) -> Result<SharedFile<TempFile>, async_tempfile::Error> {
89        let file = TempFile::from_existing(path, ownership).await?;
90        Ok(Self::from(file))
91    }
92
93    /// Returns the path of the underlying temporary file.
94    pub fn file_path(&self) -> &PathBuf {
95        self.sentinel.original.file_path()
96    }
97}