summaryrefslogtreecommitdiffstats
path: root/third_party/rust/inherent/src/visibility.rs
blob: 72213fda622d1f39f125128277d7200a84136789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::ext::IdentExt;
use syn::parse::{Parse, ParseStream, Result};
use syn::{Ident, Path, Token, VisPublic, VisRestricted};

pub struct Visibility {
    variant: syn::Visibility,
}

impl Parse for Visibility {
    fn parse(input: ParseStream) -> Result<Self> {
        let lookahead = input.lookahead1();

        let variant = if input.is_empty() {
            syn::Visibility::Inherited
        } else if lookahead.peek(Token![pub]) {
            syn::Visibility::Public(VisPublic {
                pub_token: input.parse()?,
            })
        } else if lookahead.peek(Token![crate])
            || lookahead.peek(Token![self])
            || lookahead.peek(Token![super])
        {
            syn::Visibility::Restricted(VisRestricted {
                pub_token: Default::default(),
                paren_token: Default::default(),
                in_token: None,
                path: Box::new(Path::from(input.call(Ident::parse_any)?)),
            })
        } else if lookahead.peek(Token![in]) {
            syn::Visibility::Restricted(VisRestricted {
                pub_token: Default::default(),
                paren_token: Default::default(),
                in_token: Some(input.parse()?),
                path: Box::new(input.call(Path::parse_mod_style)?),
            })
        } else {
            return Err(lookahead.error());
        };

        Ok(Visibility { variant })
    }
}

impl ToTokens for Visibility {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        self.variant.to_tokens(tokens);
    }
}