From 6732640f960741aef44d7b30e03203eb61236cfb Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Fri, 12 May 2023 12:33:39 +0200 Subject: [PATCH 1/4] impl PartialDiscordFileAttachment Now contains move_$ methods, which can move individual values out of the PartialDiscordFileAttachment while preserving the rest of the object. --- src/api/types.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/api/types.rs b/src/api/types.rs index 00d47a7..5322b95 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -847,7 +847,7 @@ pub struct DiscordFileAttachment { pub struct PartialDiscordFileAttachment { pub id: Option, - pub filename: Option, + pub filename: String, pub description: Option, pub content_type: Option, pub size: Option, @@ -862,6 +862,73 @@ pub struct PartialDiscordFileAttachment { pub content: Vec, } +impl PartialDiscordFileAttachment { + /** + Moves `self.content` out of `self` and returns it. + # Returns + Vec + */ + pub fn move_content(self) -> (Vec, PartialDiscordFileAttachment) { + let content = self.content; + let updated_struct = PartialDiscordFileAttachment { + id: self.id, + filename: self.filename, + description: self.description, + content_type: self.content_type, + size: self.size, + url: self.url, + proxy_url: self.proxy_url, + height: self.height, + width: self.width, + ephemeral: self.ephemeral, + duration_secs: self.duration_secs, + waveform: self.waveform, + content: Vec::new(), + }; + (content, updated_struct) + } + + pub fn move_filename(self) -> (String, PartialDiscordFileAttachment) { + let filename = self.filename; + let updated_struct = PartialDiscordFileAttachment { + id: self.id, + filename: String::new(), + description: self.description, + content_type: self.content_type, + size: self.size, + url: self.url, + proxy_url: self.proxy_url, + height: self.height, + width: self.width, + ephemeral: self.ephemeral, + duration_secs: self.duration_secs, + waveform: self.waveform, + content: self.content, + }; + (filename, updated_struct) + } + + pub fn move_content_type(self) -> (Option, PartialDiscordFileAttachment) { + let content_type = self.content_type; + let updated_struct = PartialDiscordFileAttachment { + id: self.id, + filename: self.filename, + description: self.description, + content_type: None, + size: self.size, + url: self.url, + proxy_url: self.proxy_url, + height: self.height, + width: self.width, + ephemeral: self.ephemeral, + duration_secs: self.duration_secs, + waveform: self.waveform, + content: self.content, + }; + (content_type, updated_struct) + } +} + #[derive(Debug, Serialize, Deserialize)] pub struct AllowedMention { parse: Vec, From 9fd3f733f7580c7e2bd329e2af62cce5d710989d Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Fri, 12 May 2023 12:34:00 +0200 Subject: [PATCH 2/4] Mark unused var as unused --- src/api/auth/register.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index ec0ecb3..28c9c96 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -109,6 +109,6 @@ mod test { None, ) .unwrap(); - let token = test_instance.register_account(®).await.unwrap().token; + let _ = test_instance.register_account(®).await.unwrap().token; } } From 56be2c9a17086728cc5de6af199bf2d862e1e18f Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Fri, 12 May 2023 12:34:27 +0200 Subject: [PATCH 3/4] Change send to no longer take static refs --- src/api/channels/messages.rs | 49 +++++++++++++++--------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 3f34100..6594df0 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -1,6 +1,6 @@ pub mod messages { use http::header::CONTENT_DISPOSITION; - use http::{header, HeaderMap}; + use http::HeaderMap; use reqwest::{multipart, Client}; use serde_json::to_string; @@ -24,7 +24,7 @@ pub mod messages { url_api: &String, channel_id: &String, message: &mut crate::api::schemas::MessageSendSchema, - files: Option<&'static Vec>, + files: Option>, token: &String, user: &mut User<'a>, ) -> Result { @@ -61,36 +61,30 @@ pub mod messages { form = form.part("payload_json", payload_field); - for (index, attachment) in files.iter().enumerate() { + for (index, attachment) in files.unwrap().into_iter().enumerate() { + let (attachment_content, current_attachment) = attachment.move_content(); + let (attachment_filename, current_attachment) = + current_attachment.move_filename(); + let (attachment_content_type, _) = current_attachment.move_content_type(); let part_name = format!("files[{}]", index); let content_disposition = format!( "form-data; name=\"{}\"'; filename=\"{}\"", - part_name, - attachment - .get(index) - .unwrap() - .filename - .as_deref() - .unwrap_or("file") + part_name, &attachment_filename ); let mut header_map = HeaderMap::new(); header_map .insert(CONTENT_DISPOSITION, content_disposition.parse().unwrap()) .unwrap(); - let mut part = - multipart::Part::bytes(attachment.get(index).unwrap().content.as_slice()) - .file_name( - attachment - .get(index) - .unwrap() - .filename - .as_deref() - .unwrap_or("file"), - ) - .headers(header_map); + let mut part = multipart::Part::bytes(attachment_content) + .file_name(attachment_filename) + .headers(header_map); - part = match part.mime_str("application/octet-stream") { + part = match part.mime_str( + attachment_content_type + .unwrap_or("application/octet-stream".to_string()) + .as_str(), + ) { Ok(part) => part, Err(e) => { return Err(InstanceServerError::MultipartCreationError { @@ -115,7 +109,6 @@ pub mod messages { user_rate_limits, ) .await - // TODO: Deallocate the darn memory leak! } } } @@ -125,7 +118,7 @@ pub mod messages { &mut self, message: &mut crate::api::schemas::MessageSendSchema, channel_id: &String, - files: Option<&'static Vec>, + files: Option>, ) -> Result { let token = self.token().clone(); Message::send( @@ -143,8 +136,6 @@ pub mod messages { #[cfg(test)] mod test { - use std::borrow::Cow; - use crate::{ api::{AuthUsername, LoginSchema}, instance::Instance, @@ -190,7 +181,7 @@ mod test { let settings = login_result.settings; let limits = instance.limits.clone(); let mut user = crate::api::types::User::new(&mut instance, token, limits, settings, None); - let response = user + let _ = user .send_message(&mut message, &channel_id, None) .await .unwrap(); @@ -202,7 +193,7 @@ mod test { let attachment = crate::api::types::PartialDiscordFileAttachment { id: None, - filename: Some("test".to_string()), + filename: "test".to_string(), description: None, content_type: None, size: None, @@ -255,7 +246,7 @@ mod test { let vec_attach = vec![attachment.clone()]; let arg = Some(&vec_attach); let response = user - .send_message(&mut message, &channel_id, arg) + .send_message(&mut message, &channel_id, Some(vec![attachment.clone()])) .await .unwrap(); } From 32f337015a427c7f9f0dda67eaa748ea2e1e12e2 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Fri, 12 May 2023 12:35:06 +0200 Subject: [PATCH 4/4] cargo fix --- src/api/channels/messages.rs | 4 ++-- src/api/policies/instance/instance.rs | 2 +- src/api/schemas.rs | 2 +- src/limit.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 6594df0..a099418 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -244,8 +244,8 @@ mod test { let limits = instance.limits.clone(); let mut user = crate::api::types::User::new(&mut instance, token, limits, settings, None); let vec_attach = vec![attachment.clone()]; - let arg = Some(&vec_attach); - let response = user + let _arg = Some(&vec_attach); + let _response = user .send_message(&mut message, &channel_id, Some(vec![attachment.clone()])) .await .unwrap(); diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index b13aa89..b864e54 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -51,6 +51,6 @@ mod instance_policies_schema_test { .await .unwrap(); - let schema = test_instance.instance_policies_schema().await.unwrap(); + let _schema = test_instance.instance_policies_schema().await.unwrap(); } } diff --git a/src/api/schemas.rs b/src/api/schemas.rs index ccdff53..edd8720 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; + use regex::Regex; use serde::{Deserialize, Serialize}; diff --git a/src/limit.rs b/src/limit.rs index e2a6a59..82f5732 100644 --- a/src/limit.rs +++ b/src/limit.rs @@ -329,6 +329,6 @@ mod rate_limit { Ok(result) => result, Err(_) => panic!("Request failed"), }; - let config: Config = from_str(result.text().await.unwrap().as_str()).unwrap(); + let _config: Config = from_str(result.text().await.unwrap().as_str()).unwrap(); } }