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

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))
    }

}

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)
    }
}