diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-08-17 05:49:19 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-17 05:49:19 +0800 |
| commit | 57c53affe3542cb6bd4e79ee4c18f20a1bd76b2d (patch) | |
| tree | 1cd4aef44cb7a45a8cd9d520b598f5f181e24c76 /mingling_pathf | |
| parent | ef23cd944402939605c78a4a853ef6e33af02c21 (diff) | |
refactor!: replace pack! macros with derive-based pipeline types
Remove the `pack!`, `pack_err!`, `pack_structural!`, and
`pack_err_structural!` macros, replacing all pipeline type definitions
with `#[derive(Grouped)]` and `#[derive(Grouped, Wrap)]` attributes.
This changes the generated struct shape from named-field structs with an
`inner` field to tuple structs accessed via `.0`, and removes the
auto-generated `name` and `info` fields from error types.
Diffstat (limited to 'mingling_pathf')
| -rw-r--r-- | mingling_pathf/src/pattern_analyzer.rs | 5 | ||||
| -rw-r--r-- | mingling_pathf/src/patterns.rs | 2 | ||||
| -rw-r--r-- | mingling_pathf/src/patterns/pack.rs | 104 | ||||
| -rw-r--r-- | mingling_pathf/test/src/lib.rs | 27 | ||||
| -rw-r--r-- | mingling_pathf/test/src/test_files/test_pack.rs | 17 |
5 files changed, 2 insertions, 153 deletions
diff --git a/mingling_pathf/src/pattern_analyzer.rs b/mingling_pathf/src/pattern_analyzer.rs index b7f923c..5b8b096 100644 --- a/mingling_pathf/src/pattern_analyzer.rs +++ b/mingling_pathf/src/pattern_analyzer.rs @@ -4,7 +4,7 @@ //! //! It provides a pluggable architecture via the `AnalyzePattern` trait, allowing different //! syntactic patterns to be registered and applied. Built-in patterns cover common structures -//! such as basic structs, packs, groups, derives, chains, renderers, help, completion, and +//! such as basic structs, groups, derives, chains, renderers, help, completion, and //! dispatch patterns (both standard and clap-based). //! //! The entry points are: @@ -18,14 +18,13 @@ use std::path::Path; use crate::error::MinglingPathfinderError; use crate::patterns::{ ChainPattern, CommandPattern, CompletionPattern, DispatcherClapPattern, DispatcherPattern, - GroupPattern, GroupedDerivePattern, HelpPattern, MetadataPattern, PackPattern, RendererPattern, + GroupPattern, GroupedDerivePattern, HelpPattern, MetadataPattern, RendererPattern, }; /// Creates a default `PatternAnalyzer` with all built-in patterns pre-registered. #[must_use] pub fn init() -> PatternAnalyzer { let mut analyzer = PatternAnalyzer::new(); - analyzer.add_pattern(PackPattern); analyzer.add_pattern(GroupPattern); analyzer.add_pattern(GroupedDerivePattern); analyzer.add_pattern(ChainPattern); diff --git a/mingling_pathf/src/patterns.rs b/mingling_pathf/src/patterns.rs index fa0fc8b..43c60ac 100644 --- a/mingling_pathf/src/patterns.rs +++ b/mingling_pathf/src/patterns.rs @@ -10,7 +10,6 @@ pub use group::*; pub use grouped_derive::*; pub use help::*; pub use metadata::*; -pub use pack::*; pub use renderer::*; mod chain; @@ -22,5 +21,4 @@ mod group; mod grouped_derive; mod help; mod metadata; -mod pack; mod renderer; diff --git a/mingling_pathf/src/patterns/pack.rs b/mingling_pathf/src/patterns/pack.rs deleted file mode 100644 index 10327a7..0000000 --- a/mingling_pathf/src/patterns/pack.rs +++ /dev/null @@ -1,104 +0,0 @@ -// Doc Not Optimize -//! The `PackPattern` matches types defined by `pack!`, `pack_err!`, `pack_structural!`, and `pack_err_structural!` macros. -//! It extracts the registered type name (e.g., `TypeName` from `pack!(TypeName = InnerType)`). -//! This is used to track packed type definitions for code generation or analysis. - -use syn::Item; - -use crate::pattern_analyzer::{AnalyzeItem, AnalyzePattern}; - -/// Matches types defined by `pack!`, `pack_err!`, `pack_structural!`, `pack_err_structural!` macros. -/// -/// Covered forms: -/// - `pack!(TypeName = InnerType)` -/// - `pack! { TypeName = InnerType }` -/// - `pack_err!(TypeName)` -/// - `pack_err!(TypeName = InnerType)` -/// - `pack_structural!` series same as above -pub struct PackPattern; - -impl AnalyzePattern for PackPattern { - fn contains(&self, content: &str) -> bool { - content.contains("pack!") - || content.contains("pack_err!") - || content.contains("pack_structural!") - || content.contains("pack_err_structural!") - } - - fn analyze(&self, content: &str) -> Vec<AnalyzeItem> { - let Ok(syntax) = syn::parse_file(content) else { - return Vec::new(); - }; - - let mut items = Vec::new(); - - for item in &syntax.items { - match item { - // Top-level macro calls - Item::Macro(m) => { - if let Some(name) = try_extract_pack_name(m) { - items.push(AnalyzeItem::local(String::new(), name)); - } - } - // Macro calls inside inline modules - Item::Mod(item_mod) => { - if let Some((_, nested)) = &item_mod.content { - for n in nested { - if let Item::Macro(m) = n - && let Some(name) = try_extract_pack_name(m) - { - items.push(AnalyzeItem::local(item_mod.ident.to_string(), name)); - } - } - } - } - _ => {} - } - } - - items - } -} - -/// If the macro call is `pack!` / `pack_err!` / etc., extract the registered type name. -fn try_extract_pack_name(m: &syn::ItemMacro) -> Option<String> { - let macro_name = m.mac.path.segments.last()?.ident.to_string(); - - match macro_name.as_str() { - "pack" | "pack_err" | "pack_structural" | "pack_err_structural" => {} - _ => return None, - } - - let tokens = &m.mac.tokens; - - // `pack!(T)` or `pack!(T = U)` — the first ident is the type name - // Parse simply with syn - if let Ok(ident) = syn::parse2::<syn::Ident>(tokens.clone()) { - // pack!(TypeName) — just a single ident - return Some(ident.to_string()); - } - - // Try to parse `Ident = Type` - // Clone tokens first to avoid partial consumption - let stream = tokens.clone(); - let mut iter = stream.into_iter(); - - // Skip leading attributes/doc comments - loop { - if let proc_macro2::TokenTree::Ident(ident) = iter.next()? { - // Found the first ident, this is the type name - let type_name = ident.to_string(); - - // Check if `=` follows - if let Some(proc_macro2::TokenTree::Punct(p)) = iter.next() - && p.as_char() == '=' - { - // pack!(TypeName = InnerType) - return Some(type_name); - } - - // pack_err!(TypeName) — only a single ident - return Some(type_name); - } - } -} diff --git a/mingling_pathf/test/src/lib.rs b/mingling_pathf/test/src/lib.rs index fff2597..22e1cc2 100644 --- a/mingling_pathf/test/src/lib.rs +++ b/mingling_pathf/test/src/lib.rs @@ -174,33 +174,6 @@ fn test_completion_analyze() { } #[test] -fn test_pack_analyze() { - let analyzer = mingling_pathf::pattern_analyzer::init(); - let file = current_dir().unwrap().join("src/test_files/test_pack.rs"); - - let r = analyzer.analyze_file(file).unwrap(); - let required: Vec<&str> = vec![ - "::ResultPack1", - "::ErrorPack1", - "::ErrorPack2", - "::ResultPack2", - "::ErrorPack3", - "::ErrorPack4", - "::sub::ResultPack1", - "::sub::ErrorPack1", - "::sub::ErrorPack2", - "::sub::ResultPack2", - "::sub::ErrorPack3", - "::sub::ErrorPack4", - ]; - - assert_eq!(r.len(), required.len()); - for entry in &required { - assert!(r.contains(*entry), "Result should contain: {}", entry); - } -} - -#[test] fn test_group_analyze() { let analyzer = mingling_pathf::pattern_analyzer::init(); let file = current_dir().unwrap().join("src/test_files/test_group.rs"); diff --git a/mingling_pathf/test/src/test_files/test_pack.rs b/mingling_pathf/test/src/test_files/test_pack.rs deleted file mode 100644 index 759e35f..0000000 --- a/mingling_pathf/test/src/test_files/test_pack.rs +++ /dev/null @@ -1,17 +0,0 @@ -mingling::macros::pack!(ResultPack1 = String); -mingling::macros::pack_err!(ErrorPack1); -mingling::macros::pack_err!(ErrorPack2 = PathBuf); - -pack!(ResultPack2 = (u8, String)); -pack_err!(ErrorPack3); -pack_err!(ErrorPack4 = PathBuf); - -pub mod sub { - mingling::macros::pack!(ResultPack1 = String); - mingling::macros::pack_err!(ErrorPack1); - mingling::macros::pack_err!(ErrorPack2 = PathBuf); - - pack!(ResultPack2 = (u8, String)); - pack_err!(ErrorPack3); - pack_err!(ErrorPack4 = PathBuf); -} |
