Skip to content

Commit 9b05b75

Browse files
committed
Simplify struct names for editing attachments (serenity-rs#3351)
1 parent 7813f16 commit 9b05b75

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

src/builder/create_attachment.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,10 @@ impl<'a> ImageData<'a> {
173173
}
174174
}
175175

176-
#[derive(Clone, Debug, Serialize)]
177-
struct ExistingAttachment {
178-
id: AttachmentId,
179-
}
180-
181176
#[derive(Clone, Debug)]
182-
enum NewOrExisting<'a> {
177+
enum EditAttachmentsInner<'a> {
183178
New(CreateAttachment<'a>),
184-
Existing(ExistingAttachment),
179+
Existing(AttachmentId),
185180
}
186181

187182
/// You can add new attachments and edit existing ones using this builder.
@@ -242,7 +237,7 @@ enum NewOrExisting<'a> {
242237
#[derive(Default, Debug, Clone)]
243238
#[must_use]
244239
pub struct EditAttachments<'a> {
245-
new_and_existing_attachments: Vec<NewOrExisting<'a>>,
240+
inner: Vec<EditAttachmentsInner<'a>>,
246241
}
247242

248243
impl<'a> EditAttachments<'a> {
@@ -266,15 +261,7 @@ impl<'a> EditAttachments<'a> {
266261
/// Discord will throw an error!**
267262
pub fn keep_all(msg: &Message) -> Self {
268263
Self {
269-
new_and_existing_attachments: msg
270-
.attachments
271-
.iter()
272-
.map(|a| {
273-
NewOrExisting::Existing(ExistingAttachment {
274-
id: a.id,
275-
})
276-
})
277-
.collect(),
264+
inner: msg.attachments.iter().map(|a| EditAttachmentsInner::Existing(a.id)).collect(),
278265
}
279266
}
280267

@@ -283,9 +270,7 @@ impl<'a> EditAttachments<'a> {
283270
///
284271
/// Opposite of [`Self::remove`].
285272
pub fn keep(mut self, id: AttachmentId) -> Self {
286-
self.new_and_existing_attachments.push(NewOrExisting::Existing(ExistingAttachment {
287-
id,
288-
}));
273+
self.inner.push(EditAttachmentsInner::Existing(id));
289274
self
290275
}
291276

@@ -294,28 +279,28 @@ impl<'a> EditAttachments<'a> {
294279
///
295280
/// Opposite of [`Self::keep`].
296281
pub fn remove(mut self, id: AttachmentId) -> Self {
297-
self.new_and_existing_attachments.retain(|a| match a {
298-
NewOrExisting::Existing(a) => a.id != id,
299-
NewOrExisting::New(_) => true,
282+
self.inner.retain(|a| match a {
283+
EditAttachmentsInner::Existing(existing_id) => *existing_id != id,
284+
EditAttachmentsInner::New(_) => true,
300285
});
301286
self
302287
}
303288

304289
/// Adds a new attachment to the attachment list.
305290
#[expect(clippy::should_implement_trait)] // Clippy thinks add == std::ops::Add::add
306291
pub fn add(mut self, attachment: CreateAttachment<'a>) -> Self {
307-
self.new_and_existing_attachments.push(NewOrExisting::New(attachment));
292+
self.inner.push(EditAttachmentsInner::New(attachment));
308293
self
309294
}
310295

311296
/// Clones all new attachments into a new Vec, keeping only data and filename, because those
312297
/// are needed for the multipart form data.
313298
#[cfg(feature = "http")]
314299
pub(crate) fn new_attachments(&self) -> Vec<CreateAttachment<'a>> {
315-
self.new_and_existing_attachments
300+
self.inner
316301
.iter()
317302
.filter_map(|attachment| {
318-
if let NewOrExisting::New(attachment) = &attachment {
303+
if let EditAttachmentsInner::New(attachment) = &attachment {
319304
Some(attachment.clone())
320305
} else {
321306
None
@@ -334,14 +319,19 @@ impl Serialize for EditAttachments<'_> {
334319
description: &'a Option<Cow<'a, str>>,
335320
}
336321

322+
#[derive(Serialize)]
323+
struct ExistingAttachment {
324+
id: AttachmentId,
325+
}
326+
337327
// Instead of an `AttachmentId`, the `id` field for new attachments corresponds to the
338328
// index of the new attachment in the multipart payload. The attachment data will be
339329
// labeled with `files[{id}]` in the multipart body. See `Multipart::build_form`.
340330
let mut id = 0;
341-
let mut seq = serializer.serialize_seq(Some(self.new_and_existing_attachments.len()))?;
342-
for attachment in &self.new_and_existing_attachments {
331+
let mut seq = serializer.serialize_seq(Some(self.inner.len()))?;
332+
for attachment in &self.inner {
343333
match attachment {
344-
NewOrExisting::New(new_attachment) => {
334+
EditAttachmentsInner::New(new_attachment) => {
345335
let attachment = NewAttachment {
346336
id,
347337
filename: &new_attachment.filename,
@@ -350,8 +340,10 @@ impl Serialize for EditAttachments<'_> {
350340
id += 1;
351341
seq.serialize_element(&attachment)?;
352342
},
353-
NewOrExisting::Existing(existing_attachment) => {
354-
seq.serialize_element(existing_attachment)?;
343+
EditAttachmentsInner::Existing(id) => {
344+
seq.serialize_element(&ExistingAttachment {
345+
id: *id,
346+
})?;
355347
},
356348
}
357349
}

0 commit comments

Comments
 (0)