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
New features planned are available here on github. See below for examples of current usage & features.
This library requires the nightly
channel.
Add restructed
to your projects Cargo.toml
:
restructed = "0.1"
alternatively run this in the project directory
cargo add restructed
Add the import and derive it on the target struct
use restructed::Models;
#[derive(restructed::Models)]
struct User {
id: i32,
username: String
}
And then add attributes for each model you want to create.
#[derive(restructed::Models)]
#[patch(UserUpdatables, omit(id))] // <-- Wraps all fields in a new struct with Option
#[view(UserId, fields(id))] // <-- Selectively includes fields in a new struct
struct User {
id: i32,
username: String,
}
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
.
Each model is defined using an attribute after deriving Models
and multiple models (of the same kind) can be had with multiple attributes.
A selective subset of fields from the original model of the same types. Useful for generating views of a database table, etc. Supports both struct and enums
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)
)default_derives
- A bool, iftrue
(default) then the a list of derives will be additionally derived. Otherwise,false
to avoid this (e.g.default_derives = false
)
Example:
// Original
#[derive(restructed::Models)]
#[view(UserProfile, fields(display_name, bio), derive(Clone), default_derives = false)]
struct User {
id: i32,
display_name: String,
bio: String,
password: String,
}
Generates:
#[derive(Clone)]
struct UserProfile {
display_name: String,
bio: String,
}
A complete subset of fields of the original model wrapped in Option<T>
with the ability to omit instead select fields.
I want to note that patch currently doesn't support enums as I don't see the use-case for it. If someone can, feel free to submit a feature request
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 (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)
)default_derives
- A bool, iftrue
(default) then the a list of derives will be additionally derived. Otherwise,false
to avoid this (e.g.default_derives = false
)
Example:
// Original
#[derive(restructed::Models)]
#[patch(UserUpdate, omit(id))]
struct User {
id: i32,
display_name: String,
bio: String,
password: String,
}
Generates:
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)] // <-- Default derives (when *not* disabled)
struct UserUpdate {
display_name: Option<String>,
bio: Option<String>, // MaybeUndefined<String> with feature 'openapi'
password: Option<String>,
}
Links are to other crates GitHub page that are related to the features.
Only builder
is enabled by default.
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..
Uses the typed-builder crate to derive a builder for add a type safe builder for all generated models.
Generates a function to merge changes for returning a DbState<T>
from the welds crate.
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.