use crate::field::FieldTrait;
use crate::input::read_select;
use std::fmt::Debug;
use std::str::FromStr;
#[derive(Debug)]
pub struct SelectField<T> {
pub prompt: String,
pub options: Vec<(T, String)>,
pub value: Option<T>,
}
impl<T> FieldTrait for SelectField<T>
where
T: 'static + Clone + PartialEq + Debug + FromStr,
T::Err: Debug,
{
fn fill(&mut self) -> Result<(), String> {
self.value = Some(read_select::<T>(&self.prompt, &self.options)?);
Ok(())
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn get_value(&self) -> Result<String, String> {
self.value
.as_ref()
.ok_or_else(|| "Field has no value".to_string())
.map(|v| format!("{:?}", v))
}
}