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
use crate::blueprint::constructor::{Constructor, RegisteredConstructor};
use crate::blueprint::Blueprint;
use crate::f;
use crate::unit::ByteUnit;
use ubyte::ToByteUnit;

#[derive(Debug, Clone, Copy)]
/// An upper limit on the size of incoming request bodies.  
///
/// Check out the documentation of [`BufferedBody`](crate::request::body::BufferedBody) for more details.
pub enum BodySizeLimit {
    /// There is an active limit on the size of incoming request bodies.
    Enabled {
        /// The maximum size of incoming request bodies, in bytes.
        max_size: ByteUnit,
    },
    /// There is no limit on the size of incoming request bodies.
    Disabled,
}

impl BodySizeLimit {
    /// Register the [default constructor](Self::default_constructor)
    /// for [`BodySizeLimit`] with a [`Blueprint`].
    pub fn register(bp: &mut Blueprint) -> RegisteredConstructor {
        Self::default_constructor().register(bp)
    }

    /// The [default constructor](Self::default) for [`BodySizeLimit`].
    pub fn default_constructor() -> Constructor {
        Constructor::request_scoped(f!(<super::BodySizeLimit as std::default::Default>::default))
    }
}

impl Default for BodySizeLimit {
    fn default() -> Self {
        Self::Enabled {
            max_size: 2.megabytes(),
        }
    }
}