use syn::Item;
use syn::Visibility;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RegistryPolicy {
requires_pub: bool,
allows_extern_alloc: bool,
}
impl RegistryPolicy {
pub const ALLOC: &'static str = "alloc";
pub fn tests() -> Self {
Self {
requires_pub: false,
allows_extern_alloc: false,
}
}
pub fn source() -> Self {
Self {
requires_pub: true,
allows_extern_alloc: true,
}
}
pub fn is_declaration(&self, item: &Item) -> bool {
match item {
Item::Mod(module) => module.content.is_none() && self.visibility_allows(&module.vis),
Item::ExternCrate(external) => {
self.allows_extern_alloc && external.ident == Self::ALLOC
}
_ => false,
}
}
fn visibility_allows(&self, visibility: &Visibility) -> bool {
!self.requires_pub || matches!(visibility, Visibility::Public(_))
}
}