1use s3s::dto::{LifecycleRuleAndOperator, LifecycleRuleFilter, NoncurrentVersionTransition, Tag, Transition};
15use time::OffsetDateTime;
16
17use crate::tagging;
18
19const _ERR_TRANSITION_INVALID_DAYS: &str = "Days must be 0 or greater when used with Transition";
20const _ERR_TRANSITION_INVALID_DATE: &str = "Date must be provided in ISO 8601 format";
21const ERR_TRANSITION_INVALID: &str =
22 "Exactly one of Days (0 or greater) or Date (positive ISO 8601 format) should be present in Transition.";
23const _ERR_TRANSITION_DATE_NOT_MIDNIGHT: &str = "'Date' must be at midnight GMT";
24
25pub trait Filter {
26 fn test_tags(&self, user_tags: &str) -> bool;
27 fn by_size(&self, sz: i64) -> bool;
28}
29
30impl Filter for LifecycleRuleFilter {
31 fn test_tags(&self, user_tags: &str) -> bool {
32 if !requires_tag_matching(self) {
33 return true;
34 }
35
36 let user_tags = tagging::decode_tags_to_map(user_tags);
37
38 self.tag.as_ref().is_none_or(|tag| tag_matches(tag, &user_tags))
39 && self.and.as_ref().is_none_or(|and| and_tags_match(and, &user_tags))
40 }
41
42 fn by_size(&self, sz: i64) -> bool {
43 let sz = sz.max(0);
44
45 self.object_size_greater_than.is_none_or(|min| sz > min)
46 && self.object_size_less_than.is_none_or(|max| sz < max)
47 && self.and.as_ref().is_none_or(|and| and_size_matches(and, sz))
48 }
49}
50
51fn requires_tag_matching(filter: &LifecycleRuleFilter) -> bool {
52 filter.tag.is_some()
53 || filter
54 .and
55 .as_ref()
56 .and_then(|and| and.tags.as_ref())
57 .is_some_and(|tags| !tags.is_empty())
58}
59
60fn tag_matches(tag: &Tag, user_tags: &std::collections::HashMap<String, String>) -> bool {
61 let Some(key) = tag.key.as_deref() else {
62 return false;
63 };
64 let Some(value) = tag.value.as_deref() else {
65 return false;
66 };
67
68 user_tags.get(key).is_some_and(|actual| actual == value)
69}
70
71fn and_tags_match(and: &LifecycleRuleAndOperator, user_tags: &std::collections::HashMap<String, String>) -> bool {
72 and.tags
73 .as_ref()
74 .is_none_or(|tags| tags.iter().all(|tag| tag_matches(tag, user_tags)))
75}
76
77fn and_size_matches(and: &LifecycleRuleAndOperator, sz: i64) -> bool {
78 and.object_size_greater_than.is_none_or(|min| sz > min) && and.object_size_less_than.is_none_or(|max| sz < max)
79}
80
81pub trait TransitionOps {
82 fn validate(&self) -> Result<(), std::io::Error>;
83}
84
85pub trait NoncurrentVersionTransitionOps {
86 fn validate(&self) -> Result<(), std::io::Error>;
87}
88
89fn is_midnight(date: OffsetDateTime) -> bool {
90 date.hour() == 0 && date.minute() == 0 && date.second() == 0 && date.nanosecond() == 0
91}
92
93impl TransitionOps for Transition {
94 fn validate(&self) -> Result<(), std::io::Error> {
95 if self
96 .storage_class
97 .as_ref()
98 .is_none_or(|storage_class| storage_class.as_str().is_empty())
99 {
100 return Err(std::io::Error::other("ERR_XML_NOT_WELL_FORMED"));
101 }
102 if self.date.is_some() == self.days.is_some() {
103 return Err(std::io::Error::other(ERR_TRANSITION_INVALID));
104 }
105 if self.days.is_some_and(|days| days < 0) {
106 return Err(std::io::Error::other(ERR_TRANSITION_INVALID));
107 }
108 if let Some(date) = self.date.clone() {
109 let date = OffsetDateTime::from(date);
110 if date.unix_timestamp() <= 0 || !is_midnight(date) {
111 return Err(std::io::Error::other(ERR_TRANSITION_INVALID));
112 }
113 }
114 Ok(())
115 }
116}
117
118impl NoncurrentVersionTransitionOps for NoncurrentVersionTransition {
119 fn validate(&self) -> Result<(), std::io::Error> {
120 if self
121 .storage_class
122 .as_ref()
123 .is_none_or(|storage_class| storage_class.as_str().is_empty())
124 {
125 return Err(std::io::Error::other("ERR_XML_NOT_WELL_FORMED"));
126 }
127 if self.noncurrent_days.is_none() {
128 return Err(std::io::Error::other(ERR_TRANSITION_INVALID));
129 }
130 if self.noncurrent_days.is_some_and(|days| days < 0)
131 || self.newer_noncurrent_versions.is_some_and(|versions| versions < 0)
132 {
133 return Err(std::io::Error::other(ERR_TRANSITION_INVALID));
134 }
135 Ok(())
136 }
137}
138
139#[cfg(test)]
140mod test {
141 use super::*;
142
143 #[test]
144 fn lifecycle_rule_filter_matches_single_tag() {
145 let filter = LifecycleRuleFilter {
146 tag: Some(Tag {
147 key: Some("env".to_string()),
148 value: Some("prod".to_string()),
149 }),
150 ..Default::default()
151 };
152
153 assert!(<LifecycleRuleFilter as Filter>::test_tags(&filter, "env=prod&team=storage"));
154 assert!(!<LifecycleRuleFilter as Filter>::test_tags(&filter, "env=dev&team=storage"));
155 assert!(!<LifecycleRuleFilter as Filter>::test_tags(&filter, "team=storage"));
156 }
157
158 #[test]
159 fn lifecycle_rule_filter_matches_all_and_tags() {
160 let filter = LifecycleRuleFilter {
161 and: Some(LifecycleRuleAndOperator {
162 tags: Some(vec![
163 Tag {
164 key: Some("env".to_string()),
165 value: Some("prod".to_string()),
166 },
167 Tag {
168 key: Some("team".to_string()),
169 value: Some("storage".to_string()),
170 },
171 ]),
172 ..Default::default()
173 }),
174 ..Default::default()
175 };
176
177 assert!(<LifecycleRuleFilter as Filter>::test_tags(&filter, "env=prod&team=storage"));
178 assert!(!<LifecycleRuleFilter as Filter>::test_tags(&filter, "env=prod&team=platform"));
179 }
180
181 #[test]
182 fn lifecycle_rule_filter_respects_size_bounds() {
183 let filter = LifecycleRuleFilter {
184 object_size_greater_than: Some(5),
185 object_size_less_than: Some(10),
186 ..Default::default()
187 };
188
189 assert!(!filter.by_size(5));
190 assert!(filter.by_size(6));
191 assert!(!filter.by_size(10));
192 }
193
194 #[test]
195 fn lifecycle_rule_filter_without_tag_constraints_accepts_any_tags() {
196 let filter = LifecycleRuleFilter {
197 object_size_greater_than: Some(5),
198 ..Default::default()
199 };
200
201 assert!(<LifecycleRuleFilter as Filter>::test_tags(&filter, "env=prod&team=storage"));
202 assert!(<LifecycleRuleFilter as Filter>::test_tags(&filter, ""));
203 }
204}