#type #json #macro #compile-time #json-object #model-generation #json-export

yanked generate_model

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.

0.2.2 Aug 24, 2023
0.2.1 Aug 24, 2023
0.2.0 Aug 24, 2023
0.1.0 Aug 24, 2023

#82 in #json-object

MIT license

6KB

Build Test

CURRENTLY BROKEN D:<

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

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.

Requirements

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"] }

Installation

cargo install generate_model

Usage

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
}

Configuration

To specify a non-standard path you can set the environment variable generated_models_directory to specify an absolute path.

Contributing

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.

License

MIT

Dependencies

~1.8–2.8MB
~57K SLoC