1use crate::query::{self, BadQuery};
7
8pub const MARKER: &str = "reveal";
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum Part {
14 Parties,
16 Subject,
18 Dialog,
20 Analysis,
22 Links,
24 All,
26 Audio,
28}
29
30impl Part {
31 pub const ALL: [Part; 7] = [
33 Part::Parties,
34 Part::Subject,
35 Part::Dialog,
36 Part::Analysis,
37 Part::Links,
38 Part::All,
39 Part::Audio,
40 ];
41
42 #[must_use]
44 pub fn name(self) -> &'static str {
45 match self {
46 Part::Parties => "parties",
47 Part::Subject => "subject",
48 Part::Dialog => "dialog",
49 Part::Analysis => "analysis",
50 Part::Links => "links",
51 Part::All => "all",
52 Part::Audio => "audio",
53 }
54 }
55
56 pub fn from_query(query: Option<&str>) -> Result<Option<Part>, BadQuery> {
64 let marked = query.unwrap_or("").split('&').any(|piece| {
65 form_urlencoded::parse(piece.as_bytes())
66 .next()
67 .is_some_and(|(name, _)| name == MARKER)
68 });
69 if !marked {
70 return Ok(None);
71 }
72 let read = query::pairs(query, &[MARKER])?;
73 read.get(MARKER)
74 .and_then(|value| Part::named(value))
75 .map(Some)
76 .ok_or(BadQuery)
77 }
78
79 fn named(name: &str) -> Option<Part> {
81 Part::ALL.into_iter().find(|part| part.name() == name)
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88 use crate::audit::Action;
89
90 #[test]
91 fn each_part_has_the_name_the_marker_and_the_log_use() {
92 let named: Vec<&str> = Part::ALL.into_iter().map(Part::name).collect();
93 assert_eq!(
94 named,
95 [
96 "parties", "subject", "dialog", "analysis", "links", "all", "audio"
97 ],
98 "the names"
99 );
100 }
101
102 #[test]
103 fn a_read_with_no_query_is_not_a_reveal() {
104 assert_eq!(Part::from_query(None), Ok(None), "no query");
105 }
106
107 #[test]
108 fn a_query_that_names_a_part_is_a_reveal_of_it() {
109 for part in Part::ALL {
110 let query = format!("reveal={}", part.name());
111 assert_eq!(Part::from_query(Some(&query)), Ok(Some(part)), "{query}");
112 }
113 }
114
115 #[test]
116 fn a_marker_that_names_no_known_part_is_refused() {
117 for unknown in ["everything", "party", "audio2", "*", "video"] {
118 let query = format!("reveal={unknown}");
119 assert_eq!(Part::from_query(Some(&query)), Err(BadQuery), "{query}");
120 }
121 }
122
123 #[test]
126 fn a_query_that_does_not_name_the_marker_is_not_a_reveal() {
127 for query in ["limit=3", "x=1&y=2", "revealed=all", "part=all"] {
128 assert_eq!(Part::from_query(Some(query)), Ok(None), "{query}");
129 }
130 }
131
132 #[test]
135 fn a_marker_beside_another_parameter_is_refused() {
136 for query in [
137 "x=1&reveal=links",
138 "reveal=links&x=1",
139 "limit=3&reveal=all&y=2",
140 ] {
141 assert_eq!(Part::from_query(Some(query)), Err(BadQuery), "{query}");
142 }
143 }
144
145 #[test]
146 fn a_marker_given_twice_is_refused() {
147 for query in ["reveal=links&reveal=links", "reveal=links&reveal=all"] {
148 assert_eq!(Part::from_query(Some(query)), Err(BadQuery), "{query}");
149 }
150 }
151
152 #[test]
154 fn an_empty_piece_beside_the_marker_names_nothing() {
155 for query in ["reveal=links&", "&reveal=links", "&&reveal=links&&"] {
156 assert_eq!(
157 Part::from_query(Some(query)),
158 Ok(Some(Part::Links)),
159 "{query}"
160 );
161 }
162 }
163
164 #[test]
167 fn a_marker_with_no_value_is_refused() {
168 for query in ["reveal", "reveal=", "reveal&x=1"] {
169 assert_eq!(Part::from_query(Some(query)), Err(BadQuery), "{query}");
170 }
171 }
172
173 #[test]
176 fn a_percent_encoded_marker_is_the_marker() {
177 for query in ["%72eveal=links", "reveal=%6Cinks", "r%65veal=li%6Eks"] {
178 assert_eq!(
179 Part::from_query(Some(query)),
180 Ok(Some(Part::Links)),
181 "{query}"
182 );
183 }
184 }
185
186 #[test]
187 fn a_reveal_is_recorded_under_an_action_that_names_its_part() {
188 let named: Vec<&str> = Part::ALL
189 .into_iter()
190 .map(|part| Action::Reveal(part).as_str())
191 .collect();
192 assert_eq!(
193 named,
194 [
195 "reveal_parties",
196 "reveal_subject",
197 "reveal_dialog",
198 "reveal_analysis",
199 "reveal_links",
200 "reveal_all",
201 "reveal_audio"
202 ],
203 "the names"
204 );
205 }
206
207 #[test]
208 fn a_legal_reveal_is_recorded_under_a_legal_action_that_names_its_part() {
209 let named: Vec<&str> = Part::ALL
210 .into_iter()
211 .map(|part| Action::LegalReveal(part).as_str())
212 .collect();
213 assert_eq!(
214 named,
215 [
216 "legal_reveal_parties",
217 "legal_reveal_subject",
218 "legal_reveal_dialog",
219 "legal_reveal_analysis",
220 "legal_reveal_links",
221 "legal_reveal_all",
222 "legal_reveal_audio"
223 ],
224 "the names"
225 );
226 }
227
228 fn marked(query: &str) -> Result<Option<&'static str>, BadQuery> {
230 Part::from_query(Some(query)).map(|part| part.map(Part::name))
231 }
232
233 #[test]
234 fn a_marker_of_audio_is_a_reveal_of_a_recording() {
235 assert_eq!(marked("reveal=audio"), Ok(Some("audio")), "the part");
236 }
237
238 #[test]
239 fn a_reveal_of_a_recording_has_actions_of_its_own() {
240 let audio = Part::ALL
241 .into_iter()
242 .find(|part| part.name() == "audio")
243 .expect("a part named audio");
244 assert_eq!(
245 (
246 Action::Reveal(audio).as_str(),
247 Action::LegalReveal(audio).as_str()
248 ),
249 ("reveal_audio", "legal_reveal_audio"),
250 "the names"
251 );
252 }
253}