boulder/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3//! This crate is based around two traits, [`Buildable`] and
4//! [`Generatable`], and their associated derive macros, which provide
5//! ways to construct objects succinctly.
6//!
7//! # Builder
8//!
9//! A [`Builder`] is a way to create a single object, specifying only the
10//! fields you wish to give non-default values. The default values are
11//! specified using attributes on your type for ease of reading. Each
12//! field gains a method of the same type in the builder which can be
13//! be used to customise it.
14//!
15//! Example
16//! ```rust
17//! use boulder::{Builder, Buildable};
18//! #[derive(Buildable)]
19//! struct Foo {
20//!    #[boulder(default=5)]
21//!    pub a: i32,
22//!    #[boulder(default="hello")]
23//!    pub b: String,
24//! }
25//!
26//! let f = Foo::builder()
27//!          .a(27)
28//!          .build();
29//! assert_eq!(f.a, 27);
30//! assert_eq!(f.b, "hello".to_string());
31//! ```
32//! # Generator
33//!
34//! A [`Generator`] is a way to create an infinite sequence of
35//! objects, again specifying only the functions for generating the
36//! fields you wish to have non-default sequences. The default
37//! sequences are specified using attributes on your type for ease of
38//! reading. Each field gains a method of the same name in the
39//! generator, which can be be used to customise the sequence of
40//! values produced for that field as objects are made.
41//!
42//! Example
43//! ```rust
44//! use boulder::{Generator, Generatable, Inc, Pattern};
45//! #[derive(Generatable)]
46//! struct Bar {
47//!    #[boulder(generator=Inc(1i32))]
48//!    pub a: i32,
49//!    #[boulder(generator=Pattern!("hello-{}-foo", Inc(5i32)))]
50//!    pub b: String,
51//! }
52//!
53//! let mut n = 2;
54//! let mut gen = Bar::generator()
55//!      .a(move || {
56//!          n = n + 2;
57//!          n
58//! });
59//!
60//! let bar1 = gen.generate();
61//! assert_eq!(bar1.a, 4);
62//! assert_eq!(bar1.b, "hello-5-foo".to_string());
63//!
64//! let bar2 = gen.generate();
65//! assert_eq!(bar2.a, 6);
66//! assert_eq!(bar2.b, "hello-6-foo".to_string());
67//! ```
68//!
69
70#![cfg_attr(
71    feature = "persian-rug",
72    doc = r##"
73
74# persian-rug
75
76When the `"persian-rug"` feature is enabled, a parallel set of types,
77traits and derive macros are enabled, i.e. [`BuildableWithPersianRug`]
78and [`GeneratableWithPersianRug`]. These provide similar facilities,
79but for types based on the [persian-rug](::persian_rug) crate.
80
81Briefly, [persian-rug](::persian_rug) moves ownership of all values
82into a single container (the "rug" or
83[`Context`](::persian_rug::Context)). Client code then deals with handles
84(in the form [`Proxy<T>`](::persian_rug::Proxy)) to these values. The
85builders and generators take a mutator for the type's context in their
86[`build`](BuilderWithPersianRug::build) and
87[`generate`](GeneratorWithPersianRug::generate) methods, which means
88at the point of construction, the set of other constructed instances
89(of all types in that context) is available.
90
91Since the derived generators pass through
92[`Proxy<T>`](::persian_rug::Proxy), and since to get the most benefit from
93this system you should work with proxies where possible, the most common
94usage is to use these solely through the pass throughs.
95
96Example:
97```rust
98use boulder::{BuilderWithPersianRug, BuildableWithPersianRug};
99use persian_rug::{contextual, persian_rug, Context, Proxy};
100
101#[persian_rug]
102struct Rug(
103  #[table] Foo,
104  #[table] Bar,
105);
106
107#[contextual(Rug)]
108#[derive(BuildableWithPersianRug)]
109#[boulder(persian_rug(context=Rug))]
110struct Foo {
111   #[boulder(default=5)]
112   pub a: i32,
113   #[boulder(buildable_with_persian_rug(a=5))]
114   pub b: Proxy<Bar>,
115}
116
117#[contextual(Rug)]
118#[derive(BuildableWithPersianRug)]
119#[boulder(persian_rug(context=Rug))]
120struct Bar {
121  #[boulder(default=2)]
122  pub a: i32
123}
124
125let mut r = Rug(Default::default(), Default::default());
126let (f, _) = Proxy::<Foo>::builder()
127         .a(27)
128         .build(&mut r);
129assert_eq!(r.get(&f).a, 27);
130assert_eq!(r.get(&r.get(&f).b).a, 5);
131```
132
133"##
134)]
135
136mod builder;
137mod generator;
138
139pub use self::builder::{Buildable, Builder};
140pub use self::generator::generators::{
141    Const, Cycle, Inc, Pattern, Repeat, Sample, Some, Subsets, Time,
142};
143pub use self::generator::{Generatable, Generator};
144pub use self::generator::{GeneratorIterator, GeneratorMutIterator};
145
146#[cfg(feature = "persian-rug")]
147mod persian_rug;
148#[cfg(feature = "persian-rug")]
149#[cfg_attr(docsrs, doc(cfg(feature = "persian-rug")))]
150pub use self::persian_rug::{
151    BuildableWithPersianRug, BuilderWithPersianRug, GeneratableWithPersianRug,
152    GeneratorToGeneratorWithPersianRugWrapper, GeneratorWithPersianRug,
153    GeneratorWithPersianRugIterator, GeneratorWithPersianRugMutIterator, RepeatFromPersianRug,
154    SampleFromPersianRug, SequenceGeneratorWithPersianRug, SubsetsFromPersianRug,
155    TryRepeatFromPersianRug,
156};
157
158#[doc(hidden)]
159pub mod guts {
160    pub use crate::builder::guts as builder;
161    pub use crate::generator::guts as generator;
162
163    #[cfg(feature = "persian-rug")]
164    pub mod persian_rug {
165        pub use crate::persian_rug::builder::guts as builder;
166        pub use crate::persian_rug::generator::guts as generator;
167    }
168}