ruma_macros/api/
auth_scheme.rs

1use proc_macro2::TokenStream;
2use quote::ToTokens;
3use syn::parse::{Parse, ParseStream};
4
5mod kw {
6    syn::custom_keyword!(None);
7    syn::custom_keyword!(AccessToken);
8    syn::custom_keyword!(ServerSignatures);
9}
10
11pub enum AuthScheme {
12    None(kw::None),
13    AccessToken(kw::AccessToken),
14    ServerSignatures(kw::ServerSignatures),
15}
16
17impl Parse for AuthScheme {
18    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
19        let lookahead = input.lookahead1();
20
21        if lookahead.peek(kw::None) {
22            input.parse().map(Self::None)
23        } else if lookahead.peek(kw::AccessToken) {
24            input.parse().map(Self::AccessToken)
25        } else if lookahead.peek(kw::ServerSignatures) {
26            input.parse().map(Self::ServerSignatures)
27        } else {
28            Err(lookahead.error())
29        }
30    }
31}
32
33impl ToTokens for AuthScheme {
34    fn to_tokens(&self, tokens: &mut TokenStream) {
35        match self {
36            AuthScheme::None(kw) => kw.to_tokens(tokens),
37            AuthScheme::AccessToken(kw) => kw.to_tokens(tokens),
38            AuthScheme::ServerSignatures(kw) => kw.to_tokens(tokens),
39        }
40    }
41}