promptly
Types
The error types returned by promptly
.
pub type Error(a) {
InputError
ValidationFailed(a)
}
Constructors
-
InputError
If the underlying input mechanism fails,
InputError
will be returned. -
ValidationFailed(a)
If any of your custom validation checks fail,
ValidationFailed
will be returned with your custom error.
Values
pub fn as_float(
prompt: Prompt(String, Error(a)),
to_error: fn(String) -> a,
) -> Prompt(Float, Error(a))
Same as as_int()
, but for float values.
pub fn as_int(
prompt: Prompt(String, Error(a)),
to_error: fn(String) -> a,
) -> Prompt(Int, Error(a))
A convenience function for attempting to convert text input into an integer.
Use with_validator()
for precise control over input manipulation and to
verify data. Accepts a function whose input receives the value the user
provided, for crafting your own errors.
pub fn prompt(
prompt: Prompt(a, b),
formatter: fn(Option(b)) -> String,
) -> a
Starts a prompt loop. Accepts a custom formatter function to define how your
prompt and errors should be printed. The formatter’s input is an
Option(String)
, and is Some
when an error was encountered. Raises errors
defined in your pipeline and continuously prompts the user until correct
data is provided.
pub fn prompt_once(
prompt: Prompt(a, b),
text: String,
) -> Result(a, b)
prompt_once()
will only prompt the user once and will return a result
rather than a direct value. You can use this function when you only want to
give the user one chance to respond or to manage your own side effects via
custom prompt loop logic.
pub fn quote_text(text: String) -> String
A convenience function for returning text
as "text"
.
pub fn with_default(
prompt: Prompt(String, Error(a)),
default: String,
) -> Prompt(String, Error(a))
A convenience function that allows for providing a default value when the
input is an empty string: ""
. Use with_validator()
for more control over
input manipulation and to verify data.
pub fn with_validator(
prompt: Prompt(a, Error(b)),
validator: fn(a) -> Result(c, b),
) -> Prompt(c, Error(b))
Allows you to control which data is valid or not, as well as map input data
to any value. The validator function should return a result with valid data
in Ok
variants and errors in Error
variants.