rs-hack 0.5.7

AST-aware Rust refactoring tool for AI agents - transform, rename, inspect & more
Documentation
//! AST visitor that walks syn trees to collect node matches
//! (structs, enums, functions, match expressions).

use syn::spanned::Spanned;
use syn::visit::Visit;
use syn::*;

#[allow(dead_code)]
pub struct NodeFinder {
    pub matches: Vec<NodeMatch>,
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum NodeMatch {
    Struct {
        name: String,
        span: proc_macro2::Span,
    },
    Enum {
        name: String,
        span: proc_macro2::Span,
    },
    Function {
        name: String,
        span: proc_macro2::Span,
    },
    MatchExpr {
        span: proc_macro2::Span,
    },
}

#[allow(dead_code)]
impl Default for NodeFinder {
    fn default() -> Self {
        Self::new()
    }
}

impl NodeFinder {
    pub const fn new() -> Self {
        Self {
            matches: Vec::new(),
        }
    }
}

impl<'ast> Visit<'ast> for NodeFinder {
    fn visit_item_struct(&mut self, node: &'ast ItemStruct) {
        self.matches.push(NodeMatch::Struct {
            name: node.ident.to_string(),
            span: node.span(),
        });
        syn::visit::visit_item_struct(self, node);
    }

    fn visit_item_enum(&mut self, node: &'ast ItemEnum) {
        self.matches.push(NodeMatch::Enum {
            name: node.ident.to_string(),
            span: node.span(),
        });
        syn::visit::visit_item_enum(self, node);
    }

    fn visit_item_fn(&mut self, node: &'ast ItemFn) {
        self.matches.push(NodeMatch::Function {
            name: node.sig.ident.to_string(),
            span: node.span(),
        });
        syn::visit::visit_item_fn(self, node);
    }

    fn visit_expr_match(&mut self, node: &'ast ExprMatch) {
        self.matches
            .push(NodeMatch::MatchExpr { span: node.span() });
        syn::visit::visit_expr_match(self, node);
    }
}