1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::{Dummy, Fake, Faker};
use rand::Rng;
impl<T, E> Dummy<Faker> for Result<T, E>
where
T: Dummy<Faker>,
E: Dummy<Faker>,
{
fn dummy_with_rng<R: Rng + ?Sized>(config: &Faker, rng: &mut R) -> Self {
if Faker.fake_with_rng::<bool, _>(rng) {
Ok(T::dummy_with_rng(config, rng))
} else {
Err(E::dummy_with_rng(config, rng))
}
}
}
pub struct ResultFaker<T, E> {
ok: T,
err: E,
}
impl ResultFaker<Faker, Faker> {
pub fn default() -> ResultFaker<Faker, Faker> {
ResultFaker {
ok: Faker,
err: Faker,
}
}
}
impl<T> ResultFaker<T, Faker> {
pub fn ok(ok: T) -> ResultFaker<T, Faker> {
ResultFaker { ok, err: Faker }
}
}
impl<E> ResultFaker<Faker, E> {
pub fn err(err: E) -> ResultFaker<Faker, E> {
ResultFaker { ok: Faker, err }
}
}
impl<T, E> ResultFaker<T, E> {
pub fn with(ok: T, err: E) -> ResultFaker<T, E> {
ResultFaker { ok, err }
}
}
impl<T, E, U, V> Dummy<ResultFaker<U, V>> for Result<T, E>
where
T: Dummy<U>,
E: Dummy<V>,
{
fn dummy_with_rng<R: Rng + ?Sized>(config: &ResultFaker<U, V>, rng: &mut R) -> Self {
if Faker.fake_with_rng::<bool, _>(rng) {
Ok(T::dummy_with_rng(&config.ok, rng))
} else {
Err(E::dummy_with_rng(&config.err, rng))
}
}
}