From 2151711f64e99a5da370d48fa92795f2d4799866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Dom=C3=ADnguez?= Date: Sat, 23 May 2020 15:27:38 +0200 Subject: [PATCH] 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. --- ruma-api-macros/src/api.rs | 10 ++++++++++ src/lib.rs | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/ruma-api-macros/src/api.rs b/ruma-api-macros/src/api.rs index 876b5ed5..e4a3a381 100644 --- a/ruma-api-macros/src/api.rs +++ b/ruma-api-macros/src/api.rs @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 078f368c..4239732c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 {