ascii_cli/
verification.rs

1use std::io::IsTerminal;
2
3use colored::Colorize;
4
5use crate::cli;
6
7pub fn check_base(base: u8) {
8    if !(2..=36).contains(&base) {
9        println!(
10            "{}",
11            format!(
12                "Base can only be between 2 and 36! The given base was {}",
13                base
14            )
15            .red()
16            .bold()
17        );
18        std::process::exit(1);
19    }
20}
21
22pub fn under_128(number: u32) {
23    if number > 127 {
24        println!("{} is over the ASCII limit of 127!", number);
25        std::process::exit(1);
26    }
27}
28
29pub fn get_number(string: String, base: u8) -> u32 {
30    check_base(base);
31
32    if let Ok(num) = u32::from_str_radix(&string, base as u32) {
33        num
34    } else {
35        println!(
36            "{}",
37            format!("\"{}\" is not a valid number in base {}", string, base)
38                .red()
39                .bold()
40        );
41        std::process::exit(1);
42    }
43}
44
45pub fn get_input(input_name: &str) -> String {
46    let throw_err = || {
47        println!(
48            "{}",
49            format!("Please provide a value for \"{}\"", input_name)
50                .red()
51                .bold()
52        );
53        std::process::exit(1);
54    };
55
56    if std::io::stdin().is_terminal() {
57        throw_err();
58    }
59
60    let input = cli::get_std_in();
61    input.trim().to_string()
62}