rustfs-replication 1.0.1

Replication contracts and wire formats for RustFS.
Documentation
// Copyright 2024 RustFS Team
//
// 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::config::ObjectOpts;
use s3s::dto::ReplicaModificationsStatus;
use s3s::dto::ReplicationRule;

pub trait ReplicationRuleExt {
    fn prefix(&self) -> &str;
    fn metadata_replicate(&self, obj: &ObjectOpts) -> bool;
}

impl ReplicationRuleExt for ReplicationRule {
    /// The rule's key prefix: `Filter.Prefix`, else `Filter.And.Prefix`, else
    /// the deprecated top-level `Prefix` of a V1 rule written without a
    /// `<Filter>` (backlog#2367 C-2). A rule that carries both keeps AWS's
    /// precedence: the `<Filter>` is authoritative.
    fn prefix(&self) -> &str {
        if let Some(filter) = &self.filter {
            if let Some(prefix) = &filter.prefix {
                prefix
            } else if let Some(and) = &filter.and {
                and.prefix.as_deref().unwrap_or("")
            } else {
                ""
            }
        } else {
            self.prefix.as_deref().unwrap_or("")
        }
    }

    fn metadata_replicate(&self, obj: &ObjectOpts) -> bool {
        if !obj.replica {
            return true;
        }

        self.source_selection_criteria.as_ref().is_some_and(|s| {
            s.replica_modifications
                .clone()
                .is_some_and(|r| r.status == ReplicaModificationsStatus::from_static(ReplicaModificationsStatus::ENABLED))
        })
    }
}