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
65
66
67
68
//! Utilities used to wrap user selections in [Select](crate::Select) and
//! [MultiSelect](crate::MultiSelect) prompts.

use std::fmt;

/// Represents a selection made by the user when prompted to select one or several
/// options among those presented.
///
/// It is essentially the return type of the [Select](crate::Select) and [MultiSelect](crate::MultiSelect)
/// prompts.
#[derive(Clone, Debug, PartialEq)]
pub struct OptionAnswer {
    /// Index of the selected option relative to the original (full) list passed to the prompt.
    pub index: usize,

    /// String value of the selected option.
    pub value: String,
}

impl OptionAnswer {
    /// Constructor for OptionAnswer.
    ///
    /// # Arguments
    ///
    /// * `index` - Index of the option.
    /// * `value` - String value of the option
    ///
    /// # Examples
    ///
    /// ```
    /// use inquire::option_answer::OptionAnswer;
    ///
    /// let answer = OptionAnswer::new(0, "a");
    /// ```
    pub fn new(index: usize, value: &str) -> Self {
        Self {
            index,
            value: value.to_string(),
        }
    }

    #[allow(unused)]
    pub(in crate) fn from_str_list(vals: &[&str]) -> Vec<OptionAnswer> {
        vals.iter()
            .enumerate()
            .map(|(index, value)| Self {
                index,
                value: value.to_string(),
            })
            .collect()
    }

    #[allow(unused)]
    pub(in crate) fn from_idx_str_list(vals: &[(usize, &str)]) -> Vec<OptionAnswer> {
        vals.iter()
            .map(|(index, value)| Self {
                index: *index,
                value: value.to_string(),
            })
            .collect()
    }
}

impl fmt::Display for OptionAnswer {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.value)
    }
}