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/// A wrapper for streaming request body data.
16#[doc(hidden)]
17pub struct StreamReader<S> {
18    /// The underlying stream that provides the body data.
19    pub stream: S,
20    /// Optional content length of the stream, if known in advance.
21    pub content_length: Option<u64>,
22}
23
24/// Represents different types of HTTP request bodies.
25///
26/// This enum encapsulates the various body formats that can be sent in an HTTP request,
27/// including raw bytes, form data, and multipart forms.
28pub enum Body<S> {
29    /// Raw byte content with a specified content type.
30    Bytes {
31        /// The actual byte content of the body.
32        content: Cow<'static, [u8]>,
33        /// The MIME content type for the body.
34        content_type: Cow<'static, str>,
35    },
36    /// URL-encoded form data.
37    Form {
38        /// Collection of key-value pairs representing the form fields.
39        fields: Vec<(Cow<'static, str>, Cow<'static, str>)>,
40    },
41    /// Multipart form data, enabled with the "multipart" feature.
42    #[cfg(feature = "multipart")]
43    Multipart {
44        /// Collection of parts that make up the multipart form.
45        parts: Vec<Part<S>>,
46    },
47    /// Streaming body data.
48    #[doc(hidden)]
49    Stream(StreamReader<S>),
50}
51
52impl<S> Debug for StreamReader<S>
53where
54    S: Debug,
55{
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        f.debug_struct("StreamReader")
58            .field("stream", &self.stream)
59            .field("content_length", &self.content_length)
60            .finish()
61    }
62}
63
64impl<S> Debug for Body<S>
65where
66    S: Debug,
67{
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        match self {
70            Body::Bytes {
71                content,
72                content_type,
73            } => f
74                .debug_struct("Body::Bytes")
75                .field("content", content)
76                .field("content_type", content_type)
77                .finish(),
78            Body::Form { fields } => f
79                .debug_struct("Body::Form")
80                .field("fields", fields)
81                .finish(),
82            #[cfg(feature = "multipart")]
83            Body::Multipart { parts } => f
84                .debug_struct("Body::Multipart")
85                .field("parts", parts)
86                .finish(),
87            Body::Stream(stream) => f
88                .debug_struct("Body::Stream")
89                .field("stream", stream)
90                .finish(),
91        }
92    }
93}
94
95impl<S> Clone for Body<S>
96where
97    S: Clone,
98{
99    fn clone(&self) -> Self {
100        match self {
101            Body::Bytes {
102                content,
103                content_type,
104            } => Body::Bytes {
105                content: content.clone(),
106                content_type: content_type.clone(),
107            },
108            Body::Form { fields } => Body::Form {
109                fields: fields.clone(),
110            },
111            #[cfg(feature = "multipart")]
112            Body::Multipart { parts } => Body::Multipart {
113                parts: parts.clone(),
114            },
115            Body::Stream(stream) => Body::Stream(stream.clone()),
116        }
117    }
118}
119
120impl<S> Clone for StreamReader<S>
121where
122    S: Clone,
123{
124    fn clone(&self) -> Self {
125        Self {
126            stream: self.stream.clone(),
127            content_length: self.content_length,
128        }
129    }
130}