Skip to main content

qubit_fs/directory/
create_directory_outcome.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8// facade tests.
9//! Directory creation outcome.
10
11/// Result returned after a directory creation request.
12///
13/// # Examples
14///
15/// ```rust
16/// use qubit_fs::directory::CreateDirectoryOutcome;
17///
18/// let outcome = CreateDirectoryOutcome::new(false);
19/// assert!(!outcome.already_existed());
20/// ```
21#[derive(Clone, Copy, Debug, Eq, PartialEq)]
22pub struct CreateDirectoryOutcome {
23    /// Whether an existing directory satisfied the request.
24    already_existed: bool,
25    /// Number of ancestor directories created when the provider reports it.
26    created_ancestors: Option<u64>,
27}
28
29impl CreateDirectoryOutcome {
30    /// Creates an outcome. `already_existed` reports an accepted existing
31    /// directory.
32    #[inline]
33    #[must_use]
34    pub const fn new(already_existed: bool) -> Self {
35        Self {
36            already_existed,
37            created_ancestors: None,
38        }
39    }
40
41    /// Returns whether an existing directory satisfied the request.
42    #[inline]
43    #[must_use]
44    pub const fn already_existed(self) -> bool {
45        self.already_existed
46    }
47
48    /// Attaches the number of ancestor directories created, when known.
49    #[inline]
50    #[must_use]
51    pub const fn with_created_ancestors(mut self, count: u64) -> Self {
52        self.created_ancestors = Some(count);
53        self
54    }
55
56    /// Returns the number of created ancestor directories, when reported.
57    #[inline]
58    #[must_use]
59    pub const fn created_ancestors(self) -> Option<u64> {
60        self.created_ancestors
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use std::hint::black_box;
67
68    use super::CreateDirectoryOutcome;
69
70    #[test]
71    fn outcome_accessors_are_executed_at_runtime() {
72        let constructor: fn(bool) -> CreateDirectoryOutcome = black_box(CreateDirectoryOutcome::new);
73        let with_ancestors: fn(CreateDirectoryOutcome, u64) -> CreateDirectoryOutcome =
74            black_box(CreateDirectoryOutcome::with_created_ancestors);
75        let already_existed: fn(CreateDirectoryOutcome) -> bool = black_box(CreateDirectoryOutcome::already_existed);
76        let created_ancestors: fn(CreateDirectoryOutcome) -> Option<u64> =
77            black_box(CreateDirectoryOutcome::created_ancestors);
78
79        let outcome = with_ancestors(constructor(true), 2);
80        assert!(already_existed(outcome));
81        assert_eq!(Some(2), created_ancestors(outcome));
82    }
83}