user_query 0.1.0

Query the user for input on stdin.
Documentation
use std::{io::BufRead, fmt::Display, error::Error};

/// Represents the answer to a query posted to the user.
pub struct BinaryQuery<T> 
where T: Display
{
    /// For a binary query we have exactly to choices. A positive and a negative one.
    /// These choices do not need to be 'yes' or 'no', but can be generic.
    /// The only thing we need to be able to do is to communicate them.
    pub positive_choice: T,
    pub negative_choice: T,
    pub answer: String,
}

impl<T> BinaryQuery<T> 
where T: Display
{
    /// Constructor for a query.
   pub fn new<I: BufRead>(message: &str, positive_choice: T, negative_choice: T, input_handle: &mut I)  -> Result<Self, Box<dyn Error>> {
       // First we will print a (nice) message to the user to inform him that he is being queried..
       println!("{}", message); 
       // Then we will present him his two choices.
       println!("Positive answer: {}", positive_choice);
       println!("Negative answer: {}", negative_choice);
       let mut user_input = String::default();
       input_handle.read_line(&mut user_input)?;
        Ok(Self { positive_choice, negative_choice, answer: user_input.trim().to_string() })
   }
}