Merge pull request #21 from ruma/endpoint_is_request

Simplify the Endpoint trait by using the Request type as Self
This commit is contained in:
Jimmy Cuadra 2019-07-25 09:37:16 -07:00 committed by GitHub
commit 4324ab29cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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