use syn::{parse_file, File, visit::Visit, ImplItem, Item, ItemImpl, Type, TypePath};
fn has_impl_base_entity(file: &syn::File) -> bool {
for item in &file.items {
if let Item::Impl(item_impl) = item {
if let Some((_, trait_path, _)) = &item_impl.trait_ {
if let Some(seg) = trait_path.segments.last() {
if seg.ident == "BaseEntity" {
return true;
}
}
}
}
}
false
}
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; }
}
}
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");
println!("has impl BaseEntity: {}", has_impl_base_entity(&ast));
let mut visitor = BaseEntityVisitor { found: false };
visitor.visit_file(&ast);
println!("visitor found: {}", visitor.found);
}