rocketmq_common/common/attribute/
cleanup_policy.rs1use 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}