Use ruma_api::try_deserialize in manual endpoint implementations
This commit is contained in:
parent
afec9f0fbf
commit
c523fa0f74
@ -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<Vec<u8>>,
|
||||
) -> Result<Self, FromHttpRequestError> {
|
||||
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))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -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<RawJsonValue> =
|
||||
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 })
|
||||
|
@ -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(),
|
||||
};
|
||||
|
@ -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<RawJsonValue> =
|
||||
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 })
|
||||
|
Loading…
x
Reference in New Issue
Block a user