Make Endpoint bound work for ruma_wrapper::Ruma

next
Jonas Platte 2020-03-29 01:29:47 +01:00
parent 73e04e71d7
commit 1183105f15
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 26 additions and 13 deletions

View File

@ -160,7 +160,7 @@ fn create_message_event_route(
_room_id: String, _room_id: String,
_event_type: String, _event_type: String,
_txn_id: String, _txn_id: String,
body: Ruma<create_message_event::IncomingRequest>, body: Ruma<create_message_event::Request>,
) -> MatrixResult<create_message_event::Response> { ) -> MatrixResult<create_message_event::Response> {
dbg!(&body); dbg!(&body);
if let Ok(content) = body.data.clone().into_result() { if let Ok(content) = body.data.clone().into_result() {

View File

@ -5,6 +5,10 @@ use {
rocket::Outcome::*, rocket::Outcome::*,
rocket::Request, rocket::Request,
rocket::State, rocket::State,
ruma_api::{
error::{FromHttpRequestError, FromHttpResponseError},
Endpoint, Outgoing,
},
ruma_client_api::error::Error, ruma_client_api::error::Error,
std::ops::Deref, std::ops::Deref,
std::{ std::{
@ -18,14 +22,20 @@ const MESSAGE_LIMIT: u64 = 65535;
/// This struct converts rocket requests into ruma structs by converting them into http requests /// This struct converts rocket requests into ruma structs by converting them into http requests
/// first. /// first.
pub struct Ruma<T> { pub struct Ruma<T: Outgoing> {
body: T, body: T::Incoming,
headers: http::HeaderMap<http::header::HeaderValue>, headers: http::HeaderMap<http::header::HeaderValue>,
} }
impl<T: TryFrom<http::Request<Vec<u8>>>> FromDataSimple for Ruma<T> impl<T: Endpoint> FromDataSimple for Ruma<T>
where where
T::Error: fmt::Debug, // We need to duplicate Endpoint's where clauses because the compiler is not smart enough yet.
// See https://github.com/rust-lang/rust/issues/54149
<T as Outgoing>::Incoming: TryFrom<http::Request<Vec<u8>>, Error = FromHttpRequestError>,
<T::Response as Outgoing>::Incoming: TryFrom<
http::Response<Vec<u8>>,
Error = FromHttpResponseError<<T as Endpoint>::ResponseError>,
>,
{ {
type Error = (); type Error = ();
@ -45,12 +55,12 @@ where
let headers = http_request.headers().clone(); let headers = http_request.headers().clone();
log::info!("{:?}", http_request); log::info!("{:?}", http_request);
match T::try_from(http_request) { match T::Incoming::try_from(http_request) {
Ok(t) => { Ok(t) => {
//if T::METADATA.requires_authentication { if T::METADATA.requires_authentication {
//let data = request.guard::<State<crate::Data>>(); let data = request.guard::<State<crate::Data>>();
// TODO: auth // TODO: auth
//} }
Success(Ruma { body: t, headers }) Success(Ruma { body: t, headers })
} }
Err(e) => { Err(e) => {
@ -61,15 +71,18 @@ where
} }
} }
impl<T> Deref for Ruma<T> { impl<T: Outgoing> Deref for Ruma<T> {
type Target = T; type Target = T::Incoming;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.body &self.body
} }
} }
impl<T: fmt::Debug> fmt::Debug for Ruma<T> { impl<T: Outgoing> fmt::Debug for Ruma<T>
where
T::Incoming: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Ruma") f.debug_struct("Ruma")
.field("body", &self.body) .field("body", &self.body)