pub enum FormError {
UserAborted,
Timeout,
Validation(String),
Io(String),
}Expand description
Errors that can occur during form execution.
This enum represents all possible error conditions when running an interactive form with huh.
§Error Handling
Forms can fail for several reasons, but many are recoverable or expected user actions (like cancellation):
use huh::{Form, FormError, Result};
fn get_user_input() -> Result<String> {
let mut name = String::new();
Form::new(fields)
.run()?;
Ok(name)
}§Recovery Strategies
| Error Variant | Recovery Strategy |
|---|---|
UserAborted | Normal exit, not an error condition |
Timeout | Retry with longer timeout or prompt user |
Validation | Show error message, allow retry |
Io | Check terminal, fall back to non-interactive |
§Example: Handling User Abort
User abort (Ctrl+C) is a normal exit path, not an error:
match form.run() {
Ok(()) => println!("Form completed!"),
Err(FormError::UserAborted) => {
println!("Cancelled by user");
return Ok(()); // Not an error condition
}
Err(e) => return Err(e.into()),
}§Note on Clone and PartialEq
This error type implements Clone and PartialEq to support
testing and comparison. As a result, the Io variant stores
a String message rather than the underlying io::Error.
Variants§
UserAborted
User aborted the form with Ctrl+C or Escape.
This is not an error condition but a normal exit path. Users may cancel forms for valid reasons, and applications should handle this gracefully.
§Example
match form.run() {
Err(FormError::UserAborted) => {
println!("No changes made");
return Ok(());
}
// ...
}Timeout
Form execution timed out.
Occurs when a form has a timeout configured and the user does not complete it in time.
§Recovery
- Increase the timeout duration
- Prompt user to try again
- Use a default value
Validation(String)
Custom validation error.
Occurs when a field’s validation function returns an error. The contained string describes what validation failed.
§Recovery
Validation errors are recoverable - show the error message to the user and allow them to correct their input.
§Example
let input = Input::new()
.title("Email")
.validate(|s| {
if s.contains('@') {
Ok(())
} else {
Err(FormError::Validation("must contain @".into()))
}
});Io(String)
IO error during form operations.
Occurs during terminal I/O operations, particularly in accessible mode where stdin/stdout are used directly.
Note: Stores the error message as a String rather than
io::Error to maintain Clone and PartialEq derives.
§Recovery
- Check if the terminal is available
- Fall back to non-interactive input
- Log the error and exit gracefully
Implementations§
Source§impl FormError
impl FormError
Sourcepub fn validation(message: impl Into<String>) -> Self
pub fn validation(message: impl Into<String>) -> Self
Creates a validation error with the given message.
Sourcepub fn is_user_abort(&self) -> bool
pub fn is_user_abort(&self) -> bool
Returns true if this is a user-initiated abort.
Sourcepub fn is_timeout(&self) -> bool
pub fn is_timeout(&self) -> bool
Returns true if this is a timeout error.
Sourcepub fn is_recoverable(&self) -> bool
pub fn is_recoverable(&self) -> bool
Returns true if this error is recoverable (validation errors).
Trait Implementations§
Source§impl Error for FormError
impl Error for FormError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
impl Eq for FormError
impl StructuralPartialEq for FormError
Auto Trait Implementations§
impl Freeze for FormError
impl RefUnwindSafe for FormError
impl Send for FormError
impl Sync for FormError
impl Unpin for FormError
impl UnwindSafe for FormError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.