restructed
A quick and easy way to create derivative models of your existing types without repeating yourself all the damn time.
- Reduce number of structs you need to write
- Allows deriving on generated structs
- Allows generating multiple structs from one derive
- Automatically generates
From<T>traits for original <> generated structs
Usage
This library requires the nightly channel.
Add restructed to your projects Cargo.toml:
= "0.1"
alternatively run this in the project directory
Add the import and derive it on the target struct
use Models;
And then add attributes for each model you want to create.
// <-- Wraps all fields in a new struct with Option
// <-- Selectively includes fields in a new struct
Continue reading for the available models and their breakdown.
Now anywhere that requires a T: OrderStoreFilter will also accept an &T or Arc<T> where T implements OrderStoreFilter.
Models
Each model is defined using an attribute after deriving Models and multiple models (of the same kind) can be had with multiple attributes.
view
A selective subset of fields from the original model of the same types.
Arguements:
name- The name of the struct the generate (Required, Must be first e.g.MyStruct)fields- A list of field names in the original structure to carry over (Required, e.g.fields(field1, field2, ...))derive- A list of derivables (in scope) to derive on the generated struct (e.g.derive(Clone, Debug, thiserror::Error))derive_defaults- A bool, iftrue(default) then the a list of derives will be additionally derived. Otherwise,falseto avoid this (e.g.derive_defaults = false)
Example:
// Original
// Generates
patch
A complete subset of fields of the original model wrapped in Option<T> with the ability to omit instead select fields.
Arguements:
name- The name of the struct the generate (Required, Must be first e.g.MyStruct)omit- A list of field names in the original structure to omit (Required, e.g.fields(field1, field2, ...))derive- A list of derivables (in scope) to derive on the generated struct (e.g.derive(Clone, Debug, thiserror::Error))derive_defaults- A bool, iftrue(default) then the a list of derives will be additionally derived. Otherwise,falseto avoid this (e.g.derive_defaults = false)
Example:
// Original
// Generates
// <-- Default derives (when *not* disabled)
Crate Features
Links are to other crates GitHub page that are related to the features.
Only builder is enabled by default.
openapi
Wraps Option<T> from the source struct with MaybeUndefined<T> from the poem-openapi crate in patch models. All oai(...) attributes are also copied over to the generated struct meaning you keep all validators, etc..
builder
Uses the typed-builder crate to derive a builder for add a type safe builder for all generated models.
welds
Generates a function to merge changes for returning a DbState<T> from the welds crate.
Contributions & Bugs
This is my first self publish proc macro so any feedback and feature request, changes, pull requests are all welcome! If you find any bugs do submit a github issue with any relavent information and I'll try to fix it.