diff options
Diffstat (limited to 'src/backend/dialoguer.rs')
-rw-r--r-- | src/backend/dialoguer.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/backend/dialoguer.rs b/src/backend/dialoguer.rs index 11d5349..5bdebd6 100644 --- a/src/backend/dialoguer.rs +++ b/src/backend/dialoguer.rs @@ -1,7 +1,7 @@ use crate::Backend; -use crate::ValueBuilder; use crate::BuildableValue; use crate::CollectionBuilder; +use crate::ValueBuilder; pub struct DialoguerBackend; @@ -88,6 +88,9 @@ impl Backend for DialoguerBackend { Box::new(DialoguerVecBuilder::<T>(std::marker::PhantomData)) } + fn option_builder<T: 'static + BuildableValue>(&self) -> Box<dyn CollectionBuilder<Output = Option<T>, Error = Self::Error>> { + Box::new(DialoguerOptionValueBuilder::<T>(std::marker::PhantomData)) + } } pub struct DialoguerValueBuilder<T: Sized>(std::marker::PhantomData<T>); @@ -106,6 +109,27 @@ impl<T> ValueBuilder for DialoguerValueBuilder<T> } } +pub struct DialoguerOptionValueBuilder<T: BuildableValue>(std::marker::PhantomData<T>); +impl<T: BuildableValue> CollectionBuilder for DialoguerOptionValueBuilder<T> { + type Output = Option<T>; + type Error = DialoguerBackendError; + + fn build_collection(&self, value_desc: &str) -> Result<Self::Output, Self::Error> { + 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) + .map(Some) + } else { + Ok(None) + } + } +} + pub struct DialoguerVecBuilder<T: 'static + BuildableValue>(std::marker::PhantomData<T>); impl<T> CollectionBuilder for DialoguerVecBuilder<T> |