Crates.io | cinputs |
lib.rs | cinputs |
version | 0.2.5 |
source | src |
created_at | 2024-10-23 08:35:52.415344 |
updated_at | 2024-10-23 10:03:02.711531 |
description | Crate that simplifies the constraining of inputs |
homepage | |
repository | https://github.com/Larmbs/constrained-inputs |
max_upload_size | |
id | 1419826 |
size | 15,014 |
CInputs is a Rust library designed for easy input parsing with built-in constraints. It provides functionality for reading user input and applying various constraints to ensure the validity of that input.
This is the successor to constrained_inputs crate
Here is a quick example of using input() and cinput().
use cinputs::prelude::*;
fn main() {
println!("What is your favorite integer?");
let favorite_integer: isize = input().unwrap();
println!("Great Choice! {}", favorite_integer);
println!("What is your age?");
let num_constraint = NumberConstraint {
min_value: 0.0,
max_value: 120.0,
};
match cinput::<u8, _>(num_constraint) {
Ok(age) => println!("You really don't look {}.", age),
Err(err) => match err.kind {
error::ErrorKind::ValidationError => println!("Thats not possible."),
_ => eprintln!("{}", err),
},
}
println!("What is the best name that contains a J?");
let string_constraint = StringConstraint {
exclude_char: Vec::new(),
include_char: vec!['J'],
max_len: usize::MAX,
min_len: usize::MIN,
};
match cinput::<String, _>(string_constraint) {
Ok(name) => print!("Great choice of name, {} is very nice!", name),
Err(err) => match err.kind {
error::ErrorKind::ValidationError => {
println!("How did you fail to think of a name containing J!?!?")
}
_ => eprintln!("{}", err),
},
}
}