diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-04-24 12:35:13 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-04-24 12:35:13 +0200 |
commit | fcfdebaed634c9b93555da14bec397bdd603bdbf (patch) | |
tree | 52168df7163efd73fc4d031bf1b08b427cf7cbe4 | |
parent | 39c3321f86e6360d249d207f32ae3c7b41f44cd3 (diff) |
Reimplement using generics
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/backend/dialoguer.rs | 252 |
1 files changed, 24 insertions, 228 deletions
diff --git a/src/backend/dialoguer.rs b/src/backend/dialoguer.rs index 01b0eec..6689250 100644 --- a/src/backend/dialoguer.rs +++ b/src/backend/dialoguer.rs @@ -13,293 +13,89 @@ impl Backend for DialoguerBackend { type Error = DialoguerBackendError; fn bool_builder(&self) -> Box<dyn ValueBuilder<Output = bool, Error = Self::Error>> { - Box::new(DialoguerBoolValueBuilder) + Box::new(DialoguerValueBuilder::<bool>(std::marker::PhantomData)) } fn u8_builder(&self) -> Box<dyn ValueBuilder<Output = u8, Error = Self::Error>> { - Box::new(DialoguerU8ValueBuilder) + Box::new(DialoguerValueBuilder::<u8>(std::marker::PhantomData)) } fn u16_builder(&self) -> Box<dyn ValueBuilder<Output = u16, Error = Self::Error>> { - Box::new(DialoguerU16ValueBuilder) + Box::new(DialoguerValueBuilder::<u16>(std::marker::PhantomData)) } fn u32_builder(&self) -> Box<dyn ValueBuilder<Output = u32, Error = Self::Error>> { - Box::new(DialoguerU32ValueBuilder) + Box::new(DialoguerValueBuilder::<u32>(std::marker::PhantomData)) } fn u64_builder(&self) -> Box<dyn ValueBuilder<Output = u64, Error = Self::Error>> { - Box::new(DialoguerU64ValueBuilder) + Box::new(DialoguerValueBuilder::<u64>(std::marker::PhantomData)) } fn u128_builder(&self) -> Box<dyn ValueBuilder<Output = u128, Error = Self::Error>> { - Box::new(DialoguerU128ValueBuilder) + Box::new(DialoguerValueBuilder::<u128>(std::marker::PhantomData)) } fn i8_builder(&self) -> Box<dyn ValueBuilder<Output = i8, Error = Self::Error>> { - Box::new(DialoguerI8ValueBuilder) + Box::new(DialoguerValueBuilder::<i8>(std::marker::PhantomData)) } fn i16_builder(&self) -> Box<dyn ValueBuilder<Output = i16, Error = Self::Error>> { - Box::new(DialoguerI16ValueBuilder) + Box::new(DialoguerValueBuilder::<i16>(std::marker::PhantomData)) } fn i32_builder(&self) -> Box<dyn ValueBuilder<Output = i32, Error = Self::Error>> { - Box::new(DialoguerI32ValueBuilder) + Box::new(DialoguerValueBuilder::<i32>(std::marker::PhantomData)) } fn i64_builder(&self) -> Box<dyn ValueBuilder<Output = i64, Error = Self::Error>> { - Box::new(DialoguerI64ValueBuilder) + Box::new(DialoguerValueBuilder::<i64>(std::marker::PhantomData)) } fn i128_builder(&self) -> Box<dyn ValueBuilder<Output = i128, Error = Self::Error>> { - Box::new(DialoguerI128ValueBuilder) + Box::new(DialoguerValueBuilder::<i128>(std::marker::PhantomData)) } fn usize_builder(&self) -> Box<dyn ValueBuilder<Output = usize, Error = Self::Error>> { - Box::new(DialoguerUsizeValueBuilder) + Box::new(DialoguerValueBuilder::<usize>(std::marker::PhantomData)) } fn isize_builder(&self) -> Box<dyn ValueBuilder<Output = isize, Error = Self::Error>> { - Box::new(DialoguerIsizeValueBuilder) + Box::new(DialoguerValueBuilder::<isize>(std::marker::PhantomData)) } fn f32_builder(&self) -> Box<dyn ValueBuilder<Output = f32, Error = Self::Error>> { - Box::new(DialoguerF32ValueBuilder) + Box::new(DialoguerValueBuilder::<f32>(std::marker::PhantomData)) } fn f64_builder(&self) -> Box<dyn ValueBuilder<Output = f64, Error = Self::Error>> { - Box::new(DialoguerF64ValueBuilder) + Box::new(DialoguerValueBuilder::<f64>(std::marker::PhantomData)) } fn char_builder(&self) -> Box<dyn ValueBuilder<Output = char, Error = Self::Error>> { - Box::new(DialoguerCharValueBuilder) + Box::new(DialoguerValueBuilder::<char>(std::marker::PhantomData)) } fn string_builder(&self) -> Box<dyn ValueBuilder<Output = String, Error = Self::Error>> { - Box::new(DialoguerStringValueBuilder) + Box::new(DialoguerValueBuilder::<String>(std::marker::PhantomData)) } } -pub struct DialoguerBoolValueBuilder; -impl ValueBuilder for DialoguerBoolValueBuilder { - type Output = bool; +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::new() + dialoguer::Input::<T>::new() .with_prompt(question) .interact_text() .map_err(DialoguerBackendError::from) } } -pub struct DialoguerU8ValueBuilder; -impl ValueBuilder for DialoguerU8ValueBuilder { - type Output = u8; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerU16ValueBuilder; -impl ValueBuilder for DialoguerU16ValueBuilder { - type Output = u16; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerU32ValueBuilder; -impl ValueBuilder for DialoguerU32ValueBuilder { - type Output = u32; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerU64ValueBuilder; -impl ValueBuilder for DialoguerU64ValueBuilder { - type Output = u64; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerU128ValueBuilder; -impl ValueBuilder for DialoguerU128ValueBuilder { - type Output = u128; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerI8ValueBuilder; -impl ValueBuilder for DialoguerI8ValueBuilder { - type Output = i8; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerI16ValueBuilder; -impl ValueBuilder for DialoguerI16ValueBuilder { - type Output = i16; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerI32ValueBuilder; -impl ValueBuilder for DialoguerI32ValueBuilder { - type Output = i32; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerI64ValueBuilder; -impl ValueBuilder for DialoguerI64ValueBuilder { - type Output = i64; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerI128ValueBuilder; -impl ValueBuilder for DialoguerI128ValueBuilder { - type Output = i128; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerUsizeValueBuilder; -impl ValueBuilder for DialoguerUsizeValueBuilder { - type Output = usize; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerIsizeValueBuilder; -impl ValueBuilder for DialoguerIsizeValueBuilder { - type Output = isize; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerF32ValueBuilder; -impl ValueBuilder for DialoguerF32ValueBuilder { - type Output = f32; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerF64ValueBuilder; -impl ValueBuilder for DialoguerF64ValueBuilder { - type Output = f64; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerCharValueBuilder; -impl ValueBuilder for DialoguerCharValueBuilder { - type Output = char; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} - -pub struct DialoguerStringValueBuilder; -impl ValueBuilder for DialoguerStringValueBuilder { - type Output = String; - type Error = DialoguerBackendError; - - fn build_value(&self, question: &str) -> Result<Self::Output, Self::Error> { - dialoguer::Input::new() - .with_prompt(question) - .interact_text() - .map_err(DialoguerBackendError::from) - } -} |