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:


§Basics

impl TryRead for ()
impl TryRead for NonEmptyInput
impl TryRead for NonWhitespaceInput
impl TryRead for Fn(&str) -> Result<(), String>
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

§List Constraints

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

If the choices are wrapped in EnumerateInput, it 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>
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.), or read!(10..), etc

Implemented types:

impl<T: Display + FromStr + PartialOrd<T>> TryRead for Range<T>
impl<T: Display + FromStr + PartialOrd<T>> TryRead for RangeInclusive<T>
impl<T: Display + FromStr + PartialOrd<T>> TryRead for RangeTo<T>
impl<T: Display + FromStr + PartialOrd<T>> TryRead for RangeFrom<T>
impl<T: Display + FromStr + PartialOrd<T>> TryRead for RangeToInclusive<T>


§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!())


§Custom Input

input >> (must implement crate’s IntoInput)


§Default Value

[default_value]


§Example:   prompt!("Enter a color: "; prev_user_input >> ["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
  • 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§

  • Specifies the source of user input
  • This contains all possible information about the read / prompt

Traits§

  • Allows a type to be used as input. Example:
  • This is what powers the whole crate. Any struct that implements this can be used with the macros

Functions§

Type Aliases§

  • Just Result<T, Box<dyn Error>>, mostly for internal use