Use ruma_api::try_deserialize in manual endpoint implementations

This commit is contained in:
Jonas Platte 2021-04-05 22:33:05 +02:00
parent afec9f0fbf
commit c523fa0f74
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
4 changed files with 66 additions and 110 deletions

View File

@ -1,14 +1,14 @@
//! PUT /_matrix/client/r0/directory/room/:room_alias //! 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 http::{header::CONTENT_TYPE, method::Method};
use ruma_api::{ use ruma_api::{
error::{ error::{
FromHttpRequestError, FromHttpResponseError, IntoHttpError, RequestDeserializationError, FromHttpRequestError, FromHttpResponseError, IntoHttpError, ResponseDeserializationError,
ResponseDeserializationError, ServerError, Void, ServerError, Void,
}, },
AuthScheme, IncomingRequest, Metadata, OutgoingRequest, try_deserialize, AuthScheme, IncomingRequest, Metadata, OutgoingRequest,
}; };
use ruma_identifiers::{RoomAliasId, RoomId}; use ruma_identifiers::{RoomAliasId, RoomId};
use ruma_serde::Outgoing; use ruma_serde::Outgoing;
@ -71,25 +71,19 @@ impl IncomingRequest for Request {
fn try_from_http_request( fn try_from_http_request(
request: http::Request<Vec<u8>>, request: http::Request<Vec<u8>>,
) -> Result<Self, FromHttpRequestError> { ) -> Result<Self, FromHttpRequestError> {
let request_body: RequestBody = match serde_json::from_slice(request.body().as_slice()) { let request_body: RequestBody =
Ok(body) => body, try_deserialize!(request, serde_json::from_slice(request.body().as_slice()));
Err(err) => {
return Err(RequestDeserializationError::new(err, request).into());
}
};
let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect(); let path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect();
Ok(Request { Ok(Request {
room_id: request_body.room_id, room_id: request_body.room_id,
room_alias: { room_alias: {
let segment = path_segments.get(5).unwrap().as_bytes(); let decoded = try_deserialize!(
let decoded = match percent_encoding::percent_decode(segment).decode_utf8() { request,
Ok(x) => x, percent_encoding::percent_decode(path_segments[5].as_bytes()).decode_utf8(),
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), );
};
match serde_json::from_str(decoded.deref()) { try_deserialize!(request, TryFrom::try_from(&*decoded))
Ok(id) => id,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
}
}, },
}) })
} }

View File

@ -3,8 +3,8 @@
use std::convert::TryFrom; use std::convert::TryFrom;
use ruma_api::{ use ruma_api::{
error::{FromHttpRequestError, IntoHttpError, RequestDeserializationError}, error::{FromHttpRequestError, IntoHttpError},
ruma_api, Metadata, ruma_api, try_deserialize, Metadata,
}; };
use ruma_events::{AnyMessageEventContent, EventContent as _}; use ruma_events::{AnyMessageEventContent, EventContent as _};
use ruma_identifiers::{EventId, RoomId}; 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 path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect();
let room_id = { let room_id = {
let decoded = let decoded = try_deserialize!(
match percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() { request,
Ok(val) => val, percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8(),
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), );
try_deserialize!(request, RoomId::try_from(&*decoded))
}; };
match RoomId::try_from(&*decoded) { let txn_id = try_deserialize!(
Ok(val) => val, request,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), percent_encoding::percent_decode(path_segments[7].as_bytes()).decode_utf8(),
} )
}; .into_owned();
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 content = { let content = {
let request_body: Box<RawJsonValue> = let request_body: Box<RawJsonValue> =
match serde_json::from_slice(request.body().as_slice()) { try_deserialize!(request, serde_json::from_slice(request.body().as_slice()));
Ok(val) => val,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
};
let event_type = { let event_type = try_deserialize!(
match percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() { request,
Ok(val) => val, percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8()
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), );
}
};
match AnyMessageEventContent::from_parts(&event_type, request_body) { try_deserialize!(request, AnyMessageEventContent::from_parts(&event_type, request_body))
Ok(content) => content,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
}
}; };
Ok(Self { room_id, txn_id, content }) Ok(Self { room_id, txn_id, content })

View File

@ -3,8 +3,8 @@
use std::{borrow::Cow, convert::TryFrom}; use std::{borrow::Cow, convert::TryFrom};
use ruma_api::{ use ruma_api::{
error::{FromHttpRequestError, IntoHttpError, RequestDeserializationError}, error::{FromHttpRequestError, IntoHttpError},
ruma_api, Metadata, ruma_api, try_deserialize, Metadata,
}; };
use ruma_events::EventType; use ruma_events::EventType;
use ruma_identifiers::RoomId; 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 path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect();
let room_id = { let room_id = {
let decoded = let decoded = try_deserialize!(
match percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() { request,
Ok(val) => val, percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8()
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), );
};
match RoomId::try_from(&*decoded) { try_deserialize!(request, RoomId::try_from(&*decoded))
Ok(val) => val,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
}
}; };
let event_type = { let event_type = {
let decoded = let decoded = try_deserialize!(
match percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() { request,
Ok(val) => val, percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8()
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), );
};
match EventType::try_from(&*decoded) { try_deserialize!(request, EventType::try_from(&*decoded))
Ok(val) => val,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
}
}; };
let state_key = match path_segments.get(7) { let state_key = match path_segments.get(7) {
Some(segment) => { Some(segment) => {
let decoded = match percent_encoding::percent_decode(segment.as_bytes()) let decoded = try_deserialize!(
.decode_utf8() request,
{ percent_encoding::percent_decode(segment.as_bytes()).decode_utf8()
Ok(val) => val, );
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
};
match String::try_from(&*decoded) { try_deserialize!(request, String::try_from(&*decoded))
Ok(val) => val,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
}
} }
None => "".into(), None => "".into(),
}; };

View File

@ -3,8 +3,8 @@
use std::{borrow::Cow, convert::TryFrom}; use std::{borrow::Cow, convert::TryFrom};
use ruma_api::{ use ruma_api::{
error::{FromHttpRequestError, IntoHttpError, RequestDeserializationError}, error::{FromHttpRequestError, IntoHttpError},
ruma_api, Metadata, ruma_api, try_deserialize, Metadata,
}; };
use ruma_events::{AnyStateEventContent, EventContent as _}; use ruma_events::{AnyStateEventContent, EventContent as _};
use ruma_identifiers::{EventId, RoomId}; 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 path_segments: Vec<&str> = request.uri().path()[1..].split('/').collect();
let room_id = { let room_id = {
let decoded = let decoded = try_deserialize!(
match percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8() { request,
Ok(val) => val, percent_encoding::percent_decode(path_segments[4].as_bytes()).decode_utf8()
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), );
};
match RoomId::try_from(&*decoded) { try_deserialize!(request, RoomId::try_from(&*decoded))
Ok(val) => val,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
}
}; };
let state_key = match path_segments.get(7) { let state_key = match path_segments.get(7) {
Some(segment) => { Some(segment) => try_deserialize!(
match percent_encoding::percent_decode(segment.as_bytes()).decode_utf8() { request,
Ok(val) => val.into_owned(), percent_encoding::percent_decode(segment.as_bytes()).decode_utf8()
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), )
} .into_owned(),
}
None => "".into(), None => "".into(),
}; };
let content = { let content = {
let request_body: Box<RawJsonValue> = let request_body: Box<RawJsonValue> =
match serde_json::from_slice(request.body().as_slice()) { try_deserialize!(request, serde_json::from_slice(request.body().as_slice()));
Ok(val) => val,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
};
let event_type = { let event_type = try_deserialize!(
match percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8() { request,
Ok(val) => val, percent_encoding::percent_decode(path_segments[6].as_bytes()).decode_utf8()
Err(err) => return Err(RequestDeserializationError::new(err, request).into()), );
}
};
match AnyStateEventContent::from_parts(&event_type, request_body) { try_deserialize!(request, AnyStateEventContent::from_parts(&event_type, request_body))
Ok(content) => content,
Err(err) => return Err(RequestDeserializationError::new(err, request).into()),
}
}; };
Ok(Self { room_id, state_key, content }) Ok(Self { room_id, state_key, content })