-
Notifications
You must be signed in to change notification settings - Fork 179
Derive macro for StructuredParameters #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BCSol
wants to merge
27
commits into
ros2-rust:main
Choose a base branch
from
BCSol:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
3094ff8
Structured parameters initial draft
BCSol c76651c
Default values for parameters
BCSol cba5837
Make declare_parameters public
BCSol 80ed497
Move NodeState from parameter options to enabled default derivation
BCSol 2ba7748
Remove parameter struct
BCSol 2561549
Add support for description
BCSol d9bcf97
Add some documentation
BCSol 0b7ac44
Add module doc with example
BCSol 2d0be3d
Fix format
BCSol 073322c
Revert changes to build
BCSol c686ba5
Merge remote-tracking branch 'forked/main'
BCSol 4a07678
Fix error message punctuation
BCSol 620d09d
cargo fmt nightly
BCSol ba3d941
Remove docstring test
balthasarschuess 6729752
Only require macros at test time
balthasarschuess b07fd43
Add setup to bashrc
balthasarschuess cd3a8ea
Merge branch 'ros2-rust:main' into main
balthasarschuess e760adf
Fix bug in Dockerfile
balthasarschuess 36de62a
Fix cargo fmt
balthasarschuess 4665263
Follow standard macro package naming convention
balthasarschuess 5f93232
Prepare macro to implement enum derive
balthasarschuess 0879517
Reduce visibility of struct macro implementation
balthasarschuess 74cf511
Extend macro with more builder options
balthasarschuess 6616988
support discimator function
balthasarschuess e688869
Fix bug
balthasarschuess 22a022a
Add support for ranges
balthasarschuess e13ed4b
Add tests for all macro options
balthasarschuess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| [workspace] | ||
| members = [ | ||
| "rclrs", | ||
| "rclrs_proc_macros", | ||
| ] | ||
| resolver = "2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [package] | ||
| name= "rclrs_proc_macros" | ||
| version = "0.0.1" | ||
| authors = ["Balthasar Schüss <[email protected]>"] | ||
| edition = "2021" | ||
| license = "Apache-2.0" | ||
| description = "A rust library providing proc macros for rclrs" | ||
| rust-version = "1.75" | ||
|
|
||
|
|
||
| [lib] | ||
| path = "src/lib.rs" | ||
| proc-macro = true | ||
|
|
||
| [dependencies] | ||
| proc-macro2 = "1.0" | ||
| quote = "1.0" | ||
| syn = "2.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| use proc_macro2::TokenStream; | ||
| use quote::quote; | ||
| use syn::DeriveInput; | ||
|
|
||
| pub(crate) fn derive_struct_parameters(input: DeriveInput) -> syn::Result<TokenStream> { | ||
| let ident = input.ident; | ||
|
|
||
| let fields = match input.data { | ||
| syn::Data::Struct(ref s) => &s.fields, | ||
| _ => { | ||
| return syn::Result::Err(syn::Error::new_spanned( | ||
| ident, | ||
| "StrucutredParameter trait can only be derived for structs", | ||
| )); | ||
| } | ||
| }; | ||
|
|
||
| let field_types: Vec<_> = fields.iter().map(|f| &f.ty).collect(); | ||
|
|
||
| let mut args = Vec::new(); | ||
| for f in fields { | ||
| let ident = f.ident.as_ref().unwrap(); | ||
| let ident_str = syn::LitStr::new(&f.ident.as_ref().unwrap().to_string(), ident.span()); | ||
| let field_type = match &f.ty { | ||
| syn::Type::Path(p) => { | ||
| let mut p = p.path.clone(); | ||
| for segment in &mut p.segments { | ||
| segment.arguments = syn::PathArguments::None; | ||
| } | ||
| p | ||
| } | ||
| e => { | ||
| return syn::Result::Err(syn::Error::new_spanned( | ||
| e, | ||
| "attribute can only be path type", | ||
| )); | ||
| } | ||
| }; | ||
| let r = quote! { | ||
| #ident : #field_type::declare_structured( | ||
| node, &{match name { | ||
| "" => #ident_str.to_string(), | ||
| prefix => [prefix, ".", #ident_str].concat(), | ||
| } | ||
| })?, | ||
| }; | ||
| args.push(r); | ||
| } | ||
|
|
||
| let result = quote!( | ||
| impl #ident { | ||
| const _ASSERT_PARAMETER: fn() = || { | ||
| fn assert_parameter<T: rclrs::StructuredParameters>() {} | ||
| #( | ||
| assert_parameter::<#field_types>(); | ||
| )* | ||
| }; | ||
| } | ||
|
|
||
| impl rclrs::StructuredParameters for #ident { | ||
| fn declare_structured(node: &rclrs::NodeState, name: &str) -> core::result::Result<Self, rclrs::DeclarationError> { | ||
| core::result::Result::Ok(Self{ #(#args)*}) | ||
| } | ||
| } | ||
| ); | ||
| syn::Result::Ok(result) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| mod r#impl; | ||
| use proc_macro::TokenStream; | ||
| use syn::{parse_macro_input, DeriveInput}; | ||
|
|
||
| #[proc_macro_derive(StructuredParameters)] | ||
| pub fn derive_struct_parameters(input: TokenStream) -> TokenStream { | ||
| let input = parse_macro_input!(input as DeriveInput); | ||
| r#impl::derive_struct_parameters(input) | ||
| .unwrap_or_else(|e| e.to_compile_error()) | ||
| .into() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.