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.
// =============================================================================
//! Temporary resource creation options.

use crate::path::Path;

/// Options shared by temporary file and temporary directory creation.
///
/// # Examples
///
/// ```rust
/// use qubit_fs::temp::TempOptions;
///
/// assert_eq!(TempOptions::default(), TempOptions::new());
/// ```
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TempOptions {
    /// Parent directory or prefix for the temporary resource.
    parent: Option<Path>,
    /// Generated resource name prefix.
    prefix: String,
    /// Generated resource name suffix.
    suffix: String,
    /// Whether a missing parent directory is created.
    create_parent: bool,
}

impl TempOptions {
    /// Creates empty temporary-resource options without parent creation.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            parent: None,
            prefix: String::new(),
            suffix: String::new(),
            create_parent: false,
        }
    }

    /// Returns the optional parent directory or prefix.
    #[inline]
    #[must_use]
    pub const fn parent(&self) -> Option<&Path> {
        self.parent.as_ref()
    }

    /// Returns the generated resource name prefix.
    #[inline]
    #[must_use]
    pub fn prefix(&self) -> &str {
        &self.prefix
    }

    /// Returns the generated resource name suffix.
    #[inline]
    #[must_use]
    pub fn suffix(&self) -> &str {
        &self.suffix
    }

    /// Returns whether missing parent directories are created.
    #[inline]
    #[must_use]
    pub const fn creates_parent(&self) -> bool {
        self.create_parent
    }

    /// Replaces the optional parent directory or prefix.
    #[inline]
    #[must_use]
    pub fn with_parent(mut self, parent: Option<Path>) -> Self {
        self.parent = parent;
        self
    }

    /// Replaces the generated resource name prefix.
    #[inline]
    #[must_use]
    pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.prefix = prefix.into();
        self
    }

    /// Replaces the generated resource name suffix.
    #[inline]
    #[must_use]
    pub fn with_suffix(mut self, suffix: impl Into<String>) -> Self {
        self.suffix = suffix.into();
        self
    }

    /// Replaces whether missing parent directories are created.
    #[inline]
    #[must_use]
    pub const fn with_create_parent(mut self, create: bool) -> Self {
        self.create_parent = create;
        self
    }
}

impl Default for TempOptions {
    /// Creates empty temporary-resource options without parent creation.
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::TempOptions;
    use crate::path::Path;

    #[test]
    fn option_accessors_are_executed_at_runtime() {
        let parent = Path::parse("/tmp").expect("valid parent path");
        let options = TempOptions::new()
            .with_parent(Some(parent.clone()))
            .with_prefix("prefix")
            .with_suffix("suffix")
            .with_create_parent(true);

        assert_eq!(options.parent(), Some(&parent));
        assert_eq!(options.prefix(), "prefix");
        assert_eq!(options.suffix(), "suffix");
        assert!(options.creates_parent());
    }
}