Crate smart_read

source ·
Expand description

§Smart-Read

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, 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<O, F>

§List Constraints

These allow you to specify which inputs are allowed. Example: read!(&["a", "b", "c"])

If the choices are wrapped in EnumerateInput, it also returns the index of the chosen option

Special syntax: read!(= 1, 2, 3)

Implemented types:

impl<T: Display + Clone + PartialEq> TryRead for &[T]
impl<T: Display + Clone + PartialEq> TryRead for &[T; _]
impl<T: Display + Clone + PartialEq> TryRead for Vec<T>
impl<T: Display + Clone + PartialEq> TryRead for VecDeque<T>
impl<T: Display + Clone + PartialEq> TryRead for LinkedList<T>
// NOTE: If the options are filtered before being fed into smart-read, you should probably use OptionWithData<T>
impl<T: Display + Clone + PartialEq> TryRead for EnumerateInput<&[T]>
impl<T: Display + Clone + PartialEq> TryRead for EnumerateInput<&[T; _]>
impl<T: Display + Clone + PartialEq> TryRead for EnumerateInput<Vec<T>>
impl<T: Display + Clone + PartialEq> TryRead for EnumerateInput<VecDeque<T>>
impl<T: Display + Clone + PartialEq> TryRead for EnumerateInput<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,


§Extra Functionality

In addition to the type of input, data can be added at the start of read!() / prompt!(). In order, these additions are:


§Prompt Message

prompt_value; (only available with prompt!())


§Default Value

[default_value]


§Example:   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



Modules§

  • Contains implementations for (), UsizeInput, NonEmptyInput, etc
  • Contains implementations for Vec<T>, read!(= a, b, c), etc
  • Contains implementations for SimpleValidate and TransformValidate
  • 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§

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