rocket_multipart_form_data/
multipart_form_data_field.rs

1use std::str::FromStr;
2
3use crate::{mime::Mime, MultipartFormDataType, Repetition};
4
5const DEFAULT_IN_MEMORY_DATA_LIMIT: u64 = 1024 * 1024;
6const DEFAULT_FILE_DATA_LIMIT: u64 = 8 * 1024 * 1024;
7
8/// The guarder for fields.
9#[derive(Debug, Clone)]
10pub struct MultipartFormDataField<'a> {
11    /// The type of this field.
12    pub typ:          MultipartFormDataType,
13    /// The name of this field.
14    pub field_name:   &'a str,
15    /// The size limit for this field.
16    pub size_limit:   u64,
17    /// To filter the content types. It supports stars.
18    pub content_type: Option<Vec<Mime>>,
19    /// To define this `MultipartFormDataField` instance can be used how many times.
20    pub repetition:   Repetition,
21}
22
23impl<'a> MultipartFormDataField<'a> {
24    /// Create a text field, the default size_limit is 1 MiB.
25    #[inline]
26    pub fn text<S: ?Sized + AsRef<str>>(field_name: &S) -> MultipartFormDataField {
27        MultipartFormDataField {
28            typ:          MultipartFormDataType::Text,
29            field_name:   field_name.as_ref(),
30            size_limit:   DEFAULT_IN_MEMORY_DATA_LIMIT,
31            content_type: None,
32            repetition:   Repetition::default(),
33        }
34    }
35
36    /// Create a raw field, the default size_limit is 1 MiB.
37    #[inline]
38    pub fn bytes<S: ?Sized + AsRef<str>>(field_name: &S) -> MultipartFormDataField {
39        Self::raw(field_name.as_ref())
40    }
41
42    /// Create a raw field, the default size_limit is 1 MiB.
43    #[inline]
44    pub fn raw<S: ?Sized + AsRef<str>>(field_name: &S) -> MultipartFormDataField {
45        MultipartFormDataField {
46            typ:          MultipartFormDataType::Raw,
47            field_name:   field_name.as_ref(),
48            size_limit:   DEFAULT_IN_MEMORY_DATA_LIMIT,
49            content_type: None,
50            repetition:   Repetition::default(),
51        }
52    }
53
54    /// Create a file field, the default size_limit is 8 MiB.
55    #[inline]
56    pub fn file<S: ?Sized + AsRef<str>>(field_name: &S) -> MultipartFormDataField {
57        MultipartFormDataField {
58            typ:          MultipartFormDataType::File,
59            field_name:   field_name.as_ref(),
60            size_limit:   DEFAULT_FILE_DATA_LIMIT,
61            content_type: None,
62            repetition:   Repetition::default(),
63        }
64    }
65
66    /// Set the size_limit for this field.
67    #[inline]
68    pub fn size_limit(mut self, size_limit: u64) -> MultipartFormDataField<'a> {
69        self.size_limit = size_limit;
70        self
71    }
72
73    /// Add a content type filter for this field. This method can be used multiple times to use multiple content type filters.
74    #[inline]
75    pub fn content_type(mut self, content_type: Option<Mime>) -> MultipartFormDataField<'a> {
76        match content_type {
77            Some(content_type) => match self.content_type.as_mut() {
78                Some(v) => {
79                    v.push(content_type);
80                },
81                None => {
82                    self.content_type = Some(vec![content_type]);
83                },
84            },
85            None => self.content_type = None,
86        }
87        self
88    }
89
90    /// Add a content type filter for this field. This method can be used multiple times to use multiple content type filters.
91    #[inline]
92    pub fn content_type_by_string<S: AsRef<str>>(
93        mut self,
94        content_type: Option<S>,
95    ) -> Result<MultipartFormDataField<'a>, mime::FromStrError> {
96        match content_type {
97            Some(content_type) => {
98                let content_type = Mime::from_str(content_type.as_ref())?;
99                match self.content_type.as_mut() {
100                    Some(v) => {
101                        v.push(content_type);
102                    },
103                    None => {
104                        self.content_type = Some(vec![content_type]);
105                    },
106                }
107            },
108            None => self.content_type = None,
109        }
110        Ok(self)
111    }
112
113    /// Set the repetition for this field.
114    #[inline]
115    pub fn repetition(mut self, repetition: Repetition) -> MultipartFormDataField<'a> {
116        self.repetition = repetition;
117        self
118    }
119}