1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#[derive(Debug, Clone, Copy)]
pub(crate) enum RepetitionCounter {
    Fixed(u32),
    Infinite,
}

impl RepetitionCounter {
    #[inline]
    pub fn decrease_check_is_over(&mut self) -> bool {
        match self {
            RepetitionCounter::Fixed(n) => {
                debug_assert!(*n > 0);

                *n -= 1;
                *n == 0
            },
            RepetitionCounter::Infinite => false,
        }
    }
}

impl Default for RepetitionCounter {
    #[inline]
    fn default() -> Self {
        RepetitionCounter::Fixed(1)
    }
}

#[derive(Debug, Clone, Copy)]
/// It can be used to define a `MultipartFormDataField` instance which can be used how many times.
pub struct Repetition {
    counter: RepetitionCounter,
}

impl Repetition {
    #[inline]
    /// Create a `Repetition` instance for only one time.
    pub fn new() -> Repetition {
        Repetition {
            counter: RepetitionCounter::Fixed(1)
        }
    }

    #[inline]
    /// Create a `Repetition` instance for any fixed times.
    pub fn fixed(count: u32) -> Repetition {
        if count == 0 {
            eprintln!(
                "The count of fixed repetition for a `MultipartFormDataField` instance should be \
                 bigger than 0. Use 1 instead."
            );

            Repetition::new()
        } else {
            Repetition {
                counter: RepetitionCounter::Fixed(count)
            }
        }
    }

    #[inline]
    /// Create a `Repetition` instance for infinite times.
    pub fn infinite() -> Repetition {
        Repetition {
            counter: RepetitionCounter::Infinite
        }
    }

    #[inline]
    pub(crate) fn decrease_check_is_over(&mut self) -> bool {
        self.counter.decrease_check_is_over()
    }
}

impl Default for Repetition {
    #[inline]
    /// Create a `Repetition` instance for only one time.
    fn default() -> Self {
        Repetition::new()
    }
}