axum_test/multipart/
multipart_form.rs1use crate::multipart::Part;
2use axum::body::Body as AxumBody;
3use rust_multipart_rfc7578_2::client::multipart::Body as CommonMultipartBody;
4use rust_multipart_rfc7578_2::client::multipart::Form;
5use std::fmt::Display;
6use std::io::Cursor;
7
8#[derive(Debug)]
9pub struct MultipartForm {
10 inner: Form<'static>,
11}
12
13impl MultipartForm {
14 pub fn new() -> Self {
15 Default::default()
16 }
17
18 pub fn add_text<N, T>(mut self, name: N, text: T) -> Self
20 where
21 N: Display,
22 T: ToString,
23 {
24 self.inner.add_text(name, text.to_string());
25 self
26 }
27
28 pub fn add_part<N>(mut self, name: N, part: Part) -> Self
32 where
33 N: Display,
34 {
35 let reader = Cursor::new(part.bytes);
36 self.inner.add_reader_2(
37 name,
38 reader,
39 part.file_name,
40 Some(part.mime_type),
41 part.headers,
42 );
43
44 self
45 }
46
47 pub fn content_type(&self) -> String {
49 self.inner.content_type()
50 }
51}
52
53impl Default for MultipartForm {
54 fn default() -> Self {
55 Self {
56 inner: Default::default(),
57 }
58 }
59}
60
61impl From<MultipartForm> for AxumBody {
62 fn from(multipart: MultipartForm) -> Self {
63 let inner_body: CommonMultipartBody = multipart.inner.into();
64 AxumBody::from_stream(inner_body)
65 }
66}