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> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn u8_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn u16_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn u32_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn u64_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn u128_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn i8_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn i16_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn i32_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn i64_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn i128_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn usize_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn isize_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn f32_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn f64_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn char_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn string_builder(&self) -> Box> { Box::new(DialoguerValueBuilder::(std::marker::PhantomData)) } fn vec_builder(&self) -> Box, Error = Self::Error>> where T: 'static + BuildableValue { Box::new(DialoguerVecBuilder::(std::marker::PhantomData)) } } pub struct DialoguerValueBuilder(std::marker::PhantomData); impl ValueBuilder for DialoguerValueBuilder 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 { dialoguer::Input::::new() .with_prompt(question) .interact_text() .map_err(DialoguerBackendError::from) } } pub struct DialoguerVecBuilder(std::marker::PhantomData); impl CollectionBuilder for DialoguerVecBuilder where T: 'static + BuildableValue { type Output = Vec; type Error = DialoguerBackendError; fn build_collection(&self, value_desc: &str) -> Result { let mut buf = vec![]; loop { let v: T = T::builder(DialoguerBackend).build_value(value_desc)?; buf.push(v); if !dialoguer::Input::::new().with_prompt("Another one?").interact_text()? { break } } Ok(buf) } }