summaryrefslogtreecommitdiffstats
path: root/src/backend/dialoguer.rs
blob: 53eae8d998c996f8b7eb186e86d284c367307488 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::error::Result;
use crate::Backend;
use crate::BuildableValue;
use crate::BuildableCollection;
use crate::CollectionBuilder;
use crate::ValueBuilder;

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

impl Backend for DialoguerBackend {
    fn bool_builder(&self) -> Box<dyn ValueBuilder<Output = bool>> {
        Box::new(DialoguerValueBuilder::<bool>(std::marker::PhantomData))
    }

    fn u8_builder(&self)     -> Box<dyn ValueBuilder<Output = u8>> {
        Box::new(DialoguerValueBuilder::<u8>(std::marker::PhantomData))
    }

    fn u16_builder(&self)    -> Box<dyn ValueBuilder<Output = u16>> {
        Box::new(DialoguerValueBuilder::<u16>(std::marker::PhantomData))
    }

    fn u32_builder(&self)    -> Box<dyn ValueBuilder<Output = u32>> {
        Box::new(DialoguerValueBuilder::<u32>(std::marker::PhantomData))
    }

    fn u64_builder(&self)    -> Box<dyn ValueBuilder<Output = u64>> {
        Box::new(DialoguerValueBuilder::<u64>(std::marker::PhantomData))
    }

    fn u128_builder(&self)   -> Box<dyn ValueBuilder<Output = u128>> {
        Box::new(DialoguerValueBuilder::<u128>(std::marker::PhantomData))
    }

    fn i8_builder(&self)     -> Box<dyn ValueBuilder<Output = i8>> {
        Box::new(DialoguerValueBuilder::<i8>(std::marker::PhantomData))
    }

    fn i16_builder(&self)    -> Box<dyn ValueBuilder<Output = i16>> {
        Box::new(DialoguerValueBuilder::<i16>(std::marker::PhantomData))
    }

    fn i32_builder(&self)    -> Box<dyn ValueBuilder<Output = i32>> {
        Box::new(DialoguerValueBuilder::<i32>(std::marker::PhantomData))
    }

    fn i64_builder(&self)    -> Box<dyn ValueBuilder<Output = i64>> {
        Box::new(DialoguerValueBuilder::<i64>(std::marker::PhantomData))
    }

    fn i128_builder(&self)   -> Box<dyn ValueBuilder<Output = i128>> {
        Box::new(DialoguerValueBuilder::<i128>(std::marker::PhantomData))
    }

    fn usize_builder(&self)  -> Box<dyn ValueBuilder<Output = usize>> {
        Box::new(DialoguerValueBuilder::<usize>(std::marker::PhantomData))
    }

    fn isize_builder(&self)  -> Box<dyn ValueBuilder<Output = isize>> {
        Box::new(DialoguerValueBuilder::<isize>(std::marker::PhantomData))
    }

    fn f32_builder(&self)    -> Box<dyn ValueBuilder<Output = f32>> {
        Box::new(DialoguerValueBuilder::<f32>(std::marker::PhantomData))
    }

    fn f64_builder(&self)    -> Box<dyn ValueBuilder<Output = f64>> {
        Box::new(DialoguerValueBuilder::<f64>(std::marker::PhantomData))
    }

    fn char_builder(&self)   -> Box<dyn ValueBuilder<Output = char>> {
        Box::new(DialoguerValueBuilder::<char>(std::marker::PhantomData))
    }

    fn string_builder(&self) -> Box<dyn ValueBuilder<Output = String>> {
        Box::new(DialoguerValueBuilder::<String>(std::marker::PhantomData))
    }

    fn vec_builder<T>(&self) -> Box<dyn CollectionBuilder<Output = Vec<T>>>
        where T: 'static + BuildableValue
    {
        Box::new(DialoguerVecBuilder::<T>(std::marker::PhantomData))
    }

    fn option_builder<T: 'static + BuildableValue>(&self) -> Box<dyn CollectionBuilder<Output = Option<T>>> {
        Box::new(DialoguerOptionValueBuilder::<T>(std::marker::PhantomData))
    }

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

pub struct DialoguerValueBuilder<T: Sized>(std::marker::PhantomData<T>);
impl<T> ValueBuilder for DialoguerValueBuilder<T>
    where T: std::str::FromStr + Clone + std::fmt::Display,
          T::Err: std::fmt::Display + std::fmt::Debug,
{
    type Output = T;

    fn build_value(&self, question: &str, default: Option<&Self::Output>) -> Result<Self::Output> {
        let mut dia = dialoguer::Input::<T>::new();
        dia.with_prompt(question);

        if let Some(def) = default {
            dia.with_initial_text(def.to_string());
        }

        dia.interact_text().map_err(crate::error::IOBError::from)
    }
}

pub struct DialoguerOptionValueBuilder<T: BuildableValue>(std::marker::PhantomData<T>);
impl<T: BuildableValue> CollectionBuilder for DialoguerOptionValueBuilder<T> {
    type Output = Option<T>;

    fn build_collection(&self, value_desc: &str) -> Result<Self::Output> {
        let q = format!("Do you want to provide a Value for {}", value_desc);
        let b = dialoguer::Input::<bool>::new()
            .with_prompt(q)
            .interact_text()?;

        if b {
            T::builder(DialoguerBackend)
                .build_value(value_desc, None)
                .map(Some)
        } else {
            Ok(None)
        }
    }
}


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

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

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

            if !dialoguer::Input::<bool>::new().with_prompt("Another one?").interact_text()? {
                break
            }
        }

        Ok(buf)
    }
}


pub struct DialoguerStructBuilder<T: 'static + BuildableCollection>(std::marker::PhantomData<T>);
impl<T> CollectionBuilder for DialoguerStructBuilder<T>
    where T: 'static + BuildableCollection
{
    type Output = T;

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