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