Git Product home page Git Product logo

struct-path-rs's Introduction

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 "opt_value_child.child_value_str" using trait `Iter`
let s4: &str = path!(TestStructParent::opt_value_child~child_value_str);

// options, returns "valueChild/childValueStr"
let s5: &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

struct-path-rs's People

Contributors

abdolence avatar renovate[bot] avatar

Stargazers

Kamil Sindi avatar David M. Golembiowski avatar

Watchers

 avatar David M. Golembiowski avatar

Forkers

dmgolembiowski

struct-path-rs's Issues

Optional child

Hi, great library!

Is there any way to get this to work?

struct A {
  b: Option<B>
}
struct B {
  test: String
}
path!(A::b::test);

I know I can do path!(A::b, B::test), but would be nice to go directly as well

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

cargo
lib_tests/Cargo.toml
macro/Cargo.toml
  • convert_case 0.6
github-actions
.github/workflows/security-audit.yml
  • actions/checkout v4
  • actions-rs/toolchain v1
.github/workflows/tests.yml
  • actions/checkout v4
  • actions-rs/toolchain v1

  • Check this box to trigger a request for Renovate to run again on this repository

Support for serde's rename feature

While using the firestore crate I noticed that the path! macro doesn't support the rename feature of serde.

Example:

use serde::Serialize;
use struct_path::path;

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct Test {
    test_attribute: String,
}

fn main() {
    let t = Test {
        test_attribute: "Test".to_string(),
    };
    let json = serde_json::to_string(&t).unwrap();
    println!("{}", json);
    let path = path!(Test::test_attribute);
    println!("{}", path);
}

Current output:

{"testAttribute":"Test"}
test_attribute

Expected output:

{"testAttribute":"Test"}
testAttribute

Is it possible to support this feature?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.