llcio 0.1.0

LLCs Input Optimations, simpler input of various datatypes
Documentation
use std::io::{self, Write};

/// A basic function to read a line of input from the user
/// 
/// # Arguments
/// 
/// * `prompt` - The prompt to display to the user
/// 
/// # Returns
/// 
/// * `Result<String, io::Error>` - The string entered by the user or an error

pub fn input(prompt: &str) -> Result<String, io::Error> {
    print!("{}", prompt);
    io::stdout().flush().unwrap();

    let mut input = String::new();
    match io::stdin().read_line(&mut input) {
        Ok(_) => Ok(input.trim().to_string()),
        Err(e) => Err(e)
    }
}

/// A function to get integer input from the user with a prompt
/// 
/// # Arguments
/// 
/// * `prompt` - The prompt to display to the user
/// 
/// # Returns
/// 
/// * `i32` - The integer entered by the user

pub fn intput(prompt: &str) -> i32 {
    loop {
        match input(prompt) {
            Ok(input_str) => {
                match input_str.parse::<i32>() {
                    Ok(num) => return num,
                    Err(_) => {
                        println!("Please enter a valid integer.");
                        continue;
                    }
                }
            }
            Err(_) => {
                println!("Failed to read input. Please try again.");
                continue;
            }
        }
    }
}

/// A function to get string input from the user with a prompt
/// 
/// # Arguments
/// 
/// * `prompt` - The prompt to display to the user
/// 
/// # Returns
/// 
/// * `String` - The string entered by the user
/// 

pub fn strinput(prompt: &str) -> String {
    input(prompt).unwrap_or_else(|_| {
        println!("Failed to read input. Returning empty string.");
        String::new()
    })
}

/// A function to get boolean input from the user with a prompt
/// 
/// Accepts "yes"/"no", "y"/"n", "true"/"false", or "1"/"0" as valid inputs
/// 
/// # Arguments
/// 
/// * `prompt` - The prompt to display to the user
/// 
/// # Returns
/// 
/// * `bool` - The boolean value entered by the user
/// 
/// # Examples
///
pub fn boolput(prompt: &str) -> bool {
    loop {
        match input(prompt) {
            Ok(input_str) => {
                let input_lower = input_str.to_lowercase();
                match input_lower.as_str() {
                    "yes" | "y" | "true" | "1" => return true,
                    "no" | "n" | "false" | "0" => return false,
                    _ => {
                        println!("Please enter a valid boolean value (yes/no, y/n, true/false, 1/0).");
                        continue;
                    }
                }
            }
            Err(_) => {
                println!("Failed to read input. Please try again.");
                continue;
            }
        }
    }
}

/// A function to get floating-point input from the user with a prompt
/// 
/// # Arguments
/// 
/// * `prompt` - The prompt to display to the user
/// 
/// # Returns
/// 
/// * `f64` - The floating-point number entered by the user
/// 

pub fn floatput(prompt: &str) -> f64 {
    loop {
        match input(prompt) {
            Ok(input_str) => {
                match input_str.parse::<f64>() {
                    Ok(num) => return num,
                    Err(_) => {
                        println!("Please enter a valid floating-point number.");
                        continue;
                    }
                }
            }
            Err(_) => {
                println!("Failed to read input. Please try again.");
                continue;
            }
        }
    }
}