Skip to content

Commit

Permalink
Implementing '~' field reference for Iter traits (such as Option, Vec…
Browse files Browse the repository at this point in the history
…, etc)
  • Loading branch information
abdolence committed Jul 17, 2023
1 parent d8887a6 commit b6cde7d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct TestStructParent {
pub value_str: String,
pub value_num: u64,
pub value_child: TestStructChild,
pub opt_value_str: Option<TestStructChild>,
}

pub struct TestStructChild {
Expand All @@ -47,6 +48,9 @@ let s2: &str = path!(TestStructParent::value_child.child_value_str) ;
// returns also "value_child.child_value_str"
let s3: &str = path!(TestStructParent::value_child,TestStructChild::child_value_str);

// returns also "value_child.child_value_str" using trait `Iter`
let s3: &str = path!(TestStructParent::opt_value_child~child_value_str);

// options, returns "valueChild/childValueStr"
let s4: &str = path!(TestStructParent::value_child.child_value_str; delim="/", case="camel") ;

Expand Down
4 changes: 4 additions & 0 deletions lib_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod tests {
pub value_str: String,
pub value_num: u64,
pub value_child: TestStructChild,
pub opt_value_child: Option<TestStructChild>,
}

pub struct TestStructChild {
Expand Down Expand Up @@ -34,6 +35,9 @@ mod tests {
TestStructChild::child_value_str
);
assert_eq!(test_mixed_path, "value_str.child_value_str");

let test_opt_child = path!(TestStructParent::opt_value_child~child_value_str);
assert_eq!(test_opt_child, "opt_value_child.child_value_str");
}

#[test]
Expand Down
11 changes: 6 additions & 5 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub fn paths(struct_path_stream: TokenStream) -> TokenStream {

for (_, struct_fields) in &found_structs {
for field_path in struct_fields {
let mut final_field_path = field_path.clone();
let mut final_field_path = field_path.clone().replace("~", ".");
if !options.is_empty() {
final_field_path = apply_options(&options, final_field_path);
}
Expand Down Expand Up @@ -330,9 +330,9 @@ pub fn path(struct_path_stream: TokenStream) -> TokenStream {
}
}
}
TokenTree::Punct(punct) if opened_struct && punct == '.' => {
TokenTree::Punct(punct) if opened_struct && (punct == '.' || punct == '~') => {
if let Some(ref mut field_path) = &mut current_field_path {
field_path.push('.');
field_path.push(punct.as_char());
} else {
panic!(
"Unexpected punctuation input for struct path group parameters: {:?}",
Expand Down Expand Up @@ -420,7 +420,7 @@ pub fn path(struct_path_stream: TokenStream) -> TokenStream {

if let Some(full_field_path) = current_full_field_path.take() {
let all_check_functions = generate_checks_code_for(&found_structs);
let final_field_path = apply_options(&options, full_field_path);
let final_field_path = apply_options(&options, full_field_path).replace("~", ".");
let result_str = format!("{{{}\n\"{}\"}}", all_check_functions, final_field_path);
result_str.parse().unwrap()
} else {
Expand All @@ -436,6 +436,7 @@ fn generate_checks_code_for(found_structs: &Vec<(String, Vec<String>)>) -> Strin
let check_functions = struct_fields
.iter()
.map(|field_path| {
let field_path_result = field_path.replace("~", ".iter().next().unwrap().");
format!(
r#"
{{
Expand All @@ -446,7 +447,7 @@ fn generate_checks_code_for(found_structs: &Vec<(String, Vec<String>)>) -> Strin
}}
}}
"#,
struct_name, field_path
struct_name, field_path_result
)
})
.collect::<Vec<String>>()
Expand Down

0 comments on commit b6cde7d

Please sign in to comment.