From c523fa0f746f0d42f6a9b50036dae730f19dffc6 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 5 Apr 2021 22:33:05 +0200 Subject: [PATCH] Use ruma_api::try_deserialize in manual endpoint implementations --- ruma-api/tests/manual_endpoint_impl.rs | 32 +++++------- .../src/r0/message/send_message_event.rs | 48 +++++++----------- .../src/r0/state/get_state_events_for_key.rs | 47 +++++++----------- .../src/r0/state/send_state_event.rs | 49 +++++++------------ 4 files changed, 66 insertions(+), 110 deletions(-) diff --git a/ruma-api/tests/manual_endpoint_impl.rs b/ruma-api/tests/manual_endpoint_impl.rs index d28c067d..62f6ad5c 100644 --- a/ruma-api/tests/manual_endpoint_impl.rs +++ b/ruma-api/tests/manual_endpoint_impl.rs @@ -1,14 +1,14 @@ //! PUT /_matrix/client/r0/directory/room/:room_alias -use std::{convert::TryFrom, ops::Deref}; +use std::convert::TryFrom; use http::{header::CONTENT_TYPE, method::Method}; use ruma_api::{ error::{ - FromHttpRequestError, FromHttpResponseError, IntoHttpError, RequestDeserializationError, - ResponseDeserializationError, ServerError, Void, + FromHttpRequestError, FromHttpResponseError, IntoHttpError, ResponseDeserializationError, + ServerError, Void, }, - AuthScheme, IncomingRequest, Metadata, OutgoingRequest, + try_deserialize, AuthScheme, IncomingRequest, Metadata, OutgoingRequest, }; use ruma_identifiers::{RoomAliasId, RoomId}; use ruma_serde::Outgoing; @@ -71,25 +71,19 @@ impl IncomingRequest for Request { fn try_from_http_request( request: http::Request>, ) -> Result { - let request_body: RequestBody = match serde_json::from_slice(request.body().as_slice()) { - Ok(body) => body, - Err(err) => { - return Err(RequestDeserializationError::new(err, request).into()); - } - }; + let request_body: RequestBody = + try_deserialize!(request, serde_json::from_slice(request.body().as_slice())); let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect(); + Ok(Request { room_id: request_body.room_id, room_alias: { - let segment = path_segments.get(5).unwrap().as_bytes(); - let decoded = match percent_encoding::percent_decode(segment).decode_utf8() { - Ok(x) => x, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; - match serde_json::from_str(decoded.deref()) { - Ok(id) => id, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } + let decoded = try_deserialize!( + request, + percent_encoding::percent_decode(path_segments[5].as_bytes()).decode_utf8(), + ); + + try_deserialize!(request, TryFrom::try_from(&*decoded)) }, }) } diff --git a/ruma-client-api/src/r0/message/send_message_event.rs b/ruma-client-api/src/r0/message/send_message_event.rs index 06f53982..8d7675e7 100644 --- a/ruma-client-api/src/r0/message/send_message_event.rs +++ b/ruma-client-api/src/r0/message/send_message_event.rs @@ -3,8 +3,8 @@ use std::convert::TryFrom; use ruma_api::{ - error::{FromHttpRequestError, IntoHttpError, RequestDeserializationError}, - ruma_api, Metadata, + error::{FromHttpRequestError, IntoHttpError}, + ruma_api, try_deserialize, Metadata, }; use ruma_events::{AnyMessageEventContent, EventContent as _}; use ruma_identifiers::{EventId, RoomId}; @@ -115,42 +115,30 @@ impl ruma_api::IncomingRequest for IncomingRequest { let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect(); let room_id = { - let decoded = - match percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; + let decoded = try_deserialize!( + request, + percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8(), + ); - match RoomId::try_from(&*decoded) { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } + try_deserialize!(request, RoomId::try_from(&*decoded)) }; - let txn_id = - match percent_encoding::percent_decode(path_segments[7].as_bytes()).decode_utf8() { - Ok(val) => val.into_owned(), - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; + let txn_id = try_deserialize!( + request, + percent_encoding::percent_decode(path_segments[7].as_bytes()).decode_utf8(), + ) + .into_owned(); let content = { let request_body: Box = - match serde_json::from_slice(request.body().as_slice()) { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; + try_deserialize!(request, serde_json::from_slice(request.body().as_slice())); - let event_type = { - match percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } - }; + let event_type = try_deserialize!( + request, + percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() + ); - match AnyMessageEventContent::from_parts(&event_type, request_body) { - Ok(content) => content, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } + try_deserialize!(request, AnyMessageEventContent::from_parts(&event_type, request_body)) }; Ok(Self { room_id, txn_id, content }) diff --git a/ruma-client-api/src/r0/state/get_state_events_for_key.rs b/ruma-client-api/src/r0/state/get_state_events_for_key.rs index 48ad8b77..274a6bb9 100644 --- a/ruma-client-api/src/r0/state/get_state_events_for_key.rs +++ b/ruma-client-api/src/r0/state/get_state_events_for_key.rs @@ -3,8 +3,8 @@ use std::{borrow::Cow, convert::TryFrom}; use ruma_api::{ - error::{FromHttpRequestError, IntoHttpError, RequestDeserializationError}, - ruma_api, Metadata, + error::{FromHttpRequestError, IntoHttpError}, + ruma_api, try_deserialize, Metadata, }; use ruma_events::EventType; use ruma_identifiers::RoomId; @@ -117,44 +117,31 @@ impl ruma_api::IncomingRequest for IncomingRequest { let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect(); let room_id = { - let decoded = - match percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; + let decoded = try_deserialize!( + request, + percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() + ); - match RoomId::try_from(&*decoded) { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } + try_deserialize!(request, RoomId::try_from(&*decoded)) }; let event_type = { - let decoded = - match percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; + let decoded = try_deserialize!( + request, + percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() + ); - match EventType::try_from(&*decoded) { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } + try_deserialize!(request, EventType::try_from(&*decoded)) }; let state_key = match path_segments.get(7) { Some(segment) => { - let decoded = match percent_encoding::percent_decode(segment.as_bytes()) - .decode_utf8() - { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; + let decoded = try_deserialize!( + request, + percent_encoding::percent_decode(segment.as_bytes()).decode_utf8() + ); - match String::try_from(&*decoded) { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } + try_deserialize!(request, String::try_from(&*decoded)) } None => "".into(), }; diff --git a/ruma-client-api/src/r0/state/send_state_event.rs b/ruma-client-api/src/r0/state/send_state_event.rs index 292e6fc9..a300a650 100644 --- a/ruma-client-api/src/r0/state/send_state_event.rs +++ b/ruma-client-api/src/r0/state/send_state_event.rs @@ -3,8 +3,8 @@ use std::{borrow::Cow, convert::TryFrom}; use ruma_api::{ - error::{FromHttpRequestError, IntoHttpError, RequestDeserializationError}, - ruma_api, Metadata, + error::{FromHttpRequestError, IntoHttpError}, + ruma_api, try_deserialize, Metadata, }; use ruma_events::{AnyStateEventContent, EventContent as _}; use ruma_identifiers::{EventId, RoomId}; @@ -117,46 +117,33 @@ impl ruma_api::IncomingRequest for IncomingRequest { let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect(); let room_id = { - let decoded = - match percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; + let decoded = try_deserialize!( + request, + percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() + ); - match RoomId::try_from(&*decoded) { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } + try_deserialize!(request, RoomId::try_from(&*decoded)) }; let state_key = match path_segments.get(7) { - Some(segment) => { - match percent_encoding::percent_decode(segment.as_bytes()).decode_utf8() { - Ok(val) => val.into_owned(), - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } - } + Some(segment) => try_deserialize!( + request, + percent_encoding::percent_decode(segment.as_bytes()).decode_utf8() + ) + .into_owned(), None => "".into(), }; let content = { let request_body: Box = - match serde_json::from_slice(request.body().as_slice()) { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - }; + try_deserialize!(request, serde_json::from_slice(request.body().as_slice())); - let event_type = { - match percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() { - Ok(val) => val, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } - }; + let event_type = try_deserialize!( + request, + percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() + ); - match AnyStateEventContent::from_parts(&event_type, request_body) { - Ok(content) => content, - Err(err) => return Err(RequestDeserializationError::new(err, request).into()), - } + try_deserialize!(request, AnyStateEventContent::from_parts(&event_type, request_body)) }; Ok(Self { room_id, state_key, content })