nyquest_interface/
body.rs

1//! Request body types for nyquest HTTP clients.
2//!
3//! This module defines the various body types that can be used in HTTP requests,
4//! including byte content, form data, and multipart forms.
5
6use std::{borrow::Cow, fmt::Debug};
7
8#[cfg(feature = "multipart")]
9#[cfg_attr(docsrs, doc(cfg(feature = "multipart")))]
10mod multipart;
11#[cfg(feature = "multipart")]
12#[cfg_attr(docsrs, doc(cfg(feature = "multipart")))]
13pub use multipart::{Part, PartBody};
14
15/// Represents different types of HTTP request bodies.
16///
17/// This enum encapsulates the various body formats that can be sent in an HTTP request,
18/// including raw bytes, form data, and multipart forms.
19pub enum Body<S> {
20    /// Raw byte content with a specified content type.
21    Bytes {
22        /// The actual byte content of the body.
23        content: Cow<'static, [u8]>,
24        /// The MIME content type for the body.
25        content_type: Cow<'static, str>,
26    },
27    /// URL-encoded form data.
28    Form {
29        /// Collection of key-value pairs representing the form fields.
30        fields: Vec<(Cow<'static, str>, Cow<'static, str>)>,
31    },
32    /// Multipart form data, enabled with the "multipart" feature.
33    #[cfg(feature = "multipart")]
34    Multipart {
35        /// Collection of parts that make up the multipart form.
36        parts: Vec<Part<S>>,
37    },
38    /// Streaming body data.
39    Stream {
40        /// The underlying stream that provides the body data.
41        stream: S,
42        /// The MIME content type for the stream.
43        content_type: Cow<'static, str>,
44    },
45}
46
47impl<S> Debug for Body<S>
48where
49    S: Debug,
50{
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        match self {
53            Body::Bytes {
54                content,
55                content_type,
56            } => f
57                .debug_struct("Body::Bytes")
58                .field("content", content)
59                .field("content_type", content_type)
60                .finish(),
61            Body::Form { fields } => f
62                .debug_struct("Body::Form")
63                .field("fields", fields)
64                .finish(),
65            #[cfg(feature = "multipart")]
66            Body::Multipart { parts } => f
67                .debug_struct("Body::Multipart")
68                .field("parts", parts)
69                .finish(),
70            Body::Stream {
71                stream: reader,
72                content_type,
73            } => f
74                .debug_struct("Body::Stream")
75                .field("reader", reader)
76                .field("content_type", content_type)
77                .finish(),
78        }
79    }
80}