1#[derive(Debug, Clone, Copy)]
5pub struct Permissions {
6 bits: u32,
8}
9
10#[derive(Debug, Clone, Copy)]
12pub struct PermissionFlags {
13 pub print: bool,
15 pub modify_contents: bool,
17 pub copy: bool,
19 pub modify_annotations: bool,
21 pub fill_forms: bool,
23 pub accessibility: bool,
25 pub assemble: bool,
27 pub print_high_quality: bool,
29}
30
31impl Default for PermissionFlags {
32 fn default() -> Self {
33 Self {
34 print: false,
35 modify_contents: false,
36 copy: false,
37 modify_annotations: false,
38 fill_forms: false,
39 accessibility: true, assemble: false,
41 print_high_quality: false,
42 }
43 }
44}
45
46impl Permissions {
47 pub fn new() -> Self {
49 Self { bits: 0xFFFFF0C0 }
52 }
53
54 pub fn from_flags(flags: PermissionFlags) -> Self {
56 let mut perm = Self::new();
57
58 if flags.print {
59 perm.set_print(true);
60 }
61 if flags.modify_contents {
62 perm.set_modify_contents(true);
63 }
64 if flags.copy {
65 perm.set_copy(true);
66 }
67 if flags.modify_annotations {
68 perm.set_modify_annotations(true);
69 }
70 if flags.fill_forms {
71 perm.set_fill_forms(true);
72 }
73 if flags.accessibility {
74 perm.set_accessibility(true);
75 }
76 if flags.assemble {
77 perm.set_assemble(true);
78 }
79 if flags.print_high_quality {
80 perm.set_print_high_quality(true);
81 }
82
83 perm
84 }
85
86 pub fn all() -> Self {
88 let mut perm = Self::new();
89 perm.bits |= 0x0F3C; perm
91 }
92
93 pub fn bits(&self) -> u32 {
95 self.bits
96 }
97
98 pub fn from_bits(bits: u32) -> Self {
100 Self { bits }
101 }
102
103 pub fn contains(&self, other: Permissions) -> bool {
105 (self.bits & other.bits) == other.bits
106 }
107
108 pub fn set_print(&mut self, allow: bool) -> &mut Self {
110 if allow {
111 self.bits |= 1 << 2;
112 } else {
113 self.bits &= !(1 << 2);
114 }
115 self
116 }
117
118 pub fn can_print(&self) -> bool {
120 (self.bits & (1 << 2)) != 0
121 }
122
123 pub fn set_modify_contents(&mut self, allow: bool) -> &mut Self {
125 if allow {
126 self.bits |= 1 << 3;
127 } else {
128 self.bits &= !(1 << 3);
129 }
130 self
131 }
132
133 pub fn can_modify_contents(&self) -> bool {
135 (self.bits & (1 << 3)) != 0
136 }
137
138 pub fn set_copy(&mut self, allow: bool) -> &mut Self {
140 if allow {
141 self.bits |= 1 << 4;
142 } else {
143 self.bits &= !(1 << 4);
144 }
145 self
146 }
147
148 pub fn can_copy(&self) -> bool {
150 (self.bits & (1 << 4)) != 0
151 }
152
153 pub fn set_modify_annotations(&mut self, allow: bool) -> &mut Self {
155 if allow {
156 self.bits |= 1 << 5;
157 } else {
158 self.bits &= !(1 << 5);
159 }
160 self
161 }
162
163 pub fn can_modify_annotations(&self) -> bool {
165 (self.bits & (1 << 5)) != 0
166 }
167
168 pub fn set_fill_forms(&mut self, allow: bool) -> &mut Self {
170 if allow {
171 self.bits |= 1 << 8;
172 } else {
173 self.bits &= !(1 << 8);
174 }
175 self
176 }
177
178 pub fn can_fill_forms(&self) -> bool {
180 (self.bits & (1 << 8)) != 0
181 }
182
183 pub fn set_accessibility(&mut self, allow: bool) -> &mut Self {
185 if allow {
186 self.bits |= 1 << 9;
187 } else {
188 self.bits &= !(1 << 9);
189 }
190 self
191 }
192
193 pub fn can_access_for_accessibility(&self) -> bool {
195 (self.bits & (1 << 9)) != 0
196 }
197
198 pub fn set_assemble(&mut self, allow: bool) -> &mut Self {
200 if allow {
201 self.bits |= 1 << 10;
202 } else {
203 self.bits &= !(1 << 10);
204 }
205 self
206 }
207
208 pub fn can_assemble(&self) -> bool {
210 (self.bits & (1 << 10)) != 0
211 }
212
213 pub fn set_print_high_quality(&mut self, allow: bool) -> &mut Self {
215 if allow {
216 self.bits |= 1 << 11;
217 } else {
218 self.bits &= !(1 << 11);
219 }
220 self
221 }
222
223 pub fn can_print_high_quality(&self) -> bool {
225 (self.bits & (1 << 11)) != 0
226 }
227
228 pub fn flags(&self) -> PermissionFlags {
230 PermissionFlags {
231 print: self.can_print(),
232 modify_contents: self.can_modify_contents(),
233 copy: self.can_copy(),
234 modify_annotations: self.can_modify_annotations(),
235 fill_forms: self.can_fill_forms(),
236 accessibility: self.can_access_for_accessibility(),
237 assemble: self.can_assemble(),
238 print_high_quality: self.can_print_high_quality(),
239 }
240 }
241}
242
243impl Default for Permissions {
244 fn default() -> Self {
245 Self::new()
246 }
247}
248
249#[cfg(test)]
250mod tests {
251 use super::*;
252
253 #[test]
254 fn test_permissions_new() {
255 let perm = Permissions::new();
256 assert_eq!(perm.bits(), 0xFFFFF0C0);
257
258 assert!(!perm.can_print());
260 assert!(!perm.can_modify_contents());
261 assert!(!perm.can_copy());
262 }
263
264 #[test]
265 fn test_permissions_all() {
266 let perm = Permissions::all();
267
268 assert!(perm.can_print());
269 assert!(perm.can_modify_contents());
270 assert!(perm.can_copy());
271 assert!(perm.can_modify_annotations());
272 assert!(perm.can_fill_forms());
273 assert!(perm.can_access_for_accessibility());
274 assert!(perm.can_assemble());
275 assert!(perm.can_print_high_quality());
276 }
277
278 #[test]
279 fn test_permission_flags() {
280 let mut perm = Permissions::new();
281
282 perm.set_print(true);
284 assert!(perm.can_print());
285 assert_eq!(perm.bits() & (1 << 2), 1 << 2);
286
287 perm.set_copy(true);
288 assert!(perm.can_copy());
289 assert_eq!(perm.bits() & (1 << 4), 1 << 4);
290
291 perm.set_print(false);
292 assert!(!perm.can_print());
293 assert_eq!(perm.bits() & (1 << 2), 0);
294 }
295
296 #[test]
297 fn test_from_flags() {
298 let flags = PermissionFlags {
299 print: true,
300 modify_contents: false,
301 copy: true,
302 modify_annotations: false,
303 fill_forms: true,
304 accessibility: true,
305 assemble: false,
306 print_high_quality: true,
307 };
308
309 let perm = Permissions::from_flags(flags);
310 assert!(perm.can_print());
311 assert!(!perm.can_modify_contents());
312 assert!(perm.can_copy());
313 assert!(!perm.can_modify_annotations());
314 assert!(perm.can_fill_forms());
315 assert!(perm.can_access_for_accessibility());
316 assert!(!perm.can_assemble());
317 assert!(perm.can_print_high_quality());
318 }
319
320 #[test]
321 fn test_get_flags() {
322 let mut perm = Permissions::new();
323 perm.set_print(true).set_copy(true).set_fill_forms(true);
324
325 let flags = perm.flags();
326 assert!(flags.print);
327 assert!(flags.copy);
328 assert!(flags.fill_forms);
329 assert!(!flags.modify_contents);
330 assert!(!flags.modify_annotations);
331 }
332
333 #[test]
334 fn test_permissions_default() {
335 let perm = Permissions::default();
336 assert_eq!(perm.bits(), Permissions::new().bits());
337 }
338
339 #[test]
340 fn test_permissions_bits_manipulation() {
341 let mut perm = Permissions::new();
342
343 perm.set_print(true);
345 perm.set_modify_contents(true);
346 perm.set_copy(true);
347 perm.set_modify_annotations(true);
348 perm.set_fill_forms(true);
349 perm.set_accessibility(true);
350 perm.set_assemble(true);
351 perm.set_print_high_quality(true);
352
353 assert!(perm.can_print());
354 assert!(perm.can_modify_contents());
355 assert!(perm.can_copy());
356 assert!(perm.can_modify_annotations());
357 assert!(perm.can_fill_forms());
358 assert!(perm.can_access_for_accessibility());
359 assert!(perm.can_assemble());
360 assert!(perm.can_print_high_quality());
361 }
362
363 #[test]
364 fn test_permissions_bits_clearing() {
365 let mut perm = Permissions::all();
366
367 perm.set_print(false);
369 perm.set_modify_contents(false);
370 perm.set_copy(false);
371 perm.set_modify_annotations(false);
372 perm.set_fill_forms(false);
373 perm.set_accessibility(false);
374 perm.set_assemble(false);
375 perm.set_print_high_quality(false);
376
377 assert!(!perm.can_print());
378 assert!(!perm.can_modify_contents());
379 assert!(!perm.can_copy());
380 assert!(!perm.can_modify_annotations());
381 assert!(!perm.can_fill_forms());
382 assert!(!perm.can_access_for_accessibility());
383 assert!(!perm.can_assemble());
384 assert!(!perm.can_print_high_quality());
385 }
386
387 #[test]
388 fn test_permissions_chaining() {
389 let mut perm = Permissions::new();
390 perm.set_print(true).set_copy(true).set_fill_forms(true);
391
392 assert!(perm.can_print());
393 assert!(perm.can_copy());
394 assert!(perm.can_fill_forms());
395 assert!(!perm.can_modify_contents());
396 }
397
398 #[test]
399 fn test_permission_flags_debug() {
400 let flags = PermissionFlags {
401 print: true,
402 modify_contents: false,
403 copy: true,
404 modify_annotations: false,
405 fill_forms: true,
406 accessibility: true,
407 assemble: false,
408 print_high_quality: true,
409 };
410
411 let debug_str = format!("{flags:?}");
413 assert!(debug_str.contains("PermissionFlags"));
414 }
415
416 #[test]
417 fn test_permission_flags_clone() {
418 let flags = PermissionFlags {
419 print: true,
420 modify_contents: false,
421 copy: true,
422 modify_annotations: false,
423 fill_forms: true,
424 accessibility: true,
425 assemble: false,
426 print_high_quality: true,
427 };
428
429 let cloned_flags = flags;
430 assert_eq!(flags.print, cloned_flags.print);
431 assert_eq!(flags.copy, cloned_flags.copy);
432 assert_eq!(flags.fill_forms, cloned_flags.fill_forms);
433 }
434
435 #[test]
436 fn test_permissions_specific_bit_patterns() {
437 let mut perm = Permissions::from_bits(0xFFFFF0C0);
439
440 perm.set_print(true);
442 assert_eq!(perm.bits() & (1 << 2), 1 << 2);
443
444 perm.set_modify_contents(true);
446 assert_eq!(perm.bits() & (1 << 3), 1 << 3);
447
448 perm.set_copy(true);
450 assert_eq!(perm.bits() & (1 << 4), 1 << 4);
451
452 perm.set_modify_annotations(true);
454 assert_eq!(perm.bits() & (1 << 5), 1 << 5);
455 }
456
457 #[test]
458 fn test_permissions_roundtrip_conversion() {
459 let original_flags = PermissionFlags {
460 print: true,
461 modify_contents: false,
462 copy: true,
463 modify_annotations: true,
464 fill_forms: false,
465 accessibility: true,
466 assemble: false,
467 print_high_quality: true,
468 };
469
470 let perm = Permissions::from_flags(original_flags);
471 let converted_flags = perm.flags();
472
473 assert_eq!(original_flags.print, converted_flags.print);
474 assert_eq!(
475 original_flags.modify_contents,
476 converted_flags.modify_contents
477 );
478 assert_eq!(original_flags.copy, converted_flags.copy);
479 assert_eq!(
480 original_flags.modify_annotations,
481 converted_flags.modify_annotations
482 );
483 assert_eq!(original_flags.fill_forms, converted_flags.fill_forms);
484 assert_eq!(original_flags.accessibility, converted_flags.accessibility);
485 assert_eq!(original_flags.assemble, converted_flags.assemble);
486 assert_eq!(
487 original_flags.print_high_quality,
488 converted_flags.print_high_quality
489 );
490 }
491
492 #[test]
493 fn test_permissions_edge_cases() {
494 let perm = Permissions::from_bits(0x00000000);
496 assert!(!perm.can_print());
497 assert!(!perm.can_copy());
498
499 let perm = Permissions::from_bits(0xFFFFFFFF);
501 assert!(perm.can_print());
502 assert!(perm.can_modify_contents());
503 assert!(perm.can_copy());
504 assert!(perm.can_modify_annotations());
505 assert!(perm.can_fill_forms());
506 assert!(perm.can_access_for_accessibility());
507 assert!(perm.can_assemble());
508 assert!(perm.can_print_high_quality());
509 }
510}