rune/compile/
visibility.rsuse core::fmt;
use core::mem::take;
use crate::Item;
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Visibility {
#[default]
Inherited,
Public,
Crate,
Super,
SelfValue,
}
impl Visibility {
pub(crate) fn take(&mut self) -> Self {
take(self)
}
pub(crate) fn is_public(self) -> bool {
matches!(self, Self::Public)
}
pub(crate) fn is_visible(self, from: &Item, to: &Item) -> bool {
match self {
Visibility::Inherited | Visibility::SelfValue => from.is_super_of(to, 1),
Visibility::Super => from.is_super_of(to, 2),
Visibility::Public => true,
Visibility::Crate => true,
}
}
pub(crate) fn is_visible_inside(self, from: &Item, to: &Item) -> bool {
match self {
Visibility::Inherited | Visibility::SelfValue => from == to,
Visibility::Super => from.is_super_of(to, 1),
Visibility::Public => true,
Visibility::Crate => true,
}
}
}
impl fmt::Display for Visibility {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Visibility::Inherited => write!(f, "private")?,
Visibility::Public => write!(f, "pub")?,
Visibility::Crate => write!(f, "pub(crate)")?,
Visibility::Super => write!(f, "pub(super)")?,
Visibility::SelfValue => write!(f, "pub(self)")?,
}
Ok(())
}
}