From 59078db50af2e2459684fef3e59b273ad03de5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Thu, 17 Oct 2024 23:50:40 +0200 Subject: [PATCH] Fix compilation for Rust 1.75 and remove unwrap --- src/cipher.rs | 10 +++++----- src/envelope.rs | 2 +- src/messagepipe.rs | 2 +- src/push_service/cdn.rs | 18 +++++++++++++----- src/push_service/error.rs | 2 +- src/websocket/mod.rs | 6 +++--- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/cipher.rs b/src/cipher.rs index 09649b679..d787f9472 100644 --- a/src/cipher.rs +++ b/src/cipher.rs @@ -134,7 +134,7 @@ where let ciphertext = if let Some(msg) = envelope.content.as_ref() { msg } else { - return Err(ServiceError::InvalidFrameError { + return Err(ServiceError::InvalidFrame { reason: "Envelope should have either a legacy message or content." .into(), @@ -311,7 +311,7 @@ where }, _ => { // else - return Err(ServiceError::InvalidFrameError { + return Err(ServiceError::InvalidFrame { reason: format!( "Envelope has unknown type {:?}.", envelope.r#type() @@ -408,7 +408,7 @@ struct Plaintext { #[allow(clippy::comparison_chain)] fn add_padding(version: u32, contents: &[u8]) -> Result, ServiceError> { if version < 2 { - Err(ServiceError::InvalidFrameError { + Err(ServiceError::InvalidFrame { reason: format!("Unknown version {}", version), }) } else if version == 2 { @@ -436,7 +436,7 @@ fn strip_padding_version( contents: &mut Vec, ) -> Result<(), ServiceError> { if version < 2 { - Err(ServiceError::InvalidFrameError { + Err(ServiceError::InvalidFrame { reason: format!("Unknown version {}", version), }) } else if version == 2 { @@ -450,7 +450,7 @@ fn strip_padding_version( #[allow(clippy::comparison_chain)] fn strip_padding(contents: &mut Vec) -> Result<(), ServiceError> { let new_length = Iso7816::raw_unpad(contents) - .map_err(|e| ServiceError::InvalidFrameError { + .map_err(|e| ServiceError::InvalidFrame { reason: format!("Invalid message padding: {:?}", e), })? .len(); diff --git a/src/envelope.rs b/src/envelope.rs index 1a33de669..694700b89 100644 --- a/src/envelope.rs +++ b/src/envelope.rs @@ -42,7 +42,7 @@ impl Envelope { if input.len() < VERSION_LENGTH || input[VERSION_OFFSET] != SUPPORTED_VERSION { - return Err(ServiceError::InvalidFrameError { + return Err(ServiceError::InvalidFrame { reason: "Unsupported signaling cryptogram version".into(), }); } diff --git a/src/messagepipe.rs b/src/messagepipe.rs index 4fd1d54d4..711ed0cff 100644 --- a/src/messagepipe.rs +++ b/src/messagepipe.rs @@ -79,7 +79,7 @@ impl MessagePipe { let body = if let Some(body) = request.body.as_ref() { body } else { - return Err(ServiceError::InvalidFrameError { + return Err(ServiceError::InvalidFrame { reason: "Request without body.".into(), }); }; diff --git a/src/push_service/cdn.rs b/src/push_service/cdn.rs index b88c9e901..51b19abe2 100644 --- a/src/push_service/cdn.rs +++ b/src/push_service/cdn.rs @@ -32,12 +32,20 @@ impl PushService { &mut self, ptr: &AttachmentPointer, ) -> Result { - let id = match ptr.attachment_identifier.as_ref().unwrap() { - AttachmentIdentifier::CdnId(id) => &id.to_string(), - AttachmentIdentifier::CdnKey(key) => key, + let path = match ptr.attachment_identifier.as_ref() { + Some(AttachmentIdentifier::CdnId(id)) => { + format!("attachments/{}", id) + }, + Some(AttachmentIdentifier::CdnKey(key)) => { + format!("attachments/{}", key) + }, + None => { + return Err(ServiceError::InvalidFrame { + reason: "no attachment identifier in pointer".into(), + }); + }, }; - self.get_from_cdn(ptr.cdn_number(), &format!("attachments/{}", id)) - .await + self.get_from_cdn(ptr.cdn_number(), &path).await } #[tracing::instrument(skip(self))] diff --git a/src/push_service/error.rs b/src/push_service/error.rs index 99561c9e7..6efa1ec38 100644 --- a/src/push_service/error.rs +++ b/src/push_service/error.rs @@ -45,7 +45,7 @@ pub enum ServiceError { WsClosing { reason: &'static str }, #[error("Invalid frame: {reason}")] - InvalidFrameError { reason: String }, + InvalidFrame { reason: String }, #[error("MAC error")] MacError, diff --git a/src/websocket/mod.rs b/src/websocket/mod.rs index 76d7d362b..48b00ceb7 100644 --- a/src/websocket/mod.rs +++ b/src/websocket/mod.rs @@ -122,7 +122,7 @@ impl SignalWebSocketProcess { use web_socket_message::Type; match (msg.r#type(), msg.request, msg.response) { - (Type::Unknown, _, _) => Err(ServiceError::InvalidFrameError { + (Type::Unknown, _, _) => Err(ServiceError::InvalidFrame { reason: "Unknown frame type".into(), }), (Type::Request, Some(request), _) => { @@ -137,7 +137,7 @@ impl SignalWebSocketProcess { Ok(()) }, - (Type::Request, None, _) => Err(ServiceError::InvalidFrameError { + (Type::Request, None, _) => Err(ServiceError::InvalidFrame { reason: "Type was request, but does not contain request." .into(), }), @@ -175,7 +175,7 @@ impl SignalWebSocketProcess { Ok(()) }, - (Type::Response, _, None) => Err(ServiceError::InvalidFrameError { + (Type::Response, _, None) => Err(ServiceError::InvalidFrame { reason: "Type was response, but does not contain response." .into(), }),