Skip to main content

fake/impls/std/
option.rs

1use crate::{faker::boolean::en::Boolean, Dummy, Fake, Faker};
2use rand::RngExt;
3
4impl<T, U> Dummy<U> for Option<T>
5where
6    T: Dummy<U>,
7{
8    fn dummy_with_rng<R: RngExt + ?Sized>(config: &U, rng: &mut R) -> Self {
9        if Faker.fake_with_rng::<bool, _>(rng) {
10            Some(T::dummy_with_rng(config, rng))
11        } else {
12            None
13        }
14    }
15}
16
17#[derive(Debug)]
18pub struct Optional<T>(pub Option<T>);
19
20impl<T> From<Optional<T>> for Option<T> {
21    fn from(opt: Optional<T>) -> Self {
22        opt.0
23    }
24}
25
26#[derive(Debug)]
27pub struct Opt<T>(pub T, pub u8);
28
29impl<T, U> Dummy<Opt<U>> for Optional<T>
30where
31    T: Dummy<U>,
32{
33    fn dummy_with_rng<R: RngExt + ?Sized>(config: &Opt<U>, rng: &mut R) -> Self {
34        if Boolean(config.1).fake_with_rng(rng) {
35            Optional(Some(config.0.fake_with_rng(rng)))
36        } else {
37            Optional(None)
38        }
39    }
40}