Crates.io | struct-path |
lib.rs | struct-path |
version | 0.2.3 |
source | src |
created_at | 2022-10-22 22:13:50.34978 |
updated_at | 2023-07-17 15:51:45.442048 |
description | A helper macros to build a string that represents struct fields path at compile time (such as |
homepage | https://github.com/abdolence/struct-path-rs |
repository | https://github.com/abdolence/struct-path-rs |
max_upload_size | |
id | 694914 |
size | 22,657 |
Library provides a tiny macro implementation to reference Rust struct fields at compile time to represent its string format. This is needed to work with JSON paths, and some others protocols when we still want to rely on the compiler to avoid inconsistent changes.
Features:
Iter
-based (Option, Vec, etc) paths using ~
delimiter;Cargo.toml:
[dependencies]
struct-path = "0.2"
Example code:
use struct_path::*;
pub struct TestStructParent {
pub value_str: String,
pub value_num: u64,
pub value_child: TestStructChild,
pub opt_value_str: Option<TestStructChild>,
}
pub struct TestStructChild {
pub child_value_str: String,
pub child_value_num: u64,
}
// returns "value_str"
let s1: &str = path!(TestStructParent::value_str);
// returns "value_child.child_value_str"
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") ;
// returns ["value_str", "value_num"]
let arr: [&str; 2] = paths!(TestStructParent::{ value_str, value_num });
Apache Software License (ASL)
Abdulla Abdurakhmanov