aboutsummaryrefslogtreecommitdiff
path: root/mingling_pathf/src/patterns/group.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-21 06:03:24 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-21 06:03:24 +0800
commitd3abdd53e0cc86cb41c0793ce99dbb21b89a4361 (patch)
treee6d6eb1b674185d6a906a7940226a06f3b4cf50a /mingling_pathf/src/patterns/group.rs
parent700c049f64b66f424cda5da3021dfce4462655ca (diff)
feat(pathf): add foreign item support with is_foreign flag
Add `AnalyzeItem::foreign()` and `AnalyzeItem::local()` constructors to distinguish items resolved via `use` imports from local definitions. Update `GroupPattern` to collect use imports at file and module levels, resolving `group!(TypeName)` against them. Migrate all pattern implementations to use the new constructors, and adjust `type_mapping_builder` to skip the file's own module prefix for foreign items.
Diffstat (limited to 'mingling_pathf/src/patterns/group.rs')
-rw-r--r--mingling_pathf/src/patterns/group.rs116
1 files changed, 106 insertions, 10 deletions
diff --git a/mingling_pathf/src/patterns/group.rs b/mingling_pathf/src/patterns/group.rs
index 0e4b50d..0f9ecff 100644
--- a/mingling_pathf/src/patterns/group.rs
+++ b/mingling_pathf/src/patterns/group.rs
@@ -2,7 +2,10 @@
//! extracts the type name or alias defined within them.
//! This is used to track type groups for code generation or analysis.
+use std::collections::HashMap;
+
use syn::Item;
+use syn::UseTree;
use crate::pattern_analyzer::{AnalyzeItem, AnalyzePattern};
@@ -25,6 +28,9 @@ impl AnalyzePattern for GroupPattern {
return Vec::new();
};
+ // Collect `use` imports at the file level
+ let imports = collect_use_imports(&syntax.items);
+
let mut items = Vec::new();
for item in &syntax.items {
@@ -37,15 +43,15 @@ impl AnalyzePattern for GroupPattern {
if macro_name != "group" && macro_name != "group_structural" {
continue;
}
- if let Some(name) = extract_group_name(&m.mac.tokens) {
- items.push(AnalyzeItem {
- module: String::new(),
- item_name: name,
- });
+ if let Some(analyze_item) = extract_group_item(&m.mac.tokens, &imports, "") {
+ items.push(analyze_item);
}
}
Item::Mod(item_mod) => {
if let Some((_, nested)) = &item_mod.content {
+ // Collect `use` imports inside this module
+ let inner_imports = collect_use_imports(nested);
+
for n in nested {
if let Item::Macro(m) = n {
let Some(last) = m.mac.path.segments.last() else {
@@ -55,11 +61,12 @@ impl AnalyzePattern for GroupPattern {
if macro_name != "group" && macro_name != "group_structural" {
continue;
}
- if let Some(name) = extract_group_name(&m.mac.tokens) {
- items.push(AnalyzeItem {
- module: item_mod.ident.to_string(),
- item_name: name,
- });
+ if let Some(analyze_item) = extract_group_item(
+ &m.mac.tokens,
+ &inner_imports,
+ &item_mod.ident.to_string(),
+ ) {
+ items.push(analyze_item);
}
}
}
@@ -73,6 +80,95 @@ impl AnalyzePattern for GroupPattern {
}
}
+/// Extract an `AnalyzeItem` from the macro invocation tokens.
+///
+/// If the name matches a `use` import, resolves to the full foreign path.
+/// For the `Alias = path::Type` form, returns a local item (the alias exists in-crate).
+fn extract_group_item(
+ tokens: &proc_macro2::TokenStream,
+ imports: &HashMap<String, (String, String)>,
+ current_mod: &str,
+) -> Option<AnalyzeItem> {
+ let name = extract_group_name(tokens)?;
+ let is_aliased = has_equals_sign(tokens);
+
+ if is_aliased {
+ // `group!(Alias = path::Type)` — alias lives in-crate
+ Some(AnalyzeItem::local(current_mod.to_string(), name))
+ } else if let Some((module, _)) = imports.get(&name) {
+ // `group!(TypeName)` where TypeName is imported via `use` — foreign
+ Some(AnalyzeItem::foreign(module.clone(), name))
+ } else {
+ // `group!(LocalType)` — local type
+ Some(AnalyzeItem::local(current_mod.to_string(), name))
+ }
+}
+
+/// Collect `use` imports from a list of top-level items.
+///
+/// Returns a map of `short_name → (module_path, short_name)`.
+/// e.g. `use cargo_metadata::CompilerMessage;` → `"CompilerMessage" → ("cargo_metadata", "CompilerMessage")`
+fn collect_use_imports(items: &[syn::Item]) -> HashMap<String, (String, String)> {
+ let mut map = HashMap::new();
+ for item in items {
+ if let Item::Use(use_item) = item {
+ // Only handle non-`pub use` (regular imports)
+ collect_from_use_tree(&use_item.tree, "", &mut map);
+ }
+ }
+ map
+}
+
+/// Recursively traverse a `UseTree` and collect named imports.
+fn collect_from_use_tree(
+ tree: &UseTree,
+ prefix: &str,
+ map: &mut HashMap<String, (String, String)>,
+) {
+ match tree {
+ UseTree::Name(name) => {
+ let module = prefix.to_string();
+ let alias = name.ident.to_string();
+ map.entry(alias).or_insert((module, name.ident.to_string()));
+ }
+ UseTree::Path(use_path) => {
+ let new_prefix = if prefix.is_empty() {
+ use_path.ident.to_string()
+ } else {
+ format!("{}::{}", prefix, use_path.ident)
+ };
+ collect_from_use_tree(&use_path.tree, &new_prefix, map);
+ }
+ UseTree::Rename(rename) => {
+ let module = prefix.to_string();
+ let alias = rename.ident.to_string();
+ map.entry(alias)
+ .or_insert((module, rename.ident.to_string()));
+ }
+ UseTree::Glob(_) => {
+ // `use path::*;` — skip glob imports
+ }
+ UseTree::Group(group) => {
+ for item in &group.items {
+ collect_from_use_tree(item, prefix, map);
+ }
+ }
+ }
+}
+
+/// Check whether the macro tokens contain `=`, indicating aliased form.
+fn has_equals_sign(tokens: &proc_macro2::TokenStream) -> bool {
+ let stream = tokens.clone();
+ for token in stream {
+ if let proc_macro2::TokenTree::Punct(p) = token
+ && p.as_char() == '='
+ {
+ return true;
+ }
+ }
+ false
+}
+
/// Extract the alias / type name from the arguments of `group!`.
///
/// - `group!(ParseIntError)` → `ParseIntError`