summaryrefslogtreecommitdiffstats
path: root/tests/struct.rs
blob: 9845278b29324f996f51a9b10e26480026166830 (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
mod common;
use common::DummyBackend;

use interactive_object_builder::*;

pub struct Configuration {
    verbose: bool,
    values: Vec<u8>,
}

impl BuildableCollection for Configuration {
    fn builder<B>(backend: B) -> Box<dyn CollectionBuilder<Output = Self>>
        where B: 'static + Backend
    {
        Box::new(ConfigStructBuilder(backend.clone()))
    }
}

pub struct ConfigStructBuilder<B: Backend>(B);
impl<B: 'static + Backend> CollectionBuilder for ConfigStructBuilder<B> {
    type Output = Configuration;

    fn build_collection(&self, value_desc: &str) -> Result<Self::Output> {
        println!("Building {}", value_desc);

        let verbose = bool::builder(self.0).build_value(value_desc)?;
        let values = Vec::<u8>::builder(self.0).build_collection(value_desc)?;

        Ok(Configuration {
            verbose,
            values,
        })
    }
}

#[test]
fn test_struct() {
    let c = Configuration::builder(DummyBackend).build_collection("config").unwrap();
    assert!(c.verbose);
    assert_eq!(c.values, [42, 42, 42]);
}