From 1183105f15306a2594f09c7280b958119e985693 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 29 Mar 2020 01:29:47 +0100 Subject: [PATCH] Make Endpoint bound work for ruma_wrapper::Ruma --- src/main.rs | 2 +- src/ruma_wrapper.rs | 37 +++++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8b488d5..0097109 100644 --- a/src/main.rs +++ b/src/main.rs @@ -160,7 +160,7 @@ fn create_message_event_route( _room_id: String, _event_type: String, _txn_id: String, - body: Ruma, + body: Ruma, ) -> MatrixResult { dbg!(&body); if let Ok(content) = body.data.clone().into_result() { diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index ceaec3d..5b71925 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -5,6 +5,10 @@ use { rocket::Outcome::*, rocket::Request, rocket::State, + ruma_api::{ + error::{FromHttpRequestError, FromHttpResponseError}, + Endpoint, Outgoing, + }, ruma_client_api::error::Error, std::ops::Deref, std::{ @@ -18,14 +22,20 @@ const MESSAGE_LIMIT: u64 = 65535; /// This struct converts rocket requests into ruma structs by converting them into http requests /// first. -pub struct Ruma { - body: T, +pub struct Ruma { + body: T::Incoming, headers: http::HeaderMap, } -impl>>> FromDataSimple for Ruma +impl FromDataSimple for Ruma 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 + ::Incoming: TryFrom>, Error = FromHttpRequestError>, + ::Incoming: TryFrom< + http::Response>, + Error = FromHttpResponseError<::ResponseError>, + >, { type Error = (); @@ -45,12 +55,12 @@ where let headers = http_request.headers().clone(); log::info!("{:?}", http_request); - match T::try_from(http_request) { + match T::Incoming::try_from(http_request) { Ok(t) => { - //if T::METADATA.requires_authentication { - //let data = request.guard::>(); - // TODO: auth - //} + if T::METADATA.requires_authentication { + let data = request.guard::>(); + // TODO: auth + } Success(Ruma { body: t, headers }) } Err(e) => { @@ -61,15 +71,18 @@ where } } -impl Deref for Ruma { - type Target = T; +impl Deref for Ruma { + type Target = T::Incoming; fn deref(&self) -> &Self::Target { &self.body } } -impl fmt::Debug for Ruma { +impl fmt::Debug for Ruma +where + T::Incoming: fmt::Debug, +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Ruma") .field("body", &self.body)