recomp 0.4.0

Reusable components
Documentation
use std::path::{Path, PathBuf};

use object_store::{Error as ObjectStoreError, local::LocalFileSystem};

use super::super::ObjectStorage;

/// Builder for local filesystem object storage.
#[derive(Clone, Debug)]
pub struct LocalObjectStorageBuilder {
    name: String,
    root: PathBuf,
    automatic_cleanup: bool,
    fsync: bool,
}

impl LocalObjectStorageBuilder {
    pub(crate) fn new(name: impl Into<String>, root: impl AsRef<Path>) -> Self {
        Self {
            name: name.into(),
            root: root.as_ref().to_path_buf(),
            automatic_cleanup: false,
            fsync: false,
        }
    }

    /// Enables cleanup of empty parent directories after deletes.
    #[must_use]
    pub const fn with_automatic_cleanup(mut self, automatic_cleanup: bool) -> Self {
        self.automatic_cleanup = automatic_cleanup;
        self
    }

    /// Enables filesystem synchronization after writes.
    #[must_use]
    pub const fn with_fsync(mut self, fsync: bool) -> Self {
        self.fsync = fsync;
        self
    }

    /// Builds the object storage component.
    pub fn build(self) -> Result<ObjectStorage<LocalFileSystem>, ObjectStoreError> {
        let store = LocalFileSystem::new_with_prefix(self.root)?
            .with_automatic_cleanup(self.automatic_cleanup)
            .with_fsync(self.fsync);

        Ok(ObjectStorage::from_store(self.name, store))
    }
}