macro struct-path

A helper macros to build a string that represents struct fields path at compile time (such as <field-name>.<field-name>)

7 releases

0.2.3 Jul 17, 2023
0.2.2 Oct 23, 2022
0.1.4 Oct 22, 2022
Download history 931/week @ 2024-07-21 2193/week @ 2024-07-28 2244/week @ 2024-08-04 2971/week @ 2024-08-11 3936/week @ 2024-08-18 4043/week @ 2024-08-25 3667/week @ 2024-09-01 4418/week @ 2024-09-08 4655/week @ 2024-09-15 4045/week @ 2024-09-22 4942/week @ 2024-09-29 4222/week @ 2024-10-06 3340/week @ 2024-10-13 4234/week @ 2024-10-20 3432/week @ 2024-10-27 3609/week @ 2024-11-03

14,801 downloads per month
Used in 2 crates (via firestore)

Apache-2.0

23KB
406 lines

Cargo tests and formatting security audit

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;

Quick start

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


Licence

Apache Software License (ASL)

Author

Abdulla Abdurakhmanov

Dependencies

~435KB