From 23150124485eae9c2d7a1bf5096e9f613eb40969 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 27 Nov 2022 18:47:02 +0100 Subject: [PATCH] first version works --- satrs-macros/src/lib.rs | 22 ++++++++-------------- satrs-macros/src/main.rs | 2 +- satrs-macros/tests/basic.rs | 3 ++- satrs-macros/tests/group_in_enum.rs | 12 ------------ satrs-macros/tests/tests.rs | 2 +- satrs-macros/tests/verify_gen_struct.rs | 16 +++++++++++++--- 6 files changed, 25 insertions(+), 32 deletions(-) delete mode 100644 satrs-macros/tests/group_in_enum.rs diff --git a/satrs-macros/src/lib.rs b/satrs-macros/src/lib.rs index 83e3e00..98a6429 100644 --- a/satrs-macros/src/lib.rs +++ b/satrs-macros/src/lib.rs @@ -10,15 +10,12 @@ struct ResultExtGenerator { } #[proc_macro_attribute] -pub fn result( +pub fn resultcode( args: proc_macro::TokenStream, item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { let args = parse_macro_input!(args as AttributeArgs); let input = parse_macro_input!(item as Item); - - dbg!("Arguments: {}", &args); - dbg!("Input: {}", &input); let mut result_ext_generator = ResultExtGenerator::default(); result_ext_generator.parse(args, input).into() } @@ -53,10 +50,7 @@ impl ResultExtGenerator { } else { return Err(syn::Error::new( path.span(), - format!( - "Unknown attribute argument name {}", - path.ident - ), + format!("Unknown attribute argument name {}", path.ident), )); } } @@ -93,13 +87,13 @@ impl ResultExtGenerator { let gen_struct_name = format_ident!("{}_EXT", result_code_name); let info_str = self.info_str.to_owned().unwrap(); let gen_struct = quote! { - const #gen_struct_name: satrs_core::resultcode::ResultU16Ext = satrs_core::resultcode::ResultU16Ext { - name: #name_as_str, - result: &#result_code_name, - info: #info_str - }; + const #gen_struct_name: satrs_core::resultcode::ResultU16Ext = + satrs_core::resultcode::ResultU16Ext::const_new( + #name_as_str, + &#result_code_name, + #info_str + ); }; Ok(gen_struct) - //Ok(TokenStream::new()) } } diff --git a/satrs-macros/src/main.rs b/satrs-macros/src/main.rs index 7eb903a..ed36991 100644 --- a/satrs-macros/src/main.rs +++ b/satrs-macros/src/main.rs @@ -1,7 +1,7 @@ use satrs_core::resultcode::ResultU16; use satrs_macros::*; -#[result(info = "This is a test result where the first parameter is foo")] +#[resultcode(info = "This is a test result where the first parameter is foo")] const _TEST_RESULT: ResultU16 = ResultU16::const_new(0, 1); fn main() {} diff --git a/satrs-macros/tests/basic.rs b/satrs-macros/tests/basic.rs index e224e4a..bcb16b5 100644 --- a/satrs-macros/tests/basic.rs +++ b/satrs-macros/tests/basic.rs @@ -1,7 +1,8 @@ +//! Basic check which just verifies that everything compiles use satrs_core::resultcode::ResultU16; use satrs_macros::*; -#[result(info = "This is a test result where the first parameter is foo")] +#[resultcode(info = "This is a test result where the first parameter is foo")] const TEST_RESULT: ResultU16 = ResultU16::const_new(0, 1); fn main() {} diff --git a/satrs-macros/tests/group_in_enum.rs b/satrs-macros/tests/group_in_enum.rs deleted file mode 100644 index ecc71dc..0000000 --- a/satrs-macros/tests/group_in_enum.rs +++ /dev/null @@ -1,12 +0,0 @@ -use satrs_core::resultcode::ResultU16; -use satrs_macros::*; - -pub enum GroupIds { - Group0 = 0, - Group1 = 1, -} - -#[result(info = "This is a test result where the first parameter is foo")] -const TEST_RESULT: ResultU16 = ResultU16::const_new(GroupIds::Group0 as u8, 1); - -fn main() {} diff --git a/satrs-macros/tests/tests.rs b/satrs-macros/tests/tests.rs index d1b72fe..daf9fc7 100644 --- a/satrs-macros/tests/tests.rs +++ b/satrs-macros/tests/tests.rs @@ -2,6 +2,6 @@ fn tests() { let t = trybuild::TestCases::new(); t.pass("tests/basic.rs"); - //t.pass("tests/verify_gen_struct.rs"); + t.pass("tests/verify_gen_struct.rs"); //t.pass("tests/group_in_enum.rs"); } diff --git a/satrs-macros/tests/verify_gen_struct.rs b/satrs-macros/tests/verify_gen_struct.rs index e224e4a..3d308a2 100644 --- a/satrs-macros/tests/verify_gen_struct.rs +++ b/satrs-macros/tests/verify_gen_struct.rs @@ -1,7 +1,17 @@ -use satrs_core::resultcode::ResultU16; +use satrs_core::resultcode::{ResultU16, ResultU16Ext}; use satrs_macros::*; -#[result(info = "This is a test result where the first parameter is foo")] +#[resultcode(info = "This is a test result where the first parameter is foo")] const TEST_RESULT: ResultU16 = ResultU16::const_new(0, 1); +// Create named reference of auto-generated struct, which can be used by IDEs etc. +const TEST_RESULT_EXT_REF: &ResultU16Ext = &TEST_RESULT_EXT; -fn main() {} +fn main() { + assert_eq!(TEST_RESULT_EXT.name, "TEST_RESULT"); + assert_eq!(TEST_RESULT_EXT.result, &TEST_RESULT); + assert_eq!( + TEST_RESULT_EXT.info, + "This is a test result where the first parameter is foo" + ); + assert_eq!(TEST_RESULT_EXT_REF.name, "TEST_RESULT"); +}