Skip to main content

rustfs_replication/
rule.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::config::ObjectOpts;
16use s3s::dto::ReplicaModificationsStatus;
17use s3s::dto::ReplicationRule;
18
19pub trait ReplicationRuleExt {
20    fn prefix(&self) -> &str;
21    fn metadata_replicate(&self, obj: &ObjectOpts) -> bool;
22}
23
24impl ReplicationRuleExt for ReplicationRule {
25    /// The rule's key prefix: `Filter.Prefix`, else `Filter.And.Prefix`, else
26    /// the deprecated top-level `Prefix` of a V1 rule written without a
27    /// `<Filter>` (backlog#2367 C-2). A rule that carries both keeps AWS's
28    /// precedence: the `<Filter>` is authoritative.
29    fn prefix(&self) -> &str {
30        if let Some(filter) = &self.filter {
31            if let Some(prefix) = &filter.prefix {
32                prefix
33            } else if let Some(and) = &filter.and {
34                and.prefix.as_deref().unwrap_or("")
35            } else {
36                ""
37            }
38        } else {
39            self.prefix.as_deref().unwrap_or("")
40        }
41    }
42
43    fn metadata_replicate(&self, obj: &ObjectOpts) -> bool {
44        if !obj.replica {
45            return true;
46        }
47
48        self.source_selection_criteria.as_ref().is_some_and(|s| {
49            s.replica_modifications
50                .clone()
51                .is_some_and(|r| r.status == ReplicaModificationsStatus::from_static(ReplicaModificationsStatus::ENABLED))
52        })
53    }
54}