Skip to main content

qubit_fs/temp/
temp_resources.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Facade helpers for temporary resources.
11
12use std::sync::Arc;
13
14use crate::{
15    FileSystem,
16    FsResult,
17    TempDir,
18    TempDirOptions,
19    TempFile,
20    TempFileOptions,
21};
22
23/// Namespace for temporary resource creation helpers.
24pub enum TempResources {}
25
26impl TempResources {
27    /// Creates a temporary file.
28    ///
29    /// # Parameters
30    /// - `fs`: Filesystem that will own the temporary path.
31    /// - `options`: Temporary file options.
32    ///
33    /// # Returns
34    /// Native or managed temporary file handle.
35    ///
36    /// # Errors
37    /// Returns [`crate::FsError`] when native temporary file creation fails or
38    /// the managed fallback cannot reserve the temporary file.
39    pub fn create_file(fs: Arc<dyn FileSystem>, options: &TempFileOptions) -> FsResult<Box<dyn TempFile>> {
40        fs.temp_resource_factory().create_file(fs.clone(), options)
41    }
42
43    /// Creates a temporary file with default options.
44    ///
45    /// # Parameters
46    /// - `fs`: Filesystem that will own the temporary path.
47    ///
48    /// # Returns
49    /// Temporary file handle.
50    ///
51    /// # Errors
52    /// Returns [`crate::FsError`] when the temporary file cannot be created.
53    pub fn create_default_file(fs: Arc<dyn FileSystem>) -> FsResult<Box<dyn TempFile>> {
54        Self::create_file(fs, &TempFileOptions::default())
55    }
56
57    /// Creates a temporary file with a custom name prefix.
58    ///
59    /// # Parameters
60    /// - `fs`: Filesystem that will own the temporary path.
61    /// - `prefix`: Name prefix.
62    ///
63    /// # Returns
64    /// Temporary file handle.
65    ///
66    /// # Errors
67    /// Returns [`crate::FsError`] when the temporary file cannot be created.
68    pub fn create_file_with_prefix(fs: Arc<dyn FileSystem>, prefix: &str) -> FsResult<Box<dyn TempFile>> {
69        Self::create_file(
70            fs,
71            &TempFileOptions {
72                prefix: prefix.to_owned(),
73                ..TempFileOptions::default()
74            },
75        )
76    }
77
78    /// Creates a temporary directory.
79    ///
80    /// # Parameters
81    /// - `fs`: Filesystem that will own the temporary path.
82    /// - `options`: Temporary directory options.
83    ///
84    /// # Returns
85    /// Native or managed temporary directory handle.
86    ///
87    /// # Errors
88    /// Returns [`crate::FsError`] when native temporary directory creation fails
89    /// or the managed fallback cannot create the temporary directory.
90    pub fn create_dir(fs: Arc<dyn FileSystem>, options: &TempDirOptions) -> FsResult<Box<dyn TempDir>> {
91        fs.temp_resource_factory().create_dir(fs.clone(), options)
92    }
93
94    /// Creates a temporary directory with default options.
95    ///
96    /// # Parameters
97    /// - `fs`: Filesystem that will own the temporary path.
98    ///
99    /// # Returns
100    /// Temporary directory handle.
101    ///
102    /// # Errors
103    /// Returns [`crate::FsError`] when the temporary directory cannot be
104    /// created.
105    pub fn create_default_dir(fs: Arc<dyn FileSystem>) -> FsResult<Box<dyn TempDir>> {
106        Self::create_dir(fs, &TempDirOptions::default())
107    }
108
109    /// Creates a temporary directory with a custom name prefix.
110    ///
111    /// # Parameters
112    /// - `fs`: Filesystem that will own the temporary path.
113    /// - `prefix`: Name prefix.
114    ///
115    /// # Returns
116    /// Temporary directory handle.
117    ///
118    /// # Errors
119    /// Returns [`crate::FsError`] when the temporary directory cannot be
120    /// created.
121    pub fn create_dir_with_prefix(fs: Arc<dyn FileSystem>, prefix: &str) -> FsResult<Box<dyn TempDir>> {
122        Self::create_dir(
123            fs,
124            &TempDirOptions {
125                prefix: prefix.to_owned(),
126                ..TempDirOptions::default()
127            },
128        )
129    }
130}