summaryrefslogtreecommitdiffstats
path: root/examples/config.rs
blob: a91aaee79c9dcd0359edcaae4372078f4661207b (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
use interactive_object_builder::*;
use interactive_object_builder::dialoguer::DialoguerBackend;

#[derive(Debug)]
pub struct Configuration {
    verbose: bool,
    values: Vec<u8>,
}

impl BuildableCollection for Configuration {
    fn builder<B>(_backend: B) -> Box<dyn CollectionBuilder<Output = Self>>
        where B: Backend
    {
        Box::new(ConfigStructBuilder)
    }
}

pub struct ConfigStructBuilder;
impl CollectionBuilder for ConfigStructBuilder {
    type Output = Configuration;

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

        let verbose = bool::builder(DialoguerBackend).build_value("Be verbose?")?;
        let values = Vec::<u8>::builder(DialoguerBackend).build_collection("List of values")?;

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

fn main() -> Result<()> {
    let c = Configuration::builder(DialoguerBackend).build_collection("config")?;
    dbg!(c);
    Ok(())
}