Skip to main content

sword_layers/body_limit/
config.rs

1use crate::{DisplayConfig, utils::ByteConfig};
2use byte_unit::Byte;
3use console::style;
4use serde::{Deserialize, Serialize};
5use std::str::FromStr;
6
7/// ### Body Limit Configuration
8///
9/// Configuration for the Body Limit Layer
10/// This configuration allows you to set a maximum size for request bodies.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(default)]
13pub struct BodyLimitConfig {
14    /// The maximum allowed size for request bodies (e.g., "1MB", "500KB").
15    #[serde(rename = "max-size")]
16    pub max_size: ByteConfig,
17    /// Whether to display the configuration details.
18    pub display: bool,
19}
20
21impl DisplayConfig for BodyLimitConfig {
22    fn display(&self) {
23        if self.display {
24            println!("\n{}", style("Body Limit Configuration:").bold());
25            println!("  ↳  Max Body Size: {}", self.max_size.raw);
26        }
27    }
28}
29
30impl Default for BodyLimitConfig {
31    fn default() -> Self {
32        let max_size = "10MB".to_string();
33        let parsed = Byte::from_str(&max_size)
34            .unwrap_or_else(|_| Byte::from_u64(10 * 1024 * 1024))
35            .as_u64() as usize;
36
37        BodyLimitConfig {
38            display: true,
39            max_size: ByteConfig {
40                parsed,
41                raw: max_size,
42            },
43        }
44    }
45}