random-constructible-derive
random-constructible-derive is a procedural macro crate that provides custom derive macros for the random-constructible crate. It allows you to automatically implement the RandConstruct trait for your structs and enums, enabling easy random generation of complex types with minimal boilerplate.
Features
- Automatic Implementation: Derive
RandConstructfor structs and enums without manual implementation. - Support for Enums: Handle enums with unit variants, named fields, and unnamed fields, including custom probabilities.
- Support for Structs: Derive for structs with named, unnamed, or unit fields.
- Custom Probabilities: Specify custom probabilities for enum variants and
Optionfields using attributes. - Option Field Handling: Customize the probability of
Somevalues inOption<T>fields.
Installation
Add the following to your Cargo.toml:
[]
= "0.6.0"
= "0.6.0"
And include it in your crate:
use RandConstruct;
use RandConstruct;
Usage
Deriving for Structs
You can derive RandConstruct for your structs to enable random generation:
use RandConstruct;
use RandConstruct;
Deriving for Enums
Similarly, you can derive RandConstruct for enums:
use RandConstruct;
use RandConstruct;
Custom Probabilities for Enum Variants
You can specify custom probabilities for each variant using the #[rand_construct(p = ...)] attribute:
use RandConstruct;
use RandConstruct;
Handling Option Fields
For Option<T> fields, you can specify the probability of generating a Some value using the #[rand_construct(psome = ...)] attribute:
use RandConstruct;
use RandConstruct;
If you don't specify psome, it defaults to 0.5.
Examples
Struct Example
use RandConstruct;
use RandConstruct;
Enum Example with Custom Probabilities
use RandConstruct;
use RandConstruct;
Complex Enum with Fields
use RandConstruct;
use RandConstruct;
How It Works
The random-constructible-derive crate provides the RandConstruct derive macro, which automatically generates implementations of the RandConstruct trait for your types. It handles different kinds of data structures:
- Structs: Generates random values for each field, respecting any specified attributes.
- Enums: Generates random variants based on specified probabilities, and recursively generates random values for any associated data.
Attributes
#[rand_construct(p = <probability>)]: Sets the probability for an enum variant. The probabilities are relative weights.#[rand_construct(psome = <probability>)]: Sets the probability ofSomeforOption<T>fields.
Limitations
- The derive macro requires that all fields also implement
RandConstruct. - For generic types, ensure that the generic parameters are constrained with
RandConstructwhere necessary. - The current implementation assumes that probabilities are specified as
f64literals.
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue for any bugs or feature requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Note: This crate is a companion to the random-constructible crate. Ensure that you have both crates added to your dependencies to utilize the full functionality.