qubit-fs 0.3.3

Provider-neutral synchronous and asynchronous filesystem abstraction for Rust
Documentation
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Runtime-neutral asynchronous temporary-file facade handle.

use std::pin::Pin;

use crate::AsyncFileSystem;
use crate::error::FsError;
use crate::error::FsErrorKind;
use crate::error::FsOperation;
use crate::error::FsResult;
use crate::metadata::AchievedAtomicity;
use crate::metadata::AtomicityRequirement;
use crate::path::Path;
use crate::path::PathComponent;
use crate::path::RelativePath;
use crate::spi::AsyncTempResourceSpi;
use crate::spi::PersistRequest;
use crate::spi::SpiFuture;
use crate::temp::PersistFailure;
use crate::temp::PersistFailureState;
use crate::temp::PersistOptions;
use crate::temp::PersistOutcome;
use crate::temp::TempResourceState;
use crate::temp::internal::TempLifecycle;

/// A facade-owned asynchronous temporary file.
///
/// # Examples
///
/// This example uses an isolated in-memory provider fixture.
///
/// ```rust
/// # mod support { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/common/rustdoc_support.rs")); }
/// # use support::*;
/// # let (filesystem, _) = async_recording_spi::async_recording_file_system(Default::default());
/// # poll_support::ready(async {
/// use qubit_fs::temp::TempOptions;
/// use qubit_fs::temp::TempResourceState;
///
/// let mut temporary = filesystem.create_temp_file(TempOptions::default()).await?;
/// temporary.cleanup().await?;
/// assert_eq!(TempResourceState::Cleaned, temporary.state());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// # }).unwrap();
/// ```
pub struct AsyncTempFile {
    /// Facade that owns validation and persistence policy.
    file_system: AsyncFileSystem,
    /// Provider-local temporary path.
    path: Path,
    /// Pinned provider lifecycle session.
    session: Pin<Box<dyn AsyncTempResourceSpi>>,
    /// Current cleanup and publication lifecycle state.
    lifecycle: TempLifecycle,
    /// Human-readable resource kind used in lifecycle diagnostics.
    resource_name: &'static str,
}

impl AsyncTempFile {
    /// Binds a validated provider temporary session to its owning facade.
    ///
    /// # Parameters
    /// - `file_system`: Facade that owns validation and persistence policy.
    /// - `path`: Validated provider-local temporary path.
    /// - `session`: Provider lifecycle session.
    /// - `resource_name`: Human-readable resource kind used in lifecycle
    ///   diagnostics.
    ///
    /// # Returns
    /// An owned asynchronous temporary-file handle.
    pub(crate) fn new(
        file_system: AsyncFileSystem,
        path: Path,
        session: Box<dyn AsyncTempResourceSpi>,
        resource_name: &'static str,
    ) -> Self {
        Self {
            file_system,
            path,
            session: Box::into_pin(session),
            lifecycle: TempLifecycle::new(),
            resource_name,
        }
    }

    /// Returns the provider-local temporary path.
    ///
    /// # Returns
    /// The validated path supplied by the provider.
    #[inline]
    #[must_use]
    pub const fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the current ownership lifecycle state.
    ///
    /// # Returns
    /// The handle's current cleanup and publication state.
    #[inline]
    pub const fn state(&self) -> TempResourceState {
        self.lifecycle.state()
    }

    /// Returns one lexically safe child path.
    ///
    /// # Returns
    /// The path formed by appending `component` to this temporary file path.
    ///
    /// # Parameters
    /// - `component`: Validated component appended to the temporary path.
    #[inline]
    #[must_use]
    pub fn child(&self, component: &PathComponent) -> Path {
        self.path.child(component)
    }

    /// Returns one lexically safe descendant path.
    ///
    /// # Returns
    /// The path formed by joining `relative` to this temporary file path.
    ///
    /// # Parameters
    /// - `relative`: Validated relative path joined below the temporary path.
    #[inline]
    #[must_use]
    pub fn descendant(&self, relative: &RelativePath) -> Path {
        self.path.join(relative)
    }

    /// Asynchronously confirms cleanup of this temporary resource.
    ///
    /// Dropping an unpolled cleanup future changes nothing. Cancelling cleanup
    /// after it returns `Pending` leaves source authority indeterminate while
    /// preserving whether the target was already known to be published or not
    /// published. Later `persist` or `keep` calls are rejected and retain that
    /// distinction in their failure snapshot. This does not authorize another
    /// source mutation.
    ///
    /// # Returns
    /// A future resolving after provider cleanup is confirmed.
    ///
    /// # Errors
    /// Resolves to an invalid-state error when cleanup is no longer legal, or
    /// to the provider cleanup failure.
    #[inline]
    pub fn cleanup(&mut self) -> SpiFuture<'_, FsResult<()>> {
        self.lifecycle("cannot be cleaned now", FsOperation::CleanupTemp, |session| {
            session.cleanup()
        })
    }

    /// Asynchronously publishes this temporary resource to a generated target.
    ///
    /// # Returns
    /// A future resolving to the provider's confirmed publication outcome.
    ///
    /// # Errors
    /// Resolves to an invalid-state error when the resource is no longer owned,
    /// or to the provider ownership-transfer failure. An invalid-state failure
    /// retains any previously confirmed publication target and recovery state.
    #[inline]
    pub fn keep(&mut self) -> SpiFuture<'_, Result<PersistOutcome, PersistFailure>> {
        if self.lifecycle.state() != TempResourceState::Owned {
            let error = self.invalid_state(FsOperation::KeepTemp, "cannot be kept now");
            return Box::pin(async move {
                Err(PersistFailure::new(error, self.lifecycle.failure_state())
                    .with_publication_target(self.lifecycle.publication_target()))
            });
        }
        Box::pin(async move {
            self.lifecycle.begin_pending();
            match self.session.as_mut().keep().await {
                Ok(outcome) => {
                    if let Err(error) = self.file_system.validate_temp_keep_target(&self.path, outcome.target()) {
                        return Err(PersistFailure::new(error, PersistFailureState::Indeterminate));
                    }
                    self.path = outcome.target().clone();
                    self.lifecycle.record_success(true, outcome.target().clone());
                    Ok(outcome)
                }
                Err(failure) => {
                    let (error, state) = failure.into_parts();
                    self.lifecycle.record_failure(state, error.target().cloned(), true);
                    let target = self.path.clone();
                    Err(PersistFailure::new(
                        error.with_operation(FsOperation::KeepTemp).with_missing_context(
                            &self.path,
                            Some(&target),
                            self.file_system.properties().info().provider_id(),
                        ),
                        state,
                    )
                    .with_publication_target(self.lifecycle.publication_target()))
                }
            }
        })
    }

    /// Asynchronously persists this resource to a validated destination.
    ///
    /// # Parameters
    /// - `target`: Validated destination path.
    /// - `options`: Persistence atomicity and publication requirements.
    ///
    /// # Returns
    /// A future resolving to the confirmed persistence outcome.
    ///
    /// # Errors
    /// Resolves to a typed failure for invalid lifecycle state, failed local
    /// preflight, provider failure, or provider contract violation. Rejected
    /// repeated calls preserve the previously confirmed publication facts.
    pub fn persist<'a>(
        &'a mut self,
        target: &'a Path,
        options: PersistOptions,
    ) -> SpiFuture<'a, Result<PersistOutcome, PersistFailure>> {
        if self.lifecycle.state() != TempResourceState::Owned {
            let error = self
                .invalid_state(FsOperation::PersistTemp, "cannot be persisted now")
                .with_target(target.clone());
            return Box::pin(async move {
                Err(PersistFailure::new(error, self.lifecycle.failure_state())
                    .with_publication_target(self.lifecycle.publication_target()))
            });
        }
        if let Err(error) = self.file_system.preflight_temp_persist(&self.path, target, &options) {
            return Box::pin(async move { Err(PersistFailure::new(error, PersistFailureState::NotPublished)) });
        }
        Box::pin(async move {
            self.lifecycle.begin_pending();
            let atomicity = options.atomicity();
            let result = self
                .session
                .as_mut()
                .persist(PersistRequest::new(target, options))
                .await;
            match &result {
                Ok(outcome)
                    if outcome.target() == target
                        && !(atomicity == AtomicityRequirement::Required
                            && outcome.atomicity() != AchievedAtomicity::Atomic) =>
                {
                    self.lifecycle.record_success(false, outcome.target().clone());
                }
                Ok(outcome) if outcome.target() != target => {
                    self.lifecycle
                        .record_failure(PersistFailureState::Indeterminate, Some(target.clone()), false);
                }
                Ok(_) => self.lifecycle.record_failure(
                    PersistFailureState::PublishedSourceIndeterminate,
                    Some(target.clone()),
                    false,
                ),
                Err(failure) => self
                    .lifecycle
                    .record_failure(failure.state(), Some(target.clone()), false),
            }
            match result {
                Ok(outcome) if outcome.target() != target => Err(PersistFailure::new(
                    FsError::new(
                        FsErrorKind::ProviderContractViolation,
                        FsOperation::PersistTemp,
                        "provider reported a persistence target different from the request",
                    )
                    .with_path(self.path.clone())
                    .with_target(target.clone())
                    .with_provider(self.file_system.properties().info().provider_id()),
                    PersistFailureState::Indeterminate,
                )),
                Ok(outcome)
                    if atomicity == AtomicityRequirement::Required
                        && outcome.atomicity() != AchievedAtomicity::Atomic =>
                {
                    Err(PersistFailure::new(
                        FsError::new(
                            FsErrorKind::ProviderContractViolation,
                            FsOperation::PersistTemp,
                            "provider reported non-atomic success for atomic-required persist",
                        )
                        .with_path(self.path.clone())
                        .with_target(target.clone())
                        .with_provider(self.file_system.properties().info().provider_id()),
                        PersistFailureState::PublishedSourceIndeterminate,
                    )
                    .with_publication_target(self.lifecycle.publication_target()))
                }
                Err(failure) => {
                    let (error, state) = failure.into_parts();
                    Err(PersistFailure::new(self.contextual_persist_error(error, target), state)
                        .with_publication_target(self.lifecycle.publication_target()))
                }
                Ok(outcome) => Ok(outcome),
            }
        })
    }

    /// Runs one lifecycle operation while retaining an indeterminate
    /// cancellation state.
    ///
    /// # Type Parameters
    /// - `F`: One-shot provider lifecycle operation.
    ///
    /// # Parameters
    /// - `action`: Resource-specific action text used when the lifecycle state
    ///   rejects the operation.
    /// - `operation`: Filesystem operation recorded in generated errors.
    /// - `call`: Provider operation invoked after local state validation.
    ///
    /// # Returns
    /// A future resolving to the provider lifecycle result.
    ///
    /// # Errors
    /// Resolves to an invalid-state error or the provider lifecycle failure.
    fn lifecycle<'a, F>(
        &'a mut self,
        action: &'static str,
        operation: FsOperation,
        call: F,
    ) -> SpiFuture<'a, FsResult<()>>
    where
        F: FnOnce(Pin<&'a mut dyn AsyncTempResourceSpi>) -> SpiFuture<'a, FsResult<()>> + Send + 'a,
    {
        if !matches!(
            self.lifecycle.state(),
            TempResourceState::Owned | TempResourceState::CleanupRequired
        ) {
            let error = self.invalid_state(operation, action);
            return Box::pin(async move { Err(error) });
        }
        Box::pin(async move {
            let previous_lifecycle = self.lifecycle.clone();
            self.lifecycle.begin_cleanup_pending();
            let result = call(self.session.as_mut()).await;
            self.lifecycle = previous_lifecycle;
            match &result {
                Ok(()) => self.lifecycle.record_cleanup_success(),
                Err(error) => self.lifecycle.record_cleanup_error(error),
            }
            result.map_err(|error| {
                error.with_operation(operation).with_missing_context(
                    &self.path,
                    None,
                    self.file_system.properties().info().provider_id(),
                )
            })
        })
    }

    /// Builds an invalid-state error for this handle.
    ///
    /// # Parameters
    /// - `operation`: Rejected lifecycle operation.
    /// - `action`: Stable action text describing the rejected operation.
    ///
    /// # Returns
    /// A contextual invalid-state error containing the temporary path and
    /// provider.
    fn invalid_state(&self, operation: FsOperation, action: &str) -> FsError {
        let message = format!("{} {}", self.resource_name, action);
        FsError::new(FsErrorKind::InvalidState, operation, &message)
            .with_path(self.path.clone())
            .with_provider(self.file_system.properties().info().provider_id())
    }

    /// Adds only missing facade facts to a provider persistence error.
    ///
    /// # Parameters
    /// - `error`: Provider persistence error.
    /// - `target`: Requested persistence target.
    ///
    /// # Returns
    /// The error enriched with missing operation, path, target, and provider
    /// context.
    fn contextual_persist_error(&self, error: FsError, target: &Path) -> FsError {
        error.with_operation(FsOperation::PersistTemp).with_missing_context(
            &self.path,
            Some(target),
            self.file_system.properties().info().provider_id(),
        )
    }
}

impl Drop for AsyncTempFile {
    /// Invokes provider-local cancellation when this handle still owns it.
    ///
    /// This nonblocking hook does not start asynchronous cleanup or confirm
    /// that remote cleanup completed.
    fn drop(&mut self) {
        if matches!(
            self.lifecycle.state(),
            TempResourceState::Owned | TempResourceState::CleanupRequired
        ) {
            self.session.as_mut().cancel_on_drop();
        }
    }
}