summaryrefslogtreecommitdiffstats
path: root/tests/common/mod.rs
blob: 0ab668abbc0c8c81d6ef704b1ee2e540cd77bb6b (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use interactive_object_builder::*;

#[derive(Copy, Clone)]
pub struct DummyBackend;

impl Backend for DummyBackend {
    fn bool_builder(&self) -> Box<dyn ValueBuilder<Output = bool>> {
        Box::new(BoolValBuilder)
    }
    fn u8_builder(&self)     -> Box<dyn ValueBuilder<Output = u8>> {
        Box::new(U8ValueBuilder)
    }
    fn u16_builder(&self)    -> Box<dyn ValueBuilder<Output = u16>> {
        unimplemented!()
    }
    fn u32_builder(&self)    -> Box<dyn ValueBuilder<Output = u32>> {
        unimplemented!()
    }
    fn u64_builder(&self)    -> Box<dyn ValueBuilder<Output = u64>> {
        unimplemented!()
    }
    fn u128_builder(&self)   -> Box<dyn ValueBuilder<Output = u128>> {
        unimplemented!()
    }
    fn i8_builder(&self)     -> Box<dyn ValueBuilder<Output = i8>> {
        unimplemented!()
    }
    fn i16_builder(&self)    -> Box<dyn ValueBuilder<Output = i16>> {
        unimplemented!()
    }
    fn i32_builder(&self)    -> Box<dyn ValueBuilder<Output = i32>> {
        unimplemented!()
    }
    fn i64_builder(&self)    -> Box<dyn ValueBuilder<Output = i64>> {
        unimplemented!()
    }
    fn i128_builder(&self)   -> Box<dyn ValueBuilder<Output = i128>> {
        unimplemented!()
    }
    fn usize_builder(&self)  -> Box<dyn ValueBuilder<Output = usize>> {
        unimplemented!()
    }
    fn isize_builder(&self)  -> Box<dyn ValueBuilder<Output = isize>> {
        unimplemented!()
    }
    fn f32_builder(&self)    -> Box<dyn ValueBuilder<Output = f32>> {
        unimplemented!()
    }
    fn f64_builder(&self)    -> Box<dyn ValueBuilder<Output = f64>> {
        unimplemented!()
    }
    fn char_builder(&self)   -> Box<dyn ValueBuilder<Output = char>> {
        unimplemented!()
    }
    fn string_builder(&self) -> Box<dyn ValueBuilder<Output = String>> {
        unimplemented!()
    }
    fn option_builder<T: 'static + BuildableValue>(&self) -> Box<dyn CollectionBuilder<Output = Option<T>>> {
        unimplemented!()
    }
    fn vec_builder<T>(&self) -> Box<dyn CollectionBuilder<Output = Vec<T>>>
        where T: 'static + BuildableValue
    {
        Box::new(VecBuilder::<T>(std::marker::PhantomData))
    }

    fn struct_builder<T>(&self) -> Box<dyn CollectionBuilder<Output = T>>
        where T: 'static + BuildableCollection
    {
        Box::new(StructBuilder::<T>(std::marker::PhantomData))
    }
}

pub struct BoolValBuilder;
impl ValueBuilder for BoolValBuilder {
    type Output = bool;

    fn build_value(&self, _question: &str) -> Result<Self::Output> {
        Ok(true)
    }
}

pub struct U8ValueBuilder;
impl ValueBuilder for U8ValueBuilder {
    type Output = u8;

    fn build_value(&self, _question: &str) -> Result<Self::Output> {
        Ok(42)
    }
}

pub struct VecBuilder<T: 'static + BuildableValue>(std::marker::PhantomData<T>);
impl<T: BuildableValue> CollectionBuilder for VecBuilder<T> {
    type Output = Vec<T>;

    fn build_collection(&self, value_desc: &str) -> Result<Self::Output> {
        let mut buf = vec![];

        loop {
            let v: T = T::builder(DummyBackend).build_value(value_desc)?;
            buf.push(v);

            if buf.len() == 3 {
                break
            }
        }

        Ok(buf)
    }
}

pub struct StructBuilder<T: BuildableCollection>(std::marker::PhantomData<T>);
impl<T: BuildableCollection> CollectionBuilder for StructBuilder<T> {
    type Output = T;

    fn build_collection(&self, value_desc: &str) -> Result<Self::Output> {
        T::builder(DummyBackend).build_collection(value_desc)
    }
}