Crate smart_read
source ·Expand description
Complex but easy ways to read user input
§Anything that implements the TryRead
trait can be used with smart-read’s macros, and many implementations are already given
§Types that implement TryRead (basically, a list of all default functionality):
§Basics
impl TryRead for ()
impl TryRead for NonEmptyInput
impl TryRead for NonWhitespaceInput
impl TryRead for BoolInput
impl TryRead for YesNoInput
impl TryRead for CharInput
impl TryRead for UsizeInput
impl TryRead for IsizeInput
impl TryRead for U8Input, U16Input, U32Input, U64Input, U128Input
impl TryRead for I8Input, I16Input, I32Input, I64Input, I128Input
impl TryRead for F32Input
impl TryRead for F64Input
§One-Time Logic
impl<F: Fn(&str) -> Result<(), String>> TryRead for SimpleValidate<F>
impl<F: Fn(String) -> Result<O, String>, O: Display> TryRead for TransformValidate<F, O>
§List Constraints
These allow you to specify which inputs are allowed. Example: read!(&["a", "b", "c"])
Special syntax: read!(= 1, 2, 3)
Implemented types:
impl<Data> TryRead for Vec<InputOption<Data>> // this is the main implementation, the 5 below this ultimately use this impl
impl<T: Display> TryRead for &[T]
impl<T: Display> TryRead for &[T; _]
impl<T: Display> TryRead for Vec<T>
impl<T: Display> TryRead for VecDeque<T>
impl<T: Display> TryRead for LinkedList<T>
§Range Constraints
These allow you to take a number within a specified range. Example: read!(1. .. 100.)
, read!(10..)
, etc
Implemented types:
impl<T> TryRead for Range<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display,
impl<T> TryRead for RangeInclusive<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display,
impl<T> TryRead for RangeTo<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display,
impl<T> TryRead for RangeFrom<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display,
impl<T> TryRead for RangeToInclusive<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display,
§Macro Syntax
There are three items that can be included in a macro call (all optional): the prompt message, the default value, and the input type.
The prompt message is simply an expression, followed by ;
if there’s more afterwards. This is required when using the prompt macro, and not available with the read macro.
Examples: prompt!("Enter any string: ")
, prompt!(messages[i]; YesNoInput)
The default value comes after the prompt message (if given), and must be enclosed in []
.
Examples: read!([1] 0..10)
, prompt!("Confirm action? "; [true] YesNoInput)
The input type is a value that determines how the input is read. You could give a range to read a number within a range, or a UsizeInput
to read an int, or whatever else implements TryRead
from this crate (fun fact, leaving this blank will use the impl for ()
).
Examples: read!()
, prompt!("Enter a color: "; ["red"] &["red", "green", "blue"])
, read!(ExampleStruct {arg: 42})
§Feature-Specific Syntax
Currently, only one feature has custom syntax, which is the implementation for slices. Instead of read!(&[item1, item2, ...])
, you can write: read!(= item1, item2, ...)
And of course, you can combine this with any other piece of syntax: prompt!("Enter a color: "; ["red"] = "red", "green", "blue")
If you have ideas for more functionality (including things that you’ve found to be useful for yourself), feel free to open an issue / pull request
Modules§
- Contains implementations for
()
,UsizeInput
,NonEmptyInput
, etc - Contains implementations for
Vec<T>
,read!(= a, b, c)
, etc - Contains implementations for
SimpleValidate
andTransformValidate
- Easy way to use existing functionality. If you want to extend functionality instead, you can do
use smart_read::*;
- Contains implementations for
Range<T>
,RangeFrom<T>
, etc
Macros§
- Same as read!(), but also prints a prompt
- Reads a line of text, a number, etc
- Same as prompt!(), but returns a result
- Same as read!(), but returns a result
Structs§
- Useful pre-made error
- Useful pre-made error
Traits§
- This is what powers the whole crate. Any struct that implements this can be used with the macros
Functions§
- Tiny utility function, clears the terminal output, but you should probably use the ClearScreen crate instead
- Utility function, mostly for internal use
Type Aliases§
- Just
Result<T, Box<dyn Error>>
, mostly for internal use