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
use std::io::stdin;

const READ_ERROR: &'static str = "Error: unable to read user input";

/// Reads a u8 from stdin and returns a u8. Panics on fail.
pub fn read_u8() -> u8
{
    let mut input = String::new();
    stdin().read_line(&mut input).expect(READ_ERROR);
    
    input.trim().parse::<u8>().expect("Unable to parse input to a u8")

}

/// Reads a line of user input from stdin into a String and returns it. Panics on fail.
pub fn read_line() -> String
{
    let mut input = String::new();
    stdin().read_line(&mut input).expect(READ_ERROR);
    
    input
}

/// Prompts the user with the provided &str msg before calling read_line() to get a user input string
pub fn read_line_prompt(msg: &str) -> String
{
    println!("{}", msg);
    read_line()
}