ruwebframe 0.1.0

A brief description of your crate.
Documentation
use syn::{parse_file, visit::Visit, File, Item, ItemImpl, Type};

/// 查找所有 `impl BaseEntity for Xxx`,返回 Xxx 的名称列表
fn find_base_entity_impls(file: &File) -> Vec<String> {
    let mut results = Vec::new();

    for item in &file.items {
        if let Item::Impl(item_impl) = item {
            // 1. 确认是 trait impl(不是 impl MyStruct {} 这种)
            if let Some((_, trait_path, _)) = &item_impl.trait_ {
                // 2. 确认 trait 名是 BaseEntity
                if let Some(seg) = trait_path.segments.last() {
                    if seg.ident == "BaseEntity" {
                        // 3. 提取 self_ty 的名称(即 impl BaseEntity for <这里>)
                        if let Some(name) = type_to_string(&item_impl.self_ty) {
                            results.push(name);
                        }
                    }
                }
            }
        }
    }

    results
}
fn find_base_entity_impls_single(file: &File) -> Vec<String> {
    let mut results = Vec::new();

    for item in &file.items {
        if let Item::Impl(item_impl) = item {
            // 1. 确认是 trait impl(不是 impl MyStruct {} 这种)
            if let Some((_, trait_path, _)) = &item_impl.trait_ {
                // 2. 确认 trait 名是 BaseEntity
                if let Some(seg) = trait_path.segments.last() {
                    if seg.ident == "BaseEntitySingle" {
                        // 3. 提取 self_ty 的名称(即 impl BaseEntity for <这里>)
                        if let Some(name) = type_to_string(&item_impl.self_ty) {
                            results.push(name);
                        }
                    }
                }
            }
        }
    }

    results
}

/// 把 Type 转成可读的字符串(处理 Path、Reference 等常见情况)
fn type_to_string(ty: &Type) -> Option<String> {
    match ty {
        Type::Path(type_path) => {
            // 取路径最后一段,例如 std::foo::User -> "User"
            type_path.path.segments.last().map(|s| s.ident.to_string())
        }
        Type::Reference(r) => type_to_string(&r.elem), // &User -> User
        Type::Paren(p) => type_to_string(&p.elem),     // (User) -> User
        _ => None, // 遇到复杂类型(如 impl Trait、匿名结构体等)可扩展
    }
}

// ========== Visitor 版本(适合嵌套在 mod 里的 impl) ==========
struct BaseEntityImplVisitor {
    results: Vec<String>,
}

impl<'ast> Visit<'ast> for BaseEntityImplVisitor {
    fn visit_item_impl(&mut self, node: &'ast ItemImpl) {
        if let Some((_, trait_path, _)) = &node.trait_ {
            if let Some(seg) = trait_path.segments.last() {
                if seg.ident == "BaseEntity" {
                    if let Some(name) = type_to_string(&node.self_ty) {
                        self.results.push(name);
                    }
                }
            }
        }
        // 继续递归遍历(比如 mod 嵌套)
        syn::visit::visit_item_impl(self, node);
    }
}
#[test]
fn main() {
    let code = r#"
        trait BaseEntity { fn id(&self) -> u64; }
   trait BaseEntitySingle { fn id(&self) -> u64; }

        struct User { id: u64 }
        struct Order { id: u64 }

        impl BaseEntity for User {
            fn id(&self) -> u64 { self.id }
        }
 impl BaseEntitySingle for User {
            fn id(&self) -> u64 { self.id }
        }

        // 这个不是 BaseEntity
        impl Clone for Order {
            fn clone(&self) -> Self { todo!() }
        }

        mod inner {
            use super::BaseEntity;
            struct Admin;
            impl BaseEntity for Admin {
                fn id(&self) -> u64 { 0 }
            }
        }
    "#;

    let ast = parse_file(code).expect("parse failed");

    // 方法1:简单遍历(只查顶层 items)
    let found = find_base_entity_impls(&ast);
    println!("简单遍历找到: {:?}", found); // ["User"]

    // 方法2:Visitor(递归查所有层级)
    let mut visitor = BaseEntityImplVisitor { results: vec![] };
    visitor.visit_file(&ast);
    println!("Visitor 找到: {:?}", visitor.results); // ["User", "Admin"]
    let found = find_base_entity_impls(&ast);
    println!("简单遍历找到:baseentity {:?}", found); // ["User"]

    let found = find_base_entity_impls_single(&ast);
    println!("简单遍历找到: baseentitysngle{:?}", found); // ["User"]

}