Skip to main content

rustfs_replication/
tagging.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 std::collections::HashMap;
16
17use url::form_urlencoded;
18
19pub struct ReplicationTagFilter;
20
21impl ReplicationTagFilter {
22    pub fn decode_tags_to_map(tags: &str) -> HashMap<String, String> {
23        decode_tags_to_map(tags)
24    }
25}
26
27pub fn decode_tags_to_map(tags: &str) -> HashMap<String, String> {
28    let mut list = HashMap::new();
29
30    for (k, v) in form_urlencoded::parse(tags.as_bytes()) {
31        if k.is_empty() {
32            continue;
33        }
34
35        list.insert(k.to_string(), v.to_string());
36    }
37
38    list
39}
40
41#[cfg(test)]
42mod tests {
43    use super::{ReplicationTagFilter, decode_tags_to_map};
44
45    #[test]
46    fn decode_tags_to_map_preserves_bucket_tagging_parser_behavior() {
47        let tags = decode_tags_to_map("env=prod&encoded=a%2Fb&=ignored");
48
49        assert_eq!(tags.get("env").map(String::as_str), Some("prod"));
50        assert_eq!(tags.get("encoded").map(String::as_str), Some("a/b"));
51        assert!(!tags.contains_key(""));
52    }
53
54    #[test]
55    fn replication_tag_filter_uses_shared_parser() {
56        let tags = ReplicationTagFilter::decode_tags_to_map("env=prod");
57
58        assert_eq!(tags.get("env").map(String::as_str), Some("prod"));
59    }
60}