Add NonAuthEndpoint trait

This is a way to mark any type implementing `Endpoint` as not
requiring authentication information through the type system.
This is useful for clients that build on top of the ruma crates
to check this condition at compile time, avoiding a runtime error.
This commit is contained in:
Alejandro Domínguez 2020-05-23 15:27:38 +02:00 committed by Jonas Platte
parent 4d702558bb
commit 2151711f64
2 changed files with 15 additions and 0 deletions

View File

@ -90,6 +90,14 @@ impl ToTokens for Api {
let rate_limited = &self.metadata.rate_limited;
let requires_authentication = &self.metadata.requires_authentication;
let non_auth_endpoint_impl = if requires_authentication.value {
quote! {
impl ruma_api::NonAuthEndpoint for Request {}
}
} else {
TokenStream::new()
};
let request_type = &self.request;
let response_type = &self.response;
@ -507,6 +515,8 @@ impl ToTokens for Api {
requires_authentication: #requires_authentication,
};
}
#non_auth_endpoint_impl
};
api.to_tokens(tokens);

View File

@ -236,6 +236,11 @@ pub trait Endpoint:
const METADATA: Metadata;
}
/// A Matrix API endpoint that doesn't require authentication.
///
/// This marker trait is to indicate that a type implementing `Endpoint` doesn't require any authentication.
pub trait NonAuthEndpoint: Endpoint {}
/// Metadata about an API endpoint.
#[derive(Clone, Debug)]
pub struct Metadata {