nyquest_interface/body/
multipart.rs

1//! Multipart form data definitions for HTTP requests.
2//!
3//! This module defines types for creating multipart/form-data bodies,
4//! which allow sending complex data including files in HTTP requests.
5
6use std::{borrow::Cow, fmt::Debug};
7
8/// Represents a part in a multipart form.
9///
10/// Each part has a name, optional filename, content type, and a body,
11/// along with optional additional headers.
12pub struct Part<S> {
13    /// Additional headers for this part.
14    pub headers: Vec<(Cow<'static, str>, Cow<'static, str>)>,
15    /// Name of the form field.
16    pub name: Cow<'static, str>,
17    /// Optional filename for file parts.
18    pub filename: Option<Cow<'static, str>>,
19    /// MIME content type for this part.
20    pub content_type: Cow<'static, str>,
21    /// Body content for this part.
22    pub body: PartBody<S>,
23}
24
25/// Body content for a multipart form part.
26///
27/// This can be either raw bytes or a stream.
28pub enum PartBody<S> {
29    /// Raw byte content.
30    Bytes {
31        /// The bytes that make up this part's content.
32        content: Cow<'static, [u8]>,
33    },
34    /// Streaming part data.
35    Stream(S),
36}
37
38impl<S> Debug for Part<S>
39where
40    S: Debug,
41{
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        f.debug_struct("Part")
44            .field("headers", &self.headers)
45            .field("name", &self.name)
46            .field("filename", &self.filename)
47            .field("content_type", &self.content_type)
48            .field("body", &self.body)
49            .finish()
50    }
51}
52
53impl<S> Debug for PartBody<S>
54where
55    S: Debug,
56{
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            PartBody::Bytes { content } => f
60                .debug_struct("PartBody::Bytes")
61                .field("content", content)
62                .finish(),
63            PartBody::Stream(stream) => f
64                .debug_struct("PartBody::Stream")
65                .field("stream", stream)
66                .finish(),
67        }
68    }
69}