minio 0.4.0

MinIO SDK for Amazon S3 compatible object storage access
Documentation
// MinIO Rust Library for Amazon S3 Compatible Cloud Storage
// Copyright 2025 MinIO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::s3::client::MinioClient;
use crate::s3::error::ValidationErr;
use crate::s3::multimap_ext::Multimap;
use crate::s3::response::PutBucketVersioningResponse;
use crate::s3::segmented_bytes::SegmentedBytes;
use crate::s3::types::{BucketName, Region, S3Api, S3Request, ToS3Request};
use crate::s3::utils::insert;
use bytes::Bytes;
use http::Method;
use std::fmt;
use std::sync::Arc;
use typed_builder::TypedBuilder;

/// Represents the versioning state of an S3 bucket.
///
/// This enum corresponds to the possible values returned by the
/// `GetBucketVersioning` API call in S3-compatible services.
///
/// # Variants
///
/// - `Enabled`: Object versioning is enabled for the bucket.
/// - `Suspended`: Object versioning is suspended for the bucket.
#[derive(Clone, Debug, PartialEq)]
pub enum VersioningStatus {
    /// Object versioning is enabled for the bucket.
    Enabled,
    /// Object versioning is suspended for the bucket.
    Suspended,
}

impl fmt::Display for VersioningStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            VersioningStatus::Enabled => write!(f, "Enabled"),
            VersioningStatus::Suspended => write!(f, "Suspended"),
        }
    }
}

/// Argument builder for the [`PutBucketVersioning`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketVersioning.html) S3 API operation.
///
/// This struct constructs the parameters required for the [`Client::put_bucket_versioning`](crate::s3::client::MinioClient::put_bucket_versioning) method.
#[derive(Clone, Debug, TypedBuilder)]
pub struct PutBucketVersioning {
    /// The S3 client instance used to send the request.
    #[builder(!default)] // force required
    client: MinioClient,

    /// Optional additional HTTP headers to include in the request.
    #[builder(default, setter(into))]
    extra_headers: Option<Multimap>,

    /// Optional additional query parameters to include in the request URL.
    #[builder(default, setter(into))]
    extra_query_params: Option<Multimap>,

    /// Optional AWS region to override the client's default region.
    #[builder(default, setter(into))]
    region: Option<Region>,

    /// The name of the bucket for which to configure versioning.
    #[builder(setter(into), !default)]
    bucket: BucketName,

    /// Desired versioning status for the bucket.
    ///
    /// - `VersioningStatus::Enabled`: Enables versioning.
    /// - `VersioningStatus::Suspended`: Suspends versioning.
    #[builder(!default, setter(into))]
    versioning_status: VersioningStatus,

    /// Specifies whether MFA delete is enabled for the bucket.
    ///
    /// - `Some(true)`: Enables MFA delete.
    /// - `Some(false)`: Disables MFA delete.
    /// - `None`: No change to the current MFA delete setting.
    #[builder(default)]
    mfa_delete: Option<bool>,
}

/// Builder type for [`PutBucketVersioning`] that is returned by [`MinioClient::put_bucket_versioning`](crate::s3::client::MinioClient::put_bucket_versioning).
///
/// This type alias simplifies the complex generic signature generated by the `typed_builder` crate.
pub type PutBucketVersioningBldr = PutBucketVersioningBuilder<(
    (MinioClient,),
    (),
    (),
    (),
    (BucketName,),
    (VersioningStatus,),
    (),
)>;

impl S3Api for PutBucketVersioning {
    type S3Response = PutBucketVersioningResponse;
}

impl ToS3Request for PutBucketVersioning {
    fn to_s3request(self) -> Result<S3Request, ValidationErr> {
        let data: String = {
            let mut data = "<VersioningConfiguration>".to_string();

            if let Some(v) = self.mfa_delete {
                data.push_str("<MFADelete>");
                data.push_str(if v { "Enabled" } else { "Disabled" });
                data.push_str("</MFADelete>");
            }

            match self.versioning_status {
                VersioningStatus::Enabled => data.push_str("<Status>Enabled</Status>"),
                VersioningStatus::Suspended => data.push_str("<Status>Suspended</Status>"),
            }

            data.push_str("</VersioningConfiguration>");
            data
        };
        let body = Arc::new(SegmentedBytes::from(Bytes::from(data)));

        Ok(S3Request::builder()
            .client(self.client)
            .method(Method::PUT)
            .region(self.region)
            .bucket(self.bucket)
            .query_params(insert(self.extra_query_params, "versioning"))
            .headers(self.extra_headers.unwrap_or_default())
            .body(body)
            .build())
    }
}