first version works

This commit is contained in:
Robin Müller 2022-11-27 18:47:02 +01:00
parent 9b37c63280
commit 2315012448
No known key found for this signature in database
GPG Key ID: BE6480244DFE612C
6 changed files with 25 additions and 32 deletions

View File

@ -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())
}
}

View File

@ -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() {}

View File

@ -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() {}

View File

@ -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() {}

View File

@ -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");
}

View File

@ -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");
}