[][src]Struct cowmug::Prompt

pub struct Prompt { /* fields omitted */ }

Struct that take control of stdout and ask questions

Implementations

impl Prompt[src]

pub fn new() -> Self[src]

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

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!! =/ ");
    }
}

Auto Trait Implementations

impl RefUnwindSafe for Prompt

impl Send for Prompt

impl Sync for Prompt

impl Unpin for Prompt

impl UnwindSafe for Prompt

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.