summaryrefslogtreecommitdiffstats
path: root/src/backend/dialoguer.rs
blob: 11d5349a6dc6c7cdfa5e07122f4c049da59dc821 (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
use crate::Backend;
use crate::ValueBuilder;
use crate::BuildableValue;
use crate::CollectionBuilder;

pub struct DialoguerBackend;

#[derive(Debug, thiserror::Error)]
pub enum DialoguerBackendError {
    #[error("")]
    Io(#[from] std::io::Error),
}

impl Backend for DialoguerBackend {
    type Error = DialoguerBackendError;

    fn bool_builder(&self) -> Box<dyn ValueBuilder<Output = bool, Error = Self::Error>> {
        Box::new(DialoguerValueBuilder::<bool>(std::marker::PhantomData))
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    fn vec_builder<T>(&self) -> Box<dyn CollectionBuilder<Output = Vec<T>, Error = Self::Error>>
        where T: 'static + BuildableValue
    {
        Box::new(DialoguerVecBuilder::<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;
    type Error = DialoguerBackendError;

    fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> {
        dialoguer::Input::<T>::new()
            .with_prompt(question)
            .interact_text()
            .map_err(DialoguerBackendError::from)
    }
}


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

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

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

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

        Ok(buf)
    }
}