rocket_multipart_form_data/
multipart_form_data_field.rs1use 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#[derive(Debug, Clone)]
10pub struct MultipartFormDataField<'a> {
11 pub typ: MultipartFormDataType,
13 pub field_name: &'a str,
15 pub size_limit: u64,
17 pub content_type: Option<Vec<Mime>>,
19 pub repetition: Repetition,
21}
22
23impl<'a> MultipartFormDataField<'a> {
24 #[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 #[inline]
38 pub fn bytes<S: ?Sized + AsRef<str>>(field_name: &S) -> MultipartFormDataField {
39 Self::raw(field_name.as_ref())
40 }
41
42 #[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 #[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 #[inline]
68 pub fn size_limit(mut self, size_limit: u64) -> MultipartFormDataField<'a> {
69 self.size_limit = size_limit;
70 self
71 }
72
73 #[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 #[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 #[inline]
115 pub fn repetition(mut self, repetition: Repetition) -> MultipartFormDataField<'a> {
116 self.repetition = repetition;
117 self
118 }
119}