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:
parent
4d702558bb
commit
2151711f64
@ -90,6 +90,14 @@ impl ToTokens for Api {
|
|||||||
let rate_limited = &self.metadata.rate_limited;
|
let rate_limited = &self.metadata.rate_limited;
|
||||||
let requires_authentication = &self.metadata.requires_authentication;
|
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 request_type = &self.request;
|
||||||
let response_type = &self.response;
|
let response_type = &self.response;
|
||||||
|
|
||||||
@ -507,6 +515,8 @@ impl ToTokens for Api {
|
|||||||
requires_authentication: #requires_authentication,
|
requires_authentication: #requires_authentication,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#non_auth_endpoint_impl
|
||||||
};
|
};
|
||||||
|
|
||||||
api.to_tokens(tokens);
|
api.to_tokens(tokens);
|
||||||
|
@ -236,6 +236,11 @@ pub trait Endpoint:
|
|||||||
const METADATA: Metadata;
|
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.
|
/// Metadata about an API endpoint.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Metadata {
|
pub struct Metadata {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user