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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::types::parameters::ChatId;
use serde::Serialize;
use std::{borrow::Cow, collections::HashSet};
enum Header<'a> {
Field(&'static str),
File {
name: Cow<'a, str>,
filename: &'a str,
},
}
impl<'a> Header<'a> {
pub fn content_disposition(&self) -> String {
match self {
Header::Field(name) => format!("name=\"{name}\""),
Header::File { name, filename } => {
format!("name=\"{name}\"; filename=\"{filename}\"")
}
}
}
}
struct Part<'a> {
header: Header<'a>,
body: Cow<'a, [u8]>,
}
pub struct Multipart<'a> {
parts: Vec<Part<'a>>,
}
impl<'a> Multipart<'a> {
pub fn new(capacity: usize) -> Self {
Self {
parts: Vec::with_capacity(capacity),
}
}
pub fn str(mut self, name: &'static str, value: &'a str) -> Self {
self.parts.push(Part {
header: Header::Field(name),
body: Cow::Borrowed(value.as_bytes()),
});
self
}
fn part(mut self, name: &'static str, body: Cow<'a, [u8]>) -> Self {
self.parts.push(Part {
header: Header::Field(name),
body,
});
self
}
pub fn string(self, name: &'static str, value: &impl ToString) -> Self {
self.part(name, Cow::Owned(value.to_string().into_bytes()))
}
pub fn json(self, name: &'static str, value: impl Serialize) -> Self {
self.part(name, Cow::Owned(serde_json::to_vec(&value).unwrap()))
}
pub fn maybe_str(self, name: &'static str, value: Option<&'a str>) -> Self {
match value {
Some(value) => self.str(name, value),
None => self,
}
}
pub fn maybe_string(
self,
name: &'static str,
value: Option<impl ToString>,
) -> Self {
match value {
Some(value) => self.string(name, &value),
None => self,
}
}
pub fn maybe_json(
self,
name: &'static str,
value: Option<impl Serialize>,
) -> Self {
match value {
Some(value) => self.json(name, &value),
None => self,
}
}
pub fn chat_id(self, name: &'static str, id: &'a ChatId) -> Self {
match id {
ChatId::Id(id) => self.string(name, &id),
ChatId::Username(username) => self.str(name, username),
}
}
fn file_cow(
mut self,
name: Cow<'a, str>,
filename: &'a str,
body: &'a [u8],
) -> Self {
self.parts.push(Part {
header: Header::File { name, filename },
body: Cow::Borrowed(body),
});
self
}
pub fn file(
self,
name: &'a str,
filename: &'a str,
body: &'a [u8],
) -> Self {
self.file_cow(Cow::Borrowed(name), filename, body)
}
pub fn file_owned_name(
self,
name: String,
filename: &'a str,
body: &'a [u8],
) -> Self {
self.file_cow(Cow::Owned(name), filename, body)
}
pub fn finish(self) -> (String, Vec<u8>) {
let mut line_lengths = HashSet::new();
for part in &self.parts {
let mut current_line_length = 0;
for line in part.body.split(|byte| *byte == b'\n') {
current_line_length += line.len();
if line.ends_with(b"\r") {
line_lengths.insert(current_line_length - 1);
current_line_length = 0;
} else {
current_line_length += 1; }
}
line_lengths.insert(current_line_length - 1);
}
let boundary_length = (1..=usize::max_value())
.find(|n| {
!line_lengths.contains(&(*n + 2)) && !line_lengths.contains(&(*n + 4)) })
.unwrap();
let boundary_string = "-".repeat(boundary_length);
let boundary = boundary_string.as_bytes();
let mut body = Vec::new();
for part in self.parts {
body.extend_from_slice(b"--");
body.extend_from_slice(boundary);
body.extend_from_slice(b"\r\n");
body.extend_from_slice(b"Content-Disposition: form-data; ");
body.extend_from_slice(
part.header.content_disposition().as_bytes(),
);
body.extend_from_slice(b"\r\n\r\n");
match part.body {
Cow::Owned(bytes) => body.extend_from_slice(&bytes[..]),
Cow::Borrowed(bytes) => body.extend_from_slice(bytes),
}
body.extend_from_slice(b"\r\n");
}
body.extend_from_slice(b"--");
body.extend_from_slice(boundary);
body.extend_from_slice(b"--\r\n");
(boundary_string, body)
}
}