use crate::utils::get_instruction_attr;
use syn::visit::Visit;

#[derive(Debug, Default)]
pub struct InstructionFinder<'ast> {
    instructions: Vec<&'ast syn::ItemFn>,
}

impl<'ast> Visit<'ast> for InstructionFinder<'ast> {
    fn visit_item_fn(&mut self, func: &'ast syn::ItemFn) {
        if get_instruction_attr(func).is_some() {
            self.instructions.push(func);
        }
        syn::visit::visit_item_fn(self, func);
    }
}

impl<'ast> InstructionFinder<'ast> {
    pub fn run(module: &'ast syn::ItemMod) -> Vec<&'ast syn::ItemFn> {
        let mut finder = InstructionFinder::default();
        finder.visit_item_mod(module);
        finder.instructions
    }
}
