Skip to content

feat: allow to access already set fields within pattern of partial shared builder  #111

@dzmitry-lahoda

Description

@dzmitry-lahoda
use bon::builder;

#[builder(on(String, into))]
struct User {
    name: String,
    tags: Vec<String>,
    alias: Option<String>,
    description: Option<String>,
}

// Common part is here
let user = User::builder()
    .name("Bon")
    .tags(vec!["confectioner".to_owned()]);

// Differences are in each branch
let user = if user.GET_name().as_str() == "Bonny" {
    user
        .alias("Good girl")
        .description("Knows mathematics 🐱")
        .build()
} else {
    user
        .description("Skipped math classes 😿")
        .build()
};

real world usage

proptest allows to chain into hierarchy generated properties via prop_flat_map (with optimized shrinking retained).

so each prop_flat_map in chain depends on data to be generated from upper layer, uses that data to constraint its out put.

prop_flat_map(|prev| prev, prev+1 ).prop_flat_map(|(prev_0, prev_1|, ..., 0..prev_1).

so final test function gets do_test(prev_0, prev_1, prev_2).

so I thin of using builder on each stage and path only partial builder, instead of many parameters. but need access to already set fields. in the end I do call build and produce final data struct.

example without builder:

    runner
        .run(
            &(
                1..1_000_000u64,
                0..(7u8 + 1),
                0..(6u8 + 1),
                0..(6u8 + 1),
                Margins::valid_margins_strategy(),
            )
                .prop_flat_map(
                    |(
                        market_max_balance_in_units,
                        base_token_decimals,
                        quote_token_decimals,
                        market_price_decimals,
                        market_margins,
                    )| {
                        (
                            Just(market_max_balance_in_units),
                            Just(base_token_decimals),
                            Just(quote_token_decimals),
                            Just(market_price_decimals),
                            1u64..(market_max_balance_in_units + 1),
                            0..(base_token_decimals + 1),
                            1..(Decimals::from_repr(market_price_decimals)
                                .add_u8(2)
                                .scale_u64()
                                + 1), // price_mantissa
                            Just(market_margins),
                            // TODO: variate markets and token counts
                            // TODO: variate Weigth and Margins - make sure that balance of user is inverse to this
                            // TODO: variate many makers vs one taker (ensure full fill always)
                            // TODO: pepr market
                            // TODO: variate limit via size/price/quote (use inverse formuals to set limit correct)
                            // TODO: variate fill types as long as they lead to same results (full fills)
                            // TODO: extend to partial fill results (will lead to test running very long time, need to configure test runner) - so some amounts stay on balance
                        )
                    },
                ),
            |(
                market_max_balance_in_units,
                base_token_decimals,
                quote_token_decimals,
                market_price_decimals,
                base_traded_in_size,
                market_size_decimals,
                price_mantissa,
                market_margins,
            )| {
                //TODO: failed trades are fine, so somehow needed to count this
                let _ = assert_different_market_setups_and_order_sizes_filled_well(
                    base_token_decimals,
                    quote_token_decimals,
                    market_size_decimals,
                    market_price_decimals,
                    market_max_balance_in_units,
                    base_traded_in_size,
                    price_mantissa,
                    market_margins,
                );
                Ok(())
            },
        )
        .unwrap();

https://altsysrq.github.io/proptest-book/proptest/tutorial/higher-order.html

A note for the community from the maintainers

Please vote on this issue by adding a 👍 reaction to help the maintainers with prioritizing it. You may add a comment describing your real use case related to this issue for us to better understand the problem domain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions