qubit-fs 0.2.2

Provider-neutral synchronous and asynchronous filesystem abstraction for Rust
Documentation
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
// facade tests.
//! Directory creation outcome.

/// Result returned after a directory creation request.
///
/// # Examples
///
/// ```rust
/// use qubit_fs::directory::CreateDirectoryOutcome;
///
/// let outcome = CreateDirectoryOutcome::new(false);
/// assert!(!outcome.already_existed());
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CreateDirectoryOutcome {
    /// Whether an existing directory satisfied the request.
    already_existed: bool,
    /// Number of ancestor directories created when the provider reports it.
    created_ancestors: Option<u64>,
}

impl CreateDirectoryOutcome {
    /// Creates an outcome. `already_existed` reports an accepted existing
    /// directory.
    #[inline]
    #[must_use]
    pub const fn new(already_existed: bool) -> Self {
        Self {
            already_existed,
            created_ancestors: None,
        }
    }

    /// Returns whether an existing directory satisfied the request.
    #[inline]
    #[must_use]
    pub const fn already_existed(self) -> bool {
        self.already_existed
    }

    /// Attaches the number of ancestor directories created, when known.
    #[inline]
    #[must_use]
    pub const fn with_created_ancestors(mut self, count: u64) -> Self {
        self.created_ancestors = Some(count);
        self
    }

    /// Returns the number of created ancestor directories, when reported.
    #[inline]
    #[must_use]
    pub const fn created_ancestors(self) -> Option<u64> {
        self.created_ancestors
    }
}

#[cfg(test)]
mod tests {
    use std::hint::black_box;

    use super::CreateDirectoryOutcome;

    #[test]
    fn outcome_accessors_are_executed_at_runtime() {
        let constructor: fn(bool) -> CreateDirectoryOutcome = black_box(CreateDirectoryOutcome::new);
        let with_ancestors: fn(CreateDirectoryOutcome, u64) -> CreateDirectoryOutcome =
            black_box(CreateDirectoryOutcome::with_created_ancestors);
        let already_existed: fn(CreateDirectoryOutcome) -> bool = black_box(CreateDirectoryOutcome::already_existed);
        let created_ancestors: fn(CreateDirectoryOutcome) -> Option<u64> =
            black_box(CreateDirectoryOutcome::created_ancestors);

        let outcome = with_ancestors(constructor(true), 2);
        assert!(already_existed(outcome));
        assert_eq!(Some(2), created_ancestors(outcome));
    }
}