Struct Prompt

Source
pub struct Prompt { /* private fields */ }
Expand description

Struct that take control of stdout and ask questions

Implementations§

Source§

impl Prompt

Source

pub fn new() -> Self

Examples found in repository?
examples/sample.rs (line 21)
4fn main() {
5    let mut list_questions: Vec<Question> = Vec::new();
6    let mut pizza_size = Question::new(String::from("Choose the size of your pizza: "));
7    pizza_size.add_choice(String::from("Large"));
8    pizza_size.add_choice(String::from("Medium"));
9    pizza_size.add_choice(String::from("Small"));
10    list_questions.push(pizza_size);
11
12    let mut pizza_flavor = Question::new(String::from("Choose the flavor:"));
13    let pizza_flavor_choices: Vec<String> = vec![
14        String::from("Pepperoni"),
15        String::from("4 cheeses"),
16        String::from("Chicken"),
17    ];
18    pizza_flavor.add_choices(pizza_flavor_choices);
19    list_questions.push(pizza_flavor);
20
21    let mut prompt = Prompt::new();
22    prompt.exec(&mut list_questions).unwrap();
23
24    let answer_size = list_questions.get(0).unwrap().string_answer().unwrap();
25    let answer_flavor = list_questions.get(1).unwrap().string_answer().unwrap();
26    let mut choices_is_correct = Question::new(format!(
27        "You choose pizza {} with {}, you confirm?",
28        answer_size, answer_flavor
29    ));
30    choices_is_correct.add_choice(String::from("yes"));
31    choices_is_correct.add_choice(String::from("no"));
32    list_questions.clear();
33    list_questions.push(choices_is_correct);
34    prompt.exec(&mut list_questions).unwrap();
35
36    if let Some(is_correct) = list_questions.get(0).unwrap().string_answer() {
37        if is_correct.as_str() == "yes" {
38            println!("Great! See ya");
39        } else {
40            println!("Thats is terrible!! =/ ");
41        }
42    }
43}
Source

pub fn exec(&mut self, questions: &mut Vec<Question>) -> Result<(), Error>

Given a vector of Question, exec will iter through it and ask all questions and store the answer on it’s on question

§Examples
let mut list_questions: Vec<Question> = Vec::new();
let mut pizza_size = Question::new(String::from("Choose the size of your pizza: "));
pizza_size.add_choice(String::from("Large"));
pizza_size.add_choice(String::from("Medium"));
pizza_size.add_choice(String::from("Small"));
list_questions.push(pizza_size);

let mut pizza_flavor = Question::new(String::from("Choose pizza:"));
let pizza_flavor_choices: Vec<String> = vec![
    String::from("peperoni"),
    String::from("4 cheese"),
    String::from("chicken"),
];
pizza_flavor.add_choices(pizza_flavor_choices);
list_questions.push(pizza_flavor);

let mut prompt = Prompt::new();
prompt.exec(&mut list_questions).unwrap();

let answer_size = list_questions.get(0).unwrap().string_answer().unwrap();
let answer_flavor = list_questions.get(1).unwrap().string_answer().unwrap();
let mut choices_is_correct = Question::new(format!(
    "You choose pizza {} with {}, you confirm?",
    answer_size, answer_flavor
));
choices_is_correct.add_choice(String::from("yes"));
choices_is_correct.add_choice(String::from("no"));
list_questions.clear();
list_questions.push(choices_is_correct);
prompt.exec(&mut list_questions).unwrap();

if let Some(is_correct) = list_questions.get(0).unwrap().string_answer() {
    if is_correct.as_str() == "yes" {
        println!("Great! See ya");
    } else {
        println!("Thats is terrible!! =/ ");
    }
}
Examples found in repository?
examples/sample.rs (line 22)
4fn main() {
5    let mut list_questions: Vec<Question> = Vec::new();
6    let mut pizza_size = Question::new(String::from("Choose the size of your pizza: "));
7    pizza_size.add_choice(String::from("Large"));
8    pizza_size.add_choice(String::from("Medium"));
9    pizza_size.add_choice(String::from("Small"));
10    list_questions.push(pizza_size);
11
12    let mut pizza_flavor = Question::new(String::from("Choose the flavor:"));
13    let pizza_flavor_choices: Vec<String> = vec![
14        String::from("Pepperoni"),
15        String::from("4 cheeses"),
16        String::from("Chicken"),
17    ];
18    pizza_flavor.add_choices(pizza_flavor_choices);
19    list_questions.push(pizza_flavor);
20
21    let mut prompt = Prompt::new();
22    prompt.exec(&mut list_questions).unwrap();
23
24    let answer_size = list_questions.get(0).unwrap().string_answer().unwrap();
25    let answer_flavor = list_questions.get(1).unwrap().string_answer().unwrap();
26    let mut choices_is_correct = Question::new(format!(
27        "You choose pizza {} with {}, you confirm?",
28        answer_size, answer_flavor
29    ));
30    choices_is_correct.add_choice(String::from("yes"));
31    choices_is_correct.add_choice(String::from("no"));
32    list_questions.clear();
33    list_questions.push(choices_is_correct);
34    prompt.exec(&mut list_questions).unwrap();
35
36    if let Some(is_correct) = list_questions.get(0).unwrap().string_answer() {
37        if is_correct.as_str() == "yes" {
38            println!("Great! See ya");
39        } else {
40            println!("Thats is terrible!! =/ ");
41        }
42    }
43}

Auto Trait Implementations§

§

impl Freeze for Prompt

§

impl RefUnwindSafe for Prompt

§

impl Send for Prompt

§

impl Sync for Prompt

§

impl Unpin for Prompt

§

impl UnwindSafe for Prompt

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.