rune/ast/
vis.rs

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::ast::prelude::*;

#[test]
#[cfg(not(miri))]
fn ast_parse() {
    assert!(matches! {
        rt::<ast::Visibility>("pub"),
        ast::Visibility::Public(_)
    });

    assert!(matches! {
        rt::<ast::Visibility>("pub (in a::b::c)"),
        ast::Visibility::In(_)
    });

    assert!(matches! {
        rt::<ast::Visibility>("pub(in crate::x::y::z)"),
        ast::Visibility::In(_)
    });

    assert!(matches! {
        rt::<ast::Visibility>("pub(super)"),
        ast::Visibility::Super(_)
    });

    assert!(matches! {
        rt::<ast::Visibility>("pub(crate)"),
        ast::Visibility::Crate(_)
    });

    assert!(matches! {
        rt::<ast::Visibility>("pub(self)"),
        ast::Visibility::SelfValue(_)
    });
}

/// Visibility level restricted to some path.
///
/// * `pub(self)`.
/// * `pub(super)`.
/// * `pub(crate)`.
/// * `pub(in some::module)`.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, OptionSpanned)]
#[non_exhaustive]
pub enum Visibility {
    /// An inherited visibility level, this usually means private.
    Inherited,
    /// An unrestricted public visibility level: `pub`.
    Public(T![pub]),
    /// Crate visibility `pub(crate)`.
    Crate(VisibilityRestrict<T![crate]>),
    /// Super visibility `pub(super)`.
    Super(VisibilityRestrict<T![super]>),
    /// Self visibility `pub(self)`.
    SelfValue(VisibilityRestrict<T![self]>),
    /// In visibility `pub(in path)`.
    In(VisibilityRestrict<VisibilityIn>),
}

impl Visibility {
    /// Return `true` if it is the `Inherited` variant
    pub const fn is_inherited(&self) -> bool {
        matches!(self, Visibility::Inherited)
    }

    /// Return `true` if the module is public.
    pub const fn is_public(&self) -> bool {
        matches!(self, Visibility::Public(..))
    }
}

impl Default for Visibility {
    fn default() -> Self {
        Self::Inherited
    }
}

impl Parse for Visibility {
    fn parse(parser: &mut Parser<'_>) -> Result<Self> {
        let pub_token = match parser.parse::<Option<T![pub]>>()? {
            Some(pub_token) => pub_token,
            None => return Ok(Self::Inherited),
        };

        let open = match parser.parse::<Option<ast::OpenParen>>()? {
            Some(open) => open,
            None => return Ok(Self::Public(pub_token)),
        };

        Ok(match parser.nth(0)? {
            K![in] => Self::In(VisibilityRestrict {
                pub_token,
                open,
                restriction: VisibilityIn {
                    in_token: parser.parse()?,
                    path: parser.parse()?,
                },
                close: parser.parse()?,
            }),
            K![super] => Self::Super(VisibilityRestrict {
                pub_token,
                open,
                restriction: parser.parse()?,
                close: parser.parse()?,
            }),
            K![self] => Self::SelfValue(VisibilityRestrict {
                pub_token,
                open,
                restriction: parser.parse()?,
                close: parser.parse()?,
            }),
            _ => Self::Crate(VisibilityRestrict {
                pub_token,
                open,
                restriction: parser.parse()?,
                close: parser.parse()?,
            }),
        })
    }
}

/// A `in path` restriction to visibility.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
pub struct VisibilityIn {
    /// The `in` keyword.
    pub in_token: T![in],
    /// The path the restriction applies to.
    pub path: ast::Path,
}

/// A restriction to visibility.
#[derive(Debug, TryClone, PartialEq, Eq, ToTokens, Spanned)]
#[non_exhaustive]
#[try_clone(bound = {T: TryClone})]
pub struct VisibilityRestrict<T> {
    /// `pub` keyword.
    pub pub_token: ast::generated::Pub,
    /// Opening paren `(`.
    pub open: ast::OpenParen,
    /// The restriction.
    pub restriction: T,
    /// Closing paren `)`.
    pub close: ast::CloseParen,
}