struct-path for Rust
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:
- Fast parsing without huge deps;
- Macro produces the code to verify if the specified path really exists;
- Multiple fields/arrays support
- Optional camelCase and PascalCase conversion support;
- Optional delimiter parameter;
- Support for
Iter-based (Option, Vec, etc) paths using~delimiter; #[derive(StructPath)]to return all of a struct's declared fields viaType::*, without listing them by hand;Type::*nested under a field path,Parent::child.(Child::*), to prefix every one ofChild's fields withchild;
Quick start
Cargo.toml:
[]
= "0.2"
Example code:
use *;
// returns "value_str"
let s1: &str = path!;
// returns "value_child.child_value_str"
let s2: &str = path! ;
// returns also "value_child.child_value_str"
let s3: &str = path!;
// returns "opt_value_child.child_value_str" using trait `Iter`
let s4: &str = path!;
// options, returns "valueChild/childValueStr"
let s5: &str = path! ;
// returns ["value_str", "value_num"]
let arr: = paths!;
All fields with Type::*
#[derive(StructPath)] is only needed for Type::*; none of the calls above need it.
use *;
// returns ["value_str"], only the field declared with plain `pub`
let pub_only: = paths!;
// returns ["value_str", "value_internal"], every declared field
let all_fields: = paths!;
// nested `Type::*`: `TestStructChild` also needs `#[derive(StructPath)]`, and
// is named again inside the parens; returns
// ["value_child.child_value_str", "value_child.child_value_num"]
let nested: = paths!;
// `~` before the parens steps through the `Option`; returns
// ["opt_value_child.child_value_str", "opt_value_child.child_value_num"]
let nested_opt: = paths!;
The inner type named inside the parens needs #[derive(StructPath)] too.
Lint gates on the derive
#[derive(StructPath)] reuses the struct's own visibility for the generated consts:
- a
pubstruct in a private module trips#![deny(unreachable_pub)]on those consts too, and an#[allow]on the struct does not reach them; - a struct with its own
implblock fails#![deny(clippy::multiple_inherent_impl)], since the derive adds one more.
Workaround: declare the struct pub(crate), or add the #[allow] at module level instead of on the struct. The derive never emits the #[allow] itself, since that would break a crate using #[forbid(...)] instead of #[deny(...)].
Options
delim = "<str>": sets the path separator, defaults to.;case = "camel"orcase = "pascal": converts each segment to camelCase or PascalCase;~: walks into a type with aniter()method, such asOptionorVec, instead of.;visibility = "all": withType::*, returns every declared field instead of only the ones declared plainpub;- Several
;groups are accepted, so a wrapper macro can append its own options. A trailing,may end a group before the next;. A key repeated in the same group or a later one takes the last value in a field list, whileType::*accepts it only with the same value;
Licence
Apache Software License (ASL)
Author
Abdulla Abdurakhmanov