rocketmq_common/common/attribute/
cleanup_policy.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use std::fmt;
19use std::str::FromStr;
20
21#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
22pub enum CleanupPolicy {
23    #[default]
24    DELETE,
25    COMPACTION,
26}
27
28impl fmt::Display for CleanupPolicy {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        match self {
31            CleanupPolicy::DELETE => write!(f, "DELETE"),
32            CleanupPolicy::COMPACTION => write!(f, "COMPACTION"),
33        }
34    }
35}
36
37#[derive(Debug, PartialEq)]
38pub struct ParseCleanupPolicyError;
39
40impl fmt::Display for ParseCleanupPolicyError {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(f, "invalid cleanup policy")
43    }
44}
45
46impl FromStr for CleanupPolicy {
47    type Err = ParseCleanupPolicyError;
48
49    fn from_str(s: &str) -> Result<Self, Self::Err> {
50        match s.to_uppercase().as_str() {
51            "DELETE" => Ok(CleanupPolicy::DELETE),
52            "COMPACTION" => Ok(CleanupPolicy::COMPACTION),
53            _ => Err(ParseCleanupPolicyError),
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn cleanup_policy_display() {
64        assert_eq!(CleanupPolicy::DELETE.to_string(), "DELETE");
65        assert_eq!(CleanupPolicy::COMPACTION.to_string(), "COMPACTION");
66    }
67
68    #[test]
69    fn cleanup_policy_from_str() {
70        assert_eq!("DELETE".parse(), Ok(CleanupPolicy::DELETE));
71        assert_eq!("COMPACTION".parse(), Ok(CleanupPolicy::COMPACTION));
72    }
73
74    #[test]
75    fn cleanup_policy_from_str_case_insensitive() {
76        assert_eq!("delete".parse(), Ok(CleanupPolicy::DELETE));
77        assert_eq!("compaction".parse(), Ok(CleanupPolicy::COMPACTION));
78    }
79
80    #[test]
81    fn cleanup_policy_from_str_invalid() {
82        assert!("invalid".parse::<CleanupPolicy>().is_err());
83    }
84}