Crates.io | generate_model |
lib.rs | generate_model |
version | 0.2.2 |
source | src |
created_at | 2023-08-24 18:17:07.766605 |
updated_at | 2023-08-24 19:26:52.239481 |
description | Rust macro used for generating JSON objects from structs and writing them to a directory at compile time. This is so that you can easily generate types for APIs. |
homepage | |
repository | |
max_upload_size | |
id | 954018 |
size | 6,563 |
working to fix. currently only works with structs inside functions. (which is how i was testing)
update: apparently i need reflection in order to pull the types out of the token stream which means that i cannot move forward with this in my current direction. for now this is dead before it even started.
generate_model is a Rust macro used for generating JSON objects from structs and writing them to a directory at compile time. This is so that you can easily generate types for APIs.
This macro requires your struct derive from both Dummy
(Faker) and Deserialize
Serde. As well as using serde_json
.
In your Cargo.toml
you'll want to have:
serde = { version = "1.0.186", features = ["derive"] }
serde_json = "1.0.105"
fake = { version = "2.8", features = ["derive"] }
cargo install generate_model
Just decorate your structs with #[generate_model]
. A folder will be created named generated_models
(you can also specify a path with an environment variable.)
#[generate_model]
#[derive(Dummy, Serialize)]
struct TestStruct {
a: String,
b: u32,
}
#[generate_model]
#[derive(Dummy, Serialize)]
struct TestStruct2 {
a: TestStruct,
b: u32,
}
And in the generated_models
folder you'll find...
TestStruct.json
{
"a": "Z4GOd",
"b": 2390283944
}
TestStruct2.json
{
"a": {
"a": "xuVpg",
"b": 1118086824
},
"b": 358934898
}
To specify a non-standard path you can set the environment variable generated_models_directory
to specify an absolute path.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.