Skip to main content

qubit_fs/temp/
temp_dir.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//! Temporary directory handle trait.
11
12use crate::{
13    CreateDirOptions,
14    DirectoryStream,
15    FileResource,
16    FsPath,
17    FsResult,
18    ListOptions,
19    PersistOptions,
20    TempResource,
21};
22
23/// Temporary directory handle with cleanup responsibility.
24pub trait TempDir: TempResource {
25    /// Lists child entries under the temporary directory.
26    ///
27    /// # Parameters
28    /// - `options`: Listing options.
29    ///
30    /// # Returns
31    /// Directory stream opened by the owning filesystem.
32    ///
33    /// # Errors
34    /// Returns [`crate::FsError`] when the owning filesystem cannot list this
35    /// temporary directory.
36    fn list(&self, options: &ListOptions) -> FsResult<Box<dyn DirectoryStream>> {
37        self.fs().as_ref().list(self.path(), options)
38    }
39
40    /// Builds a child resource under the temporary directory.
41    ///
42    /// # Parameters
43    /// - `name`: Provider-local child name or relative path segment.
44    ///
45    /// # Returns
46    /// Child resource bound to the same filesystem.
47    ///
48    /// # Errors
49    /// Returns [`crate::FsError`] when the child path cannot be joined.
50    fn child(&self, name: &str) -> FsResult<FileResource> {
51        Ok(FileResource::new(self.fs(), self.path().join(name)?))
52    }
53
54    /// Creates and returns a child directory under this temporary directory.
55    ///
56    /// # Parameters
57    /// - `name`: Child directory name or relative path segment.
58    /// - `options`: Directory creation options.
59    ///
60    /// # Returns
61    /// Child directory resource bound to the same filesystem.
62    ///
63    /// # Errors
64    /// Returns [`crate::FsError`] when the child path cannot be joined or the
65    /// directory cannot be created.
66    fn create_child_dir(&self, name: &str, options: &CreateDirOptions) -> FsResult<FileResource> {
67        let child = self.child(name)?;
68        child.create_dir(options)?;
69        Ok(child)
70    }
71
72    /// Persists the temporary directory to a target path.
73    ///
74    /// # Parameters
75    /// - `target`: Final target path.
76    /// - `options`: Persistence options.
77    ///
78    /// # Errors
79    /// Returns [`crate::FsError`] when persistence fails.
80    fn persist(self: Box<Self>, target: &FsPath, options: &PersistOptions) -> FsResult<()>;
81}