actix_form_data/
extractor.rs

1/*
2 * This file is part of Actix Form Data.
3 *
4 * Copyright © 2026 asonix
5 *
6 * Actix Form Data is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Actix Form Data is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Actix Form Data.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20use crate::{
21    types::{Form, Value},
22    upload::handle_multipart,
23};
24use actix_web::{dev::Payload, FromRequest, HttpRequest, ResponseError};
25use std::{future::Future, pin::Pin, rc::Rc};
26
27pub trait FormData {
28    type Item: 'static;
29    type Error: ResponseError + 'static;
30
31    fn form(req: &HttpRequest) -> Result<Form<Self::Item, Self::Error>, Self::Error>;
32
33    fn extract(value: Value<Self::Item>) -> Result<Self, Self::Error>
34    where
35        Self: Sized;
36}
37
38pub struct Multipart<T>(pub T);
39
40impl<T> FromRequest for Multipart<T>
41where
42    T: FormData,
43{
44    type Error = actix_web::Error;
45    type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>> + 'static>>;
46
47    fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
48        let multipart = actix_multipart::Multipart::new(req.headers(), payload.take());
49        let res = T::form(req);
50
51        Box::pin(async move {
52            let form = Rc::new(res?);
53
54            let uploaded = match handle_multipart(multipart, Rc::clone(&form)).await {
55                Ok(Ok(uploaded)) => uploaded,
56                Ok(Err(e)) => return Err(e.into()),
57                Err(e) => {
58                    if let Some(f) = &form.transform_error {
59                        return Err((f)(e));
60                    } else {
61                        return Err(e.into());
62                    }
63                }
64            };
65
66            Ok(Multipart(T::extract(uploaded)?))
67        })
68    }
69}