ruwebframe 0.1.0

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

/// 检查文件 AST 中是否有 impl BaseEntity
fn has_impl_base_entity(file: &syn::File) -> bool {
    for item in &file.items {
        if let Item::Impl(item_impl) = item {
            // 检查是否为 trait impl(不是裸 impl SomeStruct {})
            if let Some((_, trait_path, _)) = &item_impl.trait_ {
                // 取路径最后一段,比如 std::foo::BaseEntity -> BaseEntity
                if let Some(seg) = trait_path.segments.last() {
                    if seg.ident == "BaseEntity" {
                        return true;
                    }
                }
            }
        }
    }
    false
}

/// 更灵活:用 syn::visit 遍历(适合嵌套在模块里的 impl)
struct BaseEntityVisitor {
    found: bool,
}

impl<'ast> Visit<'ast> for BaseEntityVisitor {
    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" {
                    self.found = true;
                    return; // 找到即可提前结束
                }
            }
        }
        // 继续遍历嵌套(虽然 impl 里一般不会再有 impl,但保留递归习惯)
        syn::visit::visit_item_impl(self, node);
    }
}

fn main() {
    let code = r#"
        pub trait BaseEntity {
            fn id(&self) -> u64;
        }

        pub struct User {
            id: u64,
        }

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

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

    // 方法1:简单遍历
    println!("has impl BaseEntity: {}", has_impl_base_entity(&ast));

    // 方法2:Visitor 模式(推荐,扩展性好)
    let mut visitor = BaseEntityVisitor { found: false };
    visitor.visit_file(&ast);
    println!("visitor found: {}", visitor.found);
}