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> {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        match self {
50            Body::Bytes {
51                content,
52                content_type,
53            } => f
54                .debug_struct("Body::Bytes")
55                .field("content", content)
56                .field("content_type", content_type)
57                .finish(),
58            Body::Form { fields } => f
59                .debug_struct("Body::Form")
60                .field("fields", fields)
61                .finish(),
62            #[cfg(feature = "multipart")]
63            Body::Multipart { parts: _ } => f.debug_struct("Body::Multipart").finish(),
64            Body::Stream {
65                stream: _,
66                content_type,
67            } => f
68                .debug_struct("Body::Stream")
69                .field("content_type", content_type)
70                .finish(),
71        }
72    }
73}