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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::{Field, File};
use actix_web::http::StatusCode;
use actix_web::ResponseError;
use std::str::FromStr;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Text field '{0}' not found")]
TextNotFound(String),
#[error("File upload '{0}' not found")]
FileNotFound(String),
#[error("Text field '{field_name}' couldn't be parsed: {source}")]
ParseError {
field_name: String,
source: Box<dyn std::error::Error + Send>,
},
#[error("Unexpected part found with name '{0}'")]
UnexpectedPart(String),
}
impl ResponseError for Error {
fn status_code(&self) -> StatusCode {
StatusCode::BAD_REQUEST
}
}
#[doc(hidden)]
#[derive(Default)]
pub struct FromFieldConfig {
pub deny_extra_parts: bool,
}
#[doc(hidden)]
pub trait FromField
where
Self: std::marker::Sized,
{
fn from_fields(
fields: Vec<Field>,
config: &FromFieldConfig,
field_name: &str,
) -> Result<Self, Error>;
}
#[doc(hidden)]
pub trait FromFieldExt
where
Self: std::marker::Sized,
{
fn from_fields(
fields: Vec<Field>,
config: &FromFieldConfig,
field_name: &str,
) -> Result<Self, Error>;
}
impl<T, E> FromField for T
where
T: FromStr<Err = E>,
E: std::error::Error + Send + 'static,
{
fn from_fields(
fields: Vec<Field>,
config: &FromFieldConfig,
field_name: &str,
) -> Result<Self, Error> {
let mut matches = Vec::<T>::from_fields(fields, config, field_name)?;
match matches.len() {
0 => Err(Error::TextNotFound(field_name.into())),
1 => Ok(matches.pop().unwrap()),
_ if config.deny_extra_parts => Err(Error::UnexpectedPart(field_name.into())),
_ => Ok(matches.pop().unwrap()),
}
}
}
impl<T, E> FromFieldExt for Option<T>
where
T: FromStr<Err = E>,
E: std::error::Error + Send + 'static,
{
fn from_fields(
fields: Vec<Field>,
config: &FromFieldConfig,
field_name: &str,
) -> Result<Self, Error> {
let mut matches = Vec::<T>::from_fields(fields, config, field_name)?;
match matches.len() {
0 => Ok(None),
1 => Ok(Some(matches.pop().unwrap())),
_ if config.deny_extra_parts => Err(Error::UnexpectedPart(field_name.into())),
_ => Ok(Some(matches.pop().unwrap())),
}
}
}
impl<T, E> FromFieldExt for Vec<T>
where
T: FromStr<Err = E>,
E: std::error::Error + Send + 'static,
{
fn from_fields(
fields: Vec<Field>,
config: &FromFieldConfig,
field_name: &str,
) -> Result<Self, Error> {
let total = fields.len();
let texts = fields
.into_iter()
.filter_map(Field::text)
.map(|text| {
T::from_str(&text.text).map_err(|source| Error::ParseError {
field_name: field_name.to_string(),
source: Box::new(source),
})
})
.collect::<Result<Vec<_>, _>>()?;
if config.deny_extra_parts && texts.len() < total {
return Err(Error::UnexpectedPart(field_name.to_string()));
}
Ok(texts)
}
}
impl FromField for File {
fn from_fields(
fields: Vec<Field>,
config: &FromFieldConfig,
field_name: &str,
) -> Result<Self, Error> {
let mut matches = Vec::<File>::from_fields(fields, config, field_name)?;
match matches.len() {
0 => Err(Error::FileNotFound(field_name.into())),
1 => Ok(matches.pop().unwrap()),
_ if config.deny_extra_parts => Err(Error::UnexpectedPart(field_name.into())),
_ => Ok(matches.pop().unwrap()),
}
}
}
impl FromFieldExt for Option<File> {
fn from_fields(
fields: Vec<Field>,
config: &FromFieldConfig,
field_name: &str,
) -> Result<Self, Error> {
let mut matches = Vec::<File>::from_fields(fields, config, field_name)?;
match matches.len() {
0 => Ok(None),
1 => Ok(Some(matches.pop().unwrap())),
_ if config.deny_extra_parts => Err(Error::UnexpectedPart(field_name.into())),
_ => Ok(Some(matches.pop().unwrap())),
}
}
}
impl FromFieldExt for Vec<File> {
fn from_fields(
fields: Vec<Field>,
config: &FromFieldConfig,
field_name: &str,
) -> Result<Self, Error> {
let total = fields.len();
let files = fields
.into_iter()
.filter_map(Field::file)
.collect::<Vec<_>>();
if config.deny_extra_parts && files.len() < total {
return Err(Error::UnexpectedPart(field_name.to_string()));
}
Ok(files)
}
}