Skip to content

Rust macro to rename and delete fields from struct

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

keabarnes/struct_fragment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StructFragment

A macro to create a duplicate of a struct with keys removed.

I mainly developed this macro to help with creating structs for interacting with the diesel create:

  • original struct is the model, for reads from the database
  • a struct with id removed, used for inserts into the database
  • [coming soon] the ability to do this multiple times, allowing the definition of a struct fragment which would be received as a POST body.

Usage

Either providing a list of ignored fields:

#[derive(StructFragment)]
#[fragment_ignore_list = "id,updated_at"]
#[fragment_name = "DbUser"]
pub struct User {
    pub id: i32,
    pub full_name: String,
    pub email: String,
    pub updated_at: DateTime
}

Or adding #[fragment_ignore] to fields you don't want included.

#[derive(StructFragment)]
#[fragment_name = "DbUser"]
pub struct User {
    #[fragment_ignore] pub id: i32,
    pub full_name: String,
    pub email: String,
    #[fragment_ignore] pub updated_at: DateTime
}

Useful tools

About

Rust macro to rename and delete fields from struct

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages