diff options
| author | JSDurand <mmemmew@gmail.com> | 2026-10-16 14:01:01 +0800 |
|---|---|---|
| committer | JSDurand <mmemmew@gmail.com> | 2026-10-16 14:02:10 +0800 |
| commit | fba9a90cfc6aca2a26cce72f55fb261789139781 (patch) | |
| tree | b4b121791ad639e745df689c0d5a8ed7a9e5dcc4 /graph_macro/src | |
| parent | 3ae0cf9898485a2ec35df7d23cd274e7dea5796d (diff) | |
Now I tested and discussed with claude, and had a working version of
this project. The rest is to port the claude version to my own
writings. So this serves as the starting point before that attempt.
In the new attempt the design is better, and a number of bugs were
found and supposedly fixed, so let us see how this goes at the end.
Diffstat (limited to 'graph_macro/src')
| -rw-r--r-- | graph_macro/src/lib.rs | 51 |
1 files changed, 38 insertions, 13 deletions
diff --git a/graph_macro/src/lib.rs b/graph_macro/src/lib.rs index 409e6db..5f82658 100644 --- a/graph_macro/src/lib.rs +++ b/graph_macro/src/lib.rs @@ -19,6 +19,7 @@ enum VisibilityMod { /// Since the restrictions can be given in a variety of ways, and /// since it does not matter to us, we just keep them as a /// tokentree. + #[allow(dead_code)] PubRes(TokenTree), #[default] Private, @@ -206,7 +207,7 @@ pub fn graph_derive(input: TokenStream) -> TokenStream { result }; - let result = format!( + let result = format!( " #[automatically_derived] impl{generics_impl_string} graph::Graph for {name}{generic_struct_string} {{ @@ -405,12 +406,7 @@ fn get_graph_field_name_type( return end_of_input; } - let mut result_type = get_until( - &mut cloned_body, - |tree| matches!(tree, TokenTree::Punct(p) if p.as_char() == ','), - true, - true, - )?; + let mut result_type = get_field_type(&mut cloned_body)?; let mut result = (result_name, result_type); @@ -448,12 +444,7 @@ fn get_graph_field_name_type( move_punct(&mut body, ':')?; - result_type = get_until( - &mut body, - |tree| matches!(tree, TokenTree::Punct(p) if p.as_char() == ','), - true, - true, - )?; + result_type = get_field_type(&mut body)?; result = (result_name, result_type); @@ -479,6 +470,40 @@ fn get_graph_field_name_type( } } +fn get_field_type( + input: &mut Peekable<impl Iterator<Item = TokenTree>>, +) -> Result<TokenStream, CompileError> { + let mut trees: Vec<TokenTree> = Vec::new(); + + let mut found_angle_bracket = 0usize; + + for tree in input.by_ref() { + match &tree { + TokenTree::Punct(p) => { + match p.as_char() { + '<' => { + found_angle_bracket += 1; + } + '>' => { + found_angle_bracket -= 1; + } + ',' if found_angle_bracket == 0 => { + break; + } + _ => {} + } + + trees.push(tree); + } + _ => { + trees.push(tree); + } + } + } + + Ok(trees.into_iter().collect()) +} + fn get_generics( input: &mut Peekable<impl Iterator<Item = TokenTree>>, ) -> Result<Vec<Generic>, CompileError> { |
