Use FutureFrom instead of TryFrom for responses.
This commit is contained in:
parent
517235b3e9
commit
3635fe51ac
@ -11,6 +11,7 @@ repository = "https://github.com/ruma/ruma-api"
|
|||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
futures = "0.1.13"
|
||||||
serde_json = "1.0.2"
|
serde_json = "1.0.2"
|
||||||
|
|
||||||
[dependencies.hyper]
|
[dependencies.hyper]
|
||||||
|
16
src/lib.rs
16
src/lib.rs
@ -13,15 +13,17 @@
|
|||||||
#![deny(missing_docs)]
|
#![deny(missing_docs)]
|
||||||
#![feature(associated_consts, try_from)]
|
#![feature(associated_consts, try_from)]
|
||||||
|
|
||||||
|
extern crate futures;
|
||||||
extern crate hyper;
|
extern crate hyper;
|
||||||
#[cfg(test)] extern crate ruma_identifiers;
|
#[cfg(test)] extern crate ruma_identifiers;
|
||||||
#[cfg(test)] extern crate serde;
|
#[cfg(test)] extern crate serde;
|
||||||
#[cfg(test)] #[macro_use] extern crate serde_derive;
|
#[cfg(test)] #[macro_use] extern crate serde_derive;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
|
|
||||||
use std::convert::{TryFrom, TryInto};
|
use std::convert::TryInto;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
use futures::future::FutureFrom;
|
||||||
use hyper::{Method, Request, Response, StatusCode};
|
use hyper::{Method, Request, Response, StatusCode};
|
||||||
use hyper::error::UriError;
|
use hyper::error::UriError;
|
||||||
|
|
||||||
@ -30,7 +32,7 @@ pub trait Endpoint {
|
|||||||
/// Data needed to make a request to the endpoint.
|
/// Data needed to make a request to the endpoint.
|
||||||
type Request: TryInto<Request, Error = Error>;
|
type Request: TryInto<Request, Error = Error>;
|
||||||
/// Data returned from the endpoint.
|
/// Data returned from the endpoint.
|
||||||
type Response: TryFrom<Response, Error = Error>;
|
type Response: FutureFrom<Response, Error = Error>;
|
||||||
|
|
||||||
/// Metadata about the endpoint.
|
/// Metadata about the endpoint.
|
||||||
const METADATA: Metadata;
|
const METADATA: Metadata;
|
||||||
@ -100,6 +102,7 @@ mod tests {
|
|||||||
pub mod create {
|
pub mod create {
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
use futures::future::{FutureFrom, FutureResult, err, ok};
|
||||||
use hyper::{Method, Request as HyperRequest, Response as HyperResponse, StatusCode};
|
use hyper::{Method, Request as HyperRequest, Response as HyperResponse, StatusCode};
|
||||||
use ruma_identifiers::{RoomAliasId, RoomId};
|
use ruma_identifiers::{RoomAliasId, RoomId};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
@ -164,14 +167,15 @@ mod tests {
|
|||||||
/// The response to a request to create a new room alias.
|
/// The response to a request to create a new room alias.
|
||||||
pub struct Response;
|
pub struct Response;
|
||||||
|
|
||||||
impl TryFrom<HyperResponse> for Response {
|
impl FutureFrom<HyperResponse> for Response {
|
||||||
|
type Future = FutureResult<Self, Self::Error>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn try_from(hyper_response: HyperResponse) -> Result<Response, Self::Error> {
|
fn future_from(hyper_response: HyperResponse) -> FutureResult<Self, Self::Error> {
|
||||||
if hyper_response.status() == StatusCode::Ok {
|
if hyper_response.status() == StatusCode::Ok {
|
||||||
Ok(Response)
|
ok(Response)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::StatusCode(hyper_response.status().clone()))
|
err(Error::StatusCode(hyper_response.status().clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user