actix_form_data/lib.rs
1/*
2 * This file is part of Actix Form Data.
3 *
4 * Copyright © 2020 Riley Trautman
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
20//! # Actix Form Data
21//! A library for retrieving form data from Actix Web's multipart streams. It can stream
22//! uploaded files onto the filesystem (its main purpose), but it can also parse associated
23//! form data.
24//!
25//! # Example
26//!
27//!```rust
28//! use actix_form_data::{Error, Field, Form, Value};
29//! use actix_web::{
30//! web::{post, resource},
31//! App, HttpResponse, HttpServer,
32//! };
33//! use futures_util::stream::StreamExt;
34//!
35//! async fn upload(uploaded_content: Value<()>) -> HttpResponse {
36//! println!("Uploaded Content: {:#?}", uploaded_content);
37//! HttpResponse::Created().finish()
38//! }
39//!
40//! #[actix_rt::main]
41//! async fn main() -> Result<(), anyhow::Error> {
42//! let form = Form::new()
43//! .field("Hey", Field::text())
44//! .field(
45//! "Hi",
46//! Field::map()
47//! .field("One", Field::int())
48//! .field("Two", Field::float())
49//! .finalize(),
50//! )
51//! .field(
52//! "files",
53//! Field::array(Field::file(|_, _, mut stream| async move {
54//! while let Some(_) = stream.next().await {
55//! // do something
56//! }
57//! Ok(()) as Result<(), Error>
58//! })),
59//! );
60//!
61//! println!("{:?}", form);
62//!
63//! HttpServer::new(move || {
64//! App::new()
65//! .wrap(form.clone())
66//! .service(resource("/upload").route(post().to(upload)))
67//! })
68//! .bind("127.0.0.1:8082")?;
69//! // commented out to prevent infinite doctest
70//! // .run()
71//! // .await?;
72//!
73//! Ok(())
74//! }
75//!```
76
77mod error;
78mod middleware;
79mod types;
80mod upload;
81
82pub use self::{
83 error::Error,
84 types::{Field, FileMeta, Form, Value},
85 upload::handle_multipart,
86};