use super::IndexReader;
use crate::format::{MacroDefEntry, MacroDelim, MacroEdition, MACRO_FLAG_BUILTIN, MACRO_FLAG_EXPORTED, MACRO_FLAG_LOCAL_INNER, MACRO_FLAG_MACRO_RULES};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MacroDefView<'a> {
pub def_index: u32,
pub body: &'a str,
pub flags: u8,
pub delim: MacroDelim,
pub edition: MacroEdition,
}
impl<'a> MacroDefView<'a> {
#[inline]
pub const fn is_macro_rules(&self) -> bool {
(self.flags & MACRO_FLAG_MACRO_RULES) != 0
}
#[inline]
pub const fn is_exported(&self) -> bool {
(self.flags & MACRO_FLAG_EXPORTED) != 0
}
#[inline]
pub const fn has_local_inner_macros(&self) -> bool {
(self.flags & MACRO_FLAG_LOCAL_INNER) != 0
}
#[inline]
pub const fn is_builtin(&self) -> bool {
(self.flags & MACRO_FLAG_BUILTIN) != 0
}
}
impl<B: AsRef<[u8]>> IndexReader<B> {
#[inline]
pub fn macro_defs(&self) -> &[MacroDefEntry] {
self.typed_slice(self.header.macro_defs_offset, self.header.macro_defs_count)
}
#[inline]
fn decode_macro_def(&self, raw: &MacroDefEntry) -> MacroDefView<'_> {
let e = raw.from_le();
MacroDefView { def_index: e.def_index, body: self.get_str(e.body_offset), flags: e.flags, delim: e.delim_kind(), edition: e.edition_kind() }
}
pub fn macro_defs_iter(&self) -> impl ExactSizeIterator<Item = MacroDefView<'_>> + '_ {
self.macro_defs().iter().map(|raw| self.decode_macro_def(raw))
}
pub fn macro_def_for_def(&self, def_index: u32) -> Option<MacroDefView<'_>> {
let entries = self.macro_defs();
let idx = entries.binary_search_by_key(&def_index, |raw| raw.def_index()).ok()?;
Some(self.decode_macro_def(&entries[idx]))
}
}