1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Utilities used to wrap user selections in [Select](crate::Select) and
//! [`MultiSelect`](crate::MultiSelect) prompts.
use std::fmt::{self, Display};
/// Represents a selection made by the user when prompted to select one or several
/// options among those presented.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListOption<T> {
/// Index of the selected option relative to the original (full) list passed to the prompt.
pub index: usize,
/// Value of the selected option.
pub value: T,
}
impl<T> ListOption<T> {
/// Constructor for `ListOption`.
///
/// # Arguments
///
/// * `index` - Index of the option.
/// * `value` - String value of the option
///
/// # Examples
///
/// ```
/// use inquire::list_option::ListOption;
///
/// let answer = ListOption::new(0, "a");
/// ```
pub fn new(index: usize, value: T) -> Self {
Self { index, value }
}
/// Converts from `&ListOption<T>` to `ListOption<&T>`.
pub fn as_ref(&self) -> ListOption<&T> {
ListOption::new(self.index, &self.value)
}
#[allow(unused)]
pub(crate) fn from_list(vals: Vec<T>) -> Vec<ListOption<T>> {
vals.into_iter()
.enumerate()
.map(|(index, value)| Self { index, value })
.collect()
}
#[allow(unused)]
pub(crate) fn from_enumerated_list(vals: Vec<(usize, T)>) -> Vec<ListOption<T>> {
vals.into_iter()
.map(|(index, value)| Self { index, value })
.collect()
}
}
impl<T> fmt::Display for ListOption<T>
where
T: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}