recomp 0.3.1

Reusable components
Documentation
use object_store::{
    Result,
    aws::{AmazonS3, AmazonS3Builder},
};

use super::super::ObjectStorage;

/// Builder for Amazon S3 compatible object storage.
#[derive(Clone, Debug)]
pub struct S3ObjectStorageBuilder {
    name: String,
    builder: AmazonS3Builder,
}

impl S3ObjectStorageBuilder {
    pub(crate) fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            builder: AmazonS3Builder::from_env(),
        }
    }

    /// Sets the S3 bucket name.
    #[must_use]
    pub fn with_bucket_name(mut self, bucket_name: impl Into<String>) -> Self {
        self.builder = self.builder.with_bucket_name(bucket_name);
        self
    }

    /// Sets the S3 region.
    #[must_use]
    pub fn with_region(mut self, region: impl Into<String>) -> Self {
        self.builder = self.builder.with_region(region);
        self
    }

    /// Sets the S3-compatible endpoint.
    #[must_use]
    pub fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.builder = self.builder.with_endpoint(endpoint);
        self
    }

    /// Sets the S3 builder URL.
    #[must_use]
    pub fn with_url(mut self, url: impl Into<String>) -> Self {
        self.builder = self.builder.with_url(url);
        self
    }

    /// Configures the underlying provider builder.
    #[must_use]
    pub fn configure_store(
        mut self,
        configure: impl FnOnce(AmazonS3Builder) -> AmazonS3Builder,
    ) -> Self {
        self.builder = configure(self.builder);
        self
    }

    /// Builds the object storage component.
    pub fn build(self) -> Result<ObjectStorage<AmazonS3>> {
        self.builder
            .build()
            .map(|store| ObjectStorage::from_store(self.name, store))
    }
}