diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-04-24 12:29:31 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-04-24 12:29:31 +0200 |
commit | 39c3321f86e6360d249d207f32ae3c7b41f44cd3 (patch) | |
tree | 19a6ebe49df03d0b66c471071d4d9862295fd09c | |
parent | c3b6b537e0afcab444e5558bd3e96e9da3940fdc (diff) |
Implement interfaces for all simple types
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/backend/dialoguer.rs | 282 | ||||
-rw-r--r-- | src/backend/mod.rs | 18 | ||||
-rw-r--r-- | src/buildable.rs | 144 |
3 files changed, 439 insertions, 5 deletions
diff --git a/src/backend/dialoguer.rs b/src/backend/dialoguer.rs index c2e944d..01b0eec 100644 --- a/src/backend/dialoguer.rs +++ b/src/backend/dialoguer.rs @@ -4,28 +4,302 @@ use crate::ValueBuilder; pub struct DialoguerBackend; #[derive(Debug, thiserror::Error)] -pub enum DialogerBackendError { +pub enum DialoguerBackendError { #[error("")] Io(#[from] std::io::Error), } impl Backend for DialoguerBackend { - type Error = DialogerBackendError; + type Error = DialoguerBackendError; fn bool_builder(&self) -> Box<dyn ValueBuilder<Output = bool, Error = Self::Error>> { Box::new(DialoguerBoolValueBuilder) } + + fn u8_builder(&self) -> Box<dyn ValueBuilder<Output = u8, Error = Self::Error>> { + Box::new(DialoguerU8ValueBuilder) + } + + fn u16_builder(&self) -> Box<dyn ValueBuilder<Output = u16, Error = Self::Error>> { + Box::new(DialoguerU16ValueBuilder) + } + + fn u32_builder(&self) -> Box<dyn ValueBuilder<Output = u32, Error = Self::Error>> { + Box::new(DialoguerU32ValueBuilder) + } + + fn u64_builder(&self) -> Box<dyn ValueBuilder<Output = u64, Error = Self::Error>> { + Box::new(DialoguerU64ValueBuilder) + } + + fn u128_builder(&self) -> Box<dyn ValueBuilder<Output = u128, Error = Self::Error>> { + Box::new(DialoguerU128ValueBuilder) + } + + fn i8_builder(&self) -> Box<dyn ValueBuilder<Output = i8, Error = Self::Error>> { + Box::new(DialoguerI8ValueBuilder) + } + + fn i16_builder(&self) -> Box<dyn ValueBuilder<Output = i16, Error = Self::Error>> { + Box::new(DialoguerI16ValueBuilder) + } + + fn i32_builder(&self) -> Box<dyn ValueBuilder<Output = i32, Error = Self::Error>> { + Box::new(DialoguerI32ValueBuilder) + } + + fn i64_builder(&self) -> Box<dyn ValueBuilder<Output = i64, Error = Self::Error>> { + Box::new(DialoguerI64ValueBuilder) + } + + fn i128_builder(&self) -> Box<dyn ValueBuilder<Output = i128, Error = Self::Error>> { + Box::new(DialoguerI128ValueBuilder) + } + + fn usize_builder(&self) -> Box<dyn ValueBuilder<Output = usize, Error = Self::Error>> { + Box::new(DialoguerUsizeValueBuilder) + } + + fn isize_builder(&self) -> Box<dyn ValueBuilder<Output = isize, Error = Self::Error>> { + Box::new(DialoguerIsizeValueBuilder) + } + + fn f32_builder(&self) -> Box<dyn ValueBuilder<Output = f32, Error = Self::Error>> { + Box::new(DialoguerF32ValueBuilder) + } + + fn f64_builder(&self) -> Box<dyn ValueBuilder<Output = f64, Error = Self::Error>> { + Box::new(DialoguerF64ValueBuilder) + } + + fn char_builder(&self) -> Box<dyn ValueBuilder<Output = char, Error = Self::Error>> { + Box::new(DialoguerCharValueBuilder) + } + + fn string_builder(&self) -> Box<dyn ValueBuilder<Output = String, Error = Self::Error>> { + Box::new(DialoguerStringValueBuilder) + } + } pub struct DialoguerBoolValueBuilder; impl ValueBuilder for DialoguerBoolValueBuilder { type Output = bool; - type Error = DialogerBackendError; + 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 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(DialogerBackendError::from) + .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) + } +} + diff --git a/src/backend/mod.rs b/src/backend/mod.rs index c3f9cf4..dfd77c8 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -6,7 +6,23 @@ use crate::ValueBuilder; pub trait Backend { type Error: Sized; - fn bool_builder(&self) -> Box<dyn ValueBuilder<Output = bool, Error = Self::Error>>; + fn bool_builder(&self) -> Box<dyn ValueBuilder<Output = bool, Error = Self::Error>>; + fn u8_builder(&self) -> Box<dyn ValueBuilder<Output = u8, Error = Self::Error>>; + fn u16_builder(&self) -> Box<dyn ValueBuilder<Output = u16, Error = Self::Error>>; + fn u32_builder(&self) -> Box<dyn ValueBuilder<Output = u32, Error = Self::Error>>; + fn u64_builder(&self) -> Box<dyn ValueBuilder<Output = u64, Error = Self::Error>>; + fn u128_builder(&self) -> Box<dyn ValueBuilder<Output = u128, Error = Self::Error>>; + fn i8_builder(&self) -> Box<dyn ValueBuilder<Output = i8, Error = Self::Error>>; + fn i16_builder(&self) -> Box<dyn ValueBuilder<Output = i16, Error = Self::Error>>; + fn i32_builder(&self) -> Box<dyn ValueBuilder<Output = i32, Error = Self::Error>>; + fn i64_builder(&self) -> Box<dyn ValueBuilder<Output = i64, Error = Self::Error>>; + fn i128_builder(&self) -> Box<dyn ValueBuilder<Output = i128, Error = Self::Error>>; + fn usize_builder(&self) -> Box<dyn ValueBuilder<Output = usize, Error = Self::Error>>; + fn isize_builder(&self) -> Box<dyn ValueBuilder<Output = isize, Error = Self::Error>>; + fn f32_builder(&self) -> Box<dyn ValueBuilder<Output = f32, Error = Self::Error>>; + fn f64_builder(&self) -> Box<dyn ValueBuilder<Output = f64, Error = Self::Error>>; + fn char_builder(&self) -> Box<dyn ValueBuilder<Output = char, Error = Self::Error>>; + fn string_builder(&self) -> Box<dyn ValueBuilder<Output = String, Error = Self::Error>>; } diff --git a/src/buildable.rs b/src/buildable.rs index 718ca73..d1d2572 100644 --- a/src/buildable.rs +++ b/src/buildable.rs @@ -18,3 +18,147 @@ impl BuildableValue for bool { } } +impl BuildableValue for u8 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = u8, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.u8_builder() + } +} + +impl BuildableValue for u16 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = u16, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.u16_builder() + } +} + +impl BuildableValue for u32 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = u32, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.u32_builder() + } +} + +impl BuildableValue for u64 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = u64, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.u64_builder() + } +} + +impl BuildableValue for u128 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = u128, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.u128_builder() + } +} + +impl BuildableValue for i8 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = i8, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.i8_builder() + } +} + +impl BuildableValue for i16 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = i16, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.i16_builder() + } +} + +impl BuildableValue for i32 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = i32, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.i32_builder() + } +} + +impl BuildableValue for i64 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = i64, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.i64_builder() + } +} + +impl BuildableValue for i128 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = i128, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.i128_builder() + } +} + +impl BuildableValue for usize { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = usize, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.usize_builder() + } +} + +impl BuildableValue for isize { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = isize, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.isize_builder() + } +} + +impl BuildableValue for f32 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = f32, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.f32_builder() + } +} + +impl BuildableValue for f64 { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = f64, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.f64_builder() + } +} + +impl BuildableValue for char { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = char, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.char_builder() + } +} + +impl BuildableValue for String { + fn builder<E, B>(backend: B) -> Box<dyn ValueBuilder<Output = String, Error = E>> + where E: Sized, + B: Backend<Error = E> + { + backend.string_builder() + } +} + |