rocket_multipart_form_data/
repetition.rs

1#[derive(Debug, Clone, Copy)]
2pub(crate) enum RepetitionCounter {
3    Fixed(u32),
4    Infinite,
5}
6
7impl RepetitionCounter {
8    #[inline]
9    pub fn decrease_check_is_over(&mut self) -> bool {
10        match self {
11            RepetitionCounter::Fixed(n) => {
12                debug_assert!(*n > 0);
13
14                *n -= 1;
15                *n == 0
16            },
17            RepetitionCounter::Infinite => false,
18        }
19    }
20}
21
22impl Default for RepetitionCounter {
23    #[inline]
24    fn default() -> Self {
25        RepetitionCounter::Fixed(1)
26    }
27}
28
29#[derive(Debug, Clone, Copy)]
30/// It can be used to define a `MultipartFormDataField` instance which can be used how many times.
31pub struct Repetition {
32    counter: RepetitionCounter,
33}
34
35impl Repetition {
36    #[inline]
37    /// Create a `Repetition` instance for only one time.
38    pub fn new() -> Repetition {
39        Repetition {
40            counter: RepetitionCounter::Fixed(1)
41        }
42    }
43
44    #[inline]
45    /// Create a `Repetition` instance for any fixed times.
46    pub fn fixed(count: u32) -> Repetition {
47        if count == 0 {
48            eprintln!(
49                "The count of fixed repetition for a `MultipartFormDataField` instance should be \
50                 bigger than 0. Use 1 instead."
51            );
52
53            Repetition::new()
54        } else {
55            Repetition {
56                counter: RepetitionCounter::Fixed(count)
57            }
58        }
59    }
60
61    #[inline]
62    /// Create a `Repetition` instance for infinite times.
63    pub fn infinite() -> Repetition {
64        Repetition {
65            counter: RepetitionCounter::Infinite
66        }
67    }
68
69    #[inline]
70    pub(crate) fn decrease_check_is_over(&mut self) -> bool {
71        self.counter.decrease_check_is_over()
72    }
73}
74
75impl Default for Repetition {
76    #[inline]
77    /// Create a `Repetition` instance for only one time.
78    fn default() -> Self {
79        Repetition::new()
80    }
81}