Crates.io | random-constructible-derive |
lib.rs | random-constructible-derive |
version | 0.10.0 |
source | src |
created_at | 2024-11-16 17:54:12.954155 |
updated_at | 2024-11-22 02:16:16.382914 |
description | Provides a derive macro for the random-constructible crate which is used for creating random instances of enums with weighted probabilities |
homepage | https://github.com/klebs6/klebs-general |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1450536 |
size | 65,809 |
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.
RandConstruct
for structs and enums without manual implementation.Option
fields using attributes.Some
values in Option<T>
fields.Add the following to your Cargo.toml
:
[dependencies]
random-constructible = "0.6.0"
random-constructible-derive = "0.6.0"
And include it in your crate:
use random_constructible::RandConstruct;
use random_constructible_derive::RandConstruct;
You can derive RandConstruct
for your structs to enable random generation:
use random_constructible::RandConstruct;
use random_constructible_derive::RandConstruct;
#[derive(RandConstruct)]
struct MyStruct {
a: u8,
b: String,
c: f64,
}
Similarly, you can derive RandConstruct
for enums:
use random_constructible::RandConstruct;
use random_constructible_derive::RandConstruct;
#[derive(RandConstruct)]
enum MyEnum {
VariantA,
VariantB(i32, String),
VariantC { x: f64, y: bool },
}
You can specify custom probabilities for each variant using the #[rand_construct(p = ...)]
attribute:
use random_constructible::RandConstruct;
use random_constructible_derive::RandConstruct;
#[derive(RandConstruct)]
enum MyEnum {
#[rand_construct(p = 0.5)]
VariantA,
#[rand_construct(p = 0.3)]
VariantB,
#[rand_construct(p = 0.2)]
VariantC,
}
Option
FieldsFor Option<T>
fields, you can specify the probability of generating a Some
value using the #[rand_construct(psome = ...)]
attribute:
use random_constructible::RandConstruct;
use random_constructible_derive::RandConstruct;
#[derive(RandConstruct)]
struct MyStruct {
#[rand_construct(psome = 0.8)]
optional_field: Option<String>,
}
If you don't specify psome
, it defaults to 0.5
.
use random_constructible::RandConstruct;
use random_constructible_derive::RandConstruct;
#[derive(RandConstruct)]
struct Config {
pub id: u32,
pub name: String,
#[rand_construct(psome = 0.9)]
pub description: Option<String>,
}
fn main() {
let random_config = Config::random();
println!("{:?}", random_config);
}
use random_constructible::RandConstruct;
use random_constructible_derive::RandConstruct;
#[derive(Debug, RandConstruct)]
enum Status {
#[rand_construct(p = 0.7)]
Active,
#[rand_construct(p = 0.2)]
Inactive,
#[rand_construct(p = 0.1)]
Pending,
}
fn main() {
let random_status = Status::random();
println!("Random Status: {:?}", random_status);
}
use random_constructible::RandConstruct;
use random_constructible_derive::RandConstruct;
#[derive(Debug, RandConstruct)]
enum Message {
Text(String),
Data(u8, u8, u8),
#[rand_construct(p = 0.05)]
Disconnect,
}
fn main() {
let random_message = Message::random();
println!("Random Message: {:?}", random_message);
}
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:
#[rand_construct(p = <probability>)]
: Sets the probability for an enum variant. The probabilities are relative weights.#[rand_construct(psome = <probability>)]
: Sets the probability of Some
for Option<T>
fields.RandConstruct
.RandConstruct
where necessary.f64
literals.Contributions are welcome! Please feel free to submit a pull request or open an issue for any bugs or feature requests.
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.