Skip to main content

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    #[must_use]
38    /// Create a `Repetition` instance for only one time.
39    pub const fn new() -> Repetition {
40        Repetition {
41            counter: RepetitionCounter::Fixed(1)
42        }
43    }
44
45    #[inline]
46    #[must_use]
47    /// Create a `Repetition` instance for any fixed times.
48    ///
49    /// A `count` of `0` is invalid and is silently treated as `1`.
50    pub const fn fixed(count: u32) -> Repetition {
51        Repetition {
52            counter: RepetitionCounter::Fixed(if count == 0 { 1 } else { count })
53        }
54    }
55
56    #[inline]
57    #[must_use]
58    /// Create a `Repetition` instance for infinite times.
59    ///
60    /// Set a finite `MultipartFormDataOptions::max_data_bytes` because every accepted occurrence is stored.
61    pub const fn infinite() -> Repetition {
62        Repetition {
63            counter: RepetitionCounter::Infinite
64        }
65    }
66
67    #[inline]
68    pub(crate) fn decrease_check_is_over(&mut self) -> bool {
69        self.counter.decrease_check_is_over()
70    }
71}
72
73impl Default for Repetition {
74    #[inline]
75    /// Create a `Repetition` instance for only one time.
76    fn default() -> Self {
77        Repetition::new()
78    }
79}