Simplify the Endpoint trait by using the Request type as Self

This commit is contained in:
Jonas Platte 2019-07-21 01:20:49 +02:00
parent 5d0b4dd833
commit 23a946c8df

View File

@ -41,22 +41,20 @@ use std::{
io, io,
}; };
use http::{self, Method, Request, Response, StatusCode}; use http::{self, Method, Request as HttpRequest, Response as HttpResponse, StatusCode};
use ruma_identifiers; use ruma_identifiers;
use serde_json; use serde_json;
use serde_urlencoded; use serde_urlencoded;
/// A Matrix API endpoint. /// A Matrix API endpoint's Request type
pub trait Endpoint { pub trait Endpoint:
/// Data needed to make a request to the endpoint. TryFrom<HttpRequest<Vec<u8>>, Error = Error> + TryInto<HttpRequest<Vec<u8>>, Error = Error>
type Request: TryFrom<Request<Vec<u8>>, Error = Error> {
+ TryInto<Request<Vec<u8>>, Error = Error>; /// The corresponding Response type
type Response: TryFrom<HttpResponse<Vec<u8>>, Error = Error>
+ TryInto<HttpResponse<Vec<u8>>, Error = Error>;
/// Data returned from the endpoint. /// Metadata about the endpoint
type Response: TryFrom<Response<Vec<u8>>, Error = Error>
+ TryInto<Response<Vec<u8>>, Error = Error>;
/// Metadata about the endpoint.
const METADATA: Metadata; const METADATA: Metadata;
} }
@ -180,13 +178,9 @@ mod tests {
use serde_json; use serde_json;
use url::percent_encoding; use url::percent_encoding;
use crate::{Endpoint as ApiEndpoint, Error, Metadata}; use crate::{Endpoint, Error, Metadata};
#[derive(Debug)] impl Endpoint for Request {
pub struct Endpoint;
impl ApiEndpoint for Endpoint {
type Request = Request;
type Response = Response; type Response = Response;
const METADATA: Metadata = Metadata { const METADATA: Metadata = Metadata {
@ -215,7 +209,7 @@ mod tests {
type Error = Error; type Error = Error;
fn try_from(request: Request) -> Result<HttpRequest<Vec<u8>>, Self::Error> { fn try_from(request: Request) -> Result<HttpRequest<Vec<u8>>, Self::Error> {
let metadata = Endpoint::METADATA; let metadata = Request::METADATA;
let path = metadata let path = metadata
.path .path