|
| 1 | +use crate::{Body, Mime}; |
| 2 | + |
| 3 | +use std::fmt::{self, Debug}; |
| 4 | +// use std::path::Path; |
| 5 | +use std::pin::Pin; |
| 6 | +use std::task::{Context, Poll}; |
| 7 | + |
| 8 | +use futures_lite::{io, prelude::*}; |
| 9 | + |
| 10 | +pin_project_lite::pin_project! { |
| 11 | + /// A single multipart entry. |
| 12 | + /// |
| 13 | + /// Structurally Multipart entries are similar to `Body`. |
| 14 | + pub struct Entry { |
| 15 | + name: String, |
| 16 | + body: Body, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl Entry { |
| 21 | + /// Create a new `Entry`. |
| 22 | + pub fn new<S, B>(name: S, body: B) -> Self |
| 23 | + where |
| 24 | + S: AsRef<str>, |
| 25 | + B: Into<Body>, |
| 26 | + { |
| 27 | + Self { |
| 28 | + name: name.as_ref().to_owned(), |
| 29 | + body: body.into(), |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// Create an empty `Entry`. |
| 34 | + pub fn empty<S>(name: S) -> Self |
| 35 | + where |
| 36 | + S: AsRef<str>, |
| 37 | + { |
| 38 | + Self::new(name, Body::empty()) |
| 39 | + } |
| 40 | + |
| 41 | + /// Create an `Entry` from a file. |
| 42 | + #[cfg(all(feature = "async_std", not(target_os = "unknown")))] |
| 43 | + pub async fn from_file<S, P>(name: S, path: P) -> crate::Result<Self> |
| 44 | + where |
| 45 | + S: AsRef<str>, |
| 46 | + P: AsRef<Path>, |
| 47 | + { |
| 48 | + let body = Body::from_file(path).await?; |
| 49 | + Ok(Self::new(name, body)) |
| 50 | + } |
| 51 | + |
| 52 | + /// Get the entry name. |
| 53 | + pub fn name(&self) -> &String { |
| 54 | + &self.name |
| 55 | + } |
| 56 | + |
| 57 | + /// Set the entry name. |
| 58 | + pub fn set_name<S>(&mut self, name: S) |
| 59 | + where |
| 60 | + S: AsRef<str>, |
| 61 | + { |
| 62 | + self.name = name.as_ref().to_owned(); |
| 63 | + } |
| 64 | + |
| 65 | + /// Returns the mime type of this Body. |
| 66 | + pub fn mime(&self) -> &Mime { |
| 67 | + self.body.mime() |
| 68 | + } |
| 69 | + |
| 70 | + /// Sets the mime type of this Body. |
| 71 | + pub fn set_mime(&mut self, mime: Mime) { |
| 72 | + self.body.set_mime(mime) |
| 73 | + } |
| 74 | + |
| 75 | + /// Get the file name of the entry, if it's set. |
| 76 | + pub fn file_name(&self) -> Option<&str> { |
| 77 | + self.body.file_name() |
| 78 | + } |
| 79 | + |
| 80 | + /// Set the file name of the `Body`. |
| 81 | + pub fn set_file_name<P>(&mut self, file_name: Option<P>) |
| 82 | + where |
| 83 | + P: AsRef<str>, |
| 84 | + { |
| 85 | + self.body.set_file_name(file_name); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl Debug for Entry { |
| 90 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 91 | + f.debug_struct("Entry") |
| 92 | + .field("name", &self.name) |
| 93 | + .field("body", &self.body) |
| 94 | + .finish() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl AsyncRead for Entry { |
| 99 | + #[allow(missing_doc_code_examples)] |
| 100 | + fn poll_read( |
| 101 | + mut self: Pin<&mut Self>, |
| 102 | + cx: &mut Context<'_>, |
| 103 | + buf: &mut [u8], |
| 104 | + ) -> Poll<io::Result<usize>> { |
| 105 | + Pin::new(&mut self.body).poll_read(cx, buf) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl AsyncBufRead for Entry { |
| 110 | + #[allow(missing_doc_code_examples)] |
| 111 | + #[allow(unused_mut)] |
| 112 | + #[allow(unused_variables)] |
| 113 | + fn poll_fill_buf(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> { |
| 114 | + // Pin::new(&mut self.body).poll_fill_buf(cx) |
| 115 | + todo!("Pin::new(&mut self.body).poll_fill_buf(cx)") |
| 116 | + } |
| 117 | + |
| 118 | + fn consume(mut self: Pin<&mut Self>, amt: usize) { |
| 119 | + Pin::new(&mut self.body).consume(amt) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +impl AsRef<Body> for Entry { |
| 124 | + fn as_ref(&self) -> &Body { |
| 125 | + &self.body |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +impl AsMut<Body> for Entry { |
| 130 | + fn as_mut(&mut self) -> &mut Body { |
| 131 | + &mut self.body |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl Into<Body> for Entry { |
| 136 | + fn into(self) -> Body { |
| 137 | + self.body |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl From<Body> for Entry { |
| 142 | + fn from(body: Body) -> Self { |
| 143 | + match body.file_name.clone() { |
| 144 | + Some(name) => Self { body, name }, |
| 145 | + None => Self { |
| 146 | + body, |
| 147 | + name: String::new(), |
| 148 | + }, |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments