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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
//! This crate was inspired by Python's input function. It allows easy reading of data from the terminal.
//!
//! A simple example of its use is:
//! ```
//! extern crate reader;
//! use reader::input;
//!
//! let name = input("Enter your name: ");
//! println!("Your name is: {}", name);
//! ```

use std::io::Write;
use std::num::{ParseIntError, ParseFloatError};

/// Read a String.
///
/// This function works in a similar way to the python "input" function,
/// that receives as an argument a sentence that will be displayed on the screen
/// and returns a String.
///
/// # Examples
///
/// Basic usage
/// ```
/// extern crate reader;
/// use reader::input;
///
/// let name = input("Enter your name: ");
/// println!("Your name is: {}", name);
/// ```
pub fn input(text: &'static str) -> String {
	let mut string = String::new();

	print!("{}", text);
	std::io::stdout().flush().unwrap();

	std::io::stdin()
		.read_line(&mut string)
		.expect("Failed to read line"); 

	string.truncate(string.len() - 1); // Removes the '\n', important to avoid line skipping.
	
	return string;
}

/// Convert a String to integer (i64)
///
/// # Examples
///	
/// Basic usage
/// ```
/// extern crate reader;
/// use reader::{input, int};
///
/// Reading a integer (i64)
///
/// let age = int(input("Enter your age: ")).unwrap();
/// println!("Your age is: {}", age);
///
/// Reading a integer (i32)
///
/// let age = int(input("Enter your age: ")).unwrap() as i32;
/// println!("Your age is: {}", age);
///
/// Reading a integer (i16)
///
/// let age = int(input("Enter your age: ")).unwrap() as i16;
/// println!("Your age is: {}", age);
///
/// Reading a integer (i8)
///
/// let age = int(input("Enter your age: ")).unwrap() as i8;
/// println!("Your age is: {}", age);
/// ```
pub fn int(string: String) -> Result<i64, ParseIntError> {
	return match string.trim().parse::<i64>() {
		Ok(integer) => Ok(integer),
		Err(e) => Err(e)
	}
} 	

/// Convert a String to float (f64)
///
/// # Examples
///
/// Basic usage
/// ```
/// extern crate reader;
/// use reader::{input, float};
///
/// Reading a float (f64) 
///
/// let salary = float(input("Enter your salary: ")).unwrap();
/// println!("Your salary is: {}", salary);
///
/// Reading a float (f32)
///
/// let salary = float(input("Enter your salary: ")).unwrap() as f32;
/// println!("Your salary is: {}", salary);
/// ```
pub fn float(string: String) -> Result<f64, ParseFloatError> {
	return  match string.trim().parse::<f64>() {
		Ok(float) => Ok(float),
		Err(e) => Err(e)
	}
}