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
use crate::ast;
use crate::{OptionSpanned, Parse, ParseError, Parser, Spanned, ToTokens};

/// Visibility level restricted to some path: pub(self) or pub(super) or pub(crate) or pub(in some::module).
#[derive(Debug, Clone, PartialEq, Eq, ToTokens, OptionSpanned)]
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
    }
}

/// Parsing Visibility specifiers
///
/// # Examples
///
/// ```rust
/// use rune::{testing, ast};
///
/// assert!(matches!{
///     testing::roundtrip::<ast::Visibility>("pub"),
///     ast::Visibility::Public(_)
/// });
///
/// assert!(matches!{
///     testing::roundtrip::<ast::Visibility>("pub (in a::b::c)"),
///     ast::Visibility::In(_)
/// });
///
/// assert!(matches!{
///     testing::roundtrip::<ast::Visibility>("pub(in crate::x::y::z)"),
///     ast::Visibility::In(_)
/// });
///
/// assert!(matches!{
///     testing::roundtrip::<ast::Visibility>("pub(super)"),
///     ast::Visibility::Super(_)
/// });
///
/// assert!(matches!{
///     testing::roundtrip::<ast::Visibility>("pub(crate)"),
///     ast::Visibility::Crate(_)
/// });
///
/// assert!(matches!{
///     testing::roundtrip::<ast::Visibility>("pub(self)"),
///     ast::Visibility::SelfValue(_)
/// });
/// ```
impl Parse for Visibility {
    fn parse(parser: &mut Parser<'_>) -> Result<Self, ParseError> {
        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, Clone, PartialEq, Eq, ToTokens, Spanned)]
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, Clone, PartialEq, Eq, ToTokens, Spanned)]
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,
}