first version works
This commit is contained in:
parent
9b37c63280
commit
2315012448
@ -10,15 +10,12 @@ struct ResultExtGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn result(
|
pub fn resultcode(
|
||||||
args: proc_macro::TokenStream,
|
args: proc_macro::TokenStream,
|
||||||
item: proc_macro::TokenStream,
|
item: proc_macro::TokenStream,
|
||||||
) -> proc_macro::TokenStream {
|
) -> proc_macro::TokenStream {
|
||||||
let args = parse_macro_input!(args as AttributeArgs);
|
let args = parse_macro_input!(args as AttributeArgs);
|
||||||
let input = parse_macro_input!(item as Item);
|
let input = parse_macro_input!(item as Item);
|
||||||
|
|
||||||
dbg!("Arguments: {}", &args);
|
|
||||||
dbg!("Input: {}", &input);
|
|
||||||
let mut result_ext_generator = ResultExtGenerator::default();
|
let mut result_ext_generator = ResultExtGenerator::default();
|
||||||
result_ext_generator.parse(args, input).into()
|
result_ext_generator.parse(args, input).into()
|
||||||
}
|
}
|
||||||
@ -53,10 +50,7 @@ impl ResultExtGenerator {
|
|||||||
} else {
|
} else {
|
||||||
return Err(syn::Error::new(
|
return Err(syn::Error::new(
|
||||||
path.span(),
|
path.span(),
|
||||||
format!(
|
format!("Unknown attribute argument name {}", path.ident),
|
||||||
"Unknown attribute argument name {}",
|
|
||||||
path.ident
|
|
||||||
),
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -93,13 +87,13 @@ impl ResultExtGenerator {
|
|||||||
let gen_struct_name = format_ident!("{}_EXT", result_code_name);
|
let gen_struct_name = format_ident!("{}_EXT", result_code_name);
|
||||||
let info_str = self.info_str.to_owned().unwrap();
|
let info_str = self.info_str.to_owned().unwrap();
|
||||||
let gen_struct = quote! {
|
let gen_struct = quote! {
|
||||||
const #gen_struct_name: satrs_core::resultcode::ResultU16Ext = satrs_core::resultcode::ResultU16Ext {
|
const #gen_struct_name: satrs_core::resultcode::ResultU16Ext =
|
||||||
name: #name_as_str,
|
satrs_core::resultcode::ResultU16Ext::const_new(
|
||||||
result: &#result_code_name,
|
#name_as_str,
|
||||||
info: #info_str
|
&#result_code_name,
|
||||||
};
|
#info_str
|
||||||
|
);
|
||||||
};
|
};
|
||||||
Ok(gen_struct)
|
Ok(gen_struct)
|
||||||
//Ok(TokenStream::new())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use satrs_core::resultcode::ResultU16;
|
use satrs_core::resultcode::ResultU16;
|
||||||
use satrs_macros::*;
|
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);
|
const _TEST_RESULT: ResultU16 = ResultU16::const_new(0, 1);
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
//! Basic check which just verifies that everything compiles
|
||||||
use satrs_core::resultcode::ResultU16;
|
use satrs_core::resultcode::ResultU16;
|
||||||
use satrs_macros::*;
|
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);
|
const TEST_RESULT: ResultU16 = ResultU16::const_new(0, 1);
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -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() {}
|
|
@ -2,6 +2,6 @@
|
|||||||
fn tests() {
|
fn tests() {
|
||||||
let t = trybuild::TestCases::new();
|
let t = trybuild::TestCases::new();
|
||||||
t.pass("tests/basic.rs");
|
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");
|
//t.pass("tests/group_in_enum.rs");
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,17 @@
|
|||||||
use satrs_core::resultcode::ResultU16;
|
use satrs_core::resultcode::{ResultU16, ResultU16Ext};
|
||||||
use satrs_macros::*;
|
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);
|
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");
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user