Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/input_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Validator for InputValidator {
let result = if !input.starts_with("SELECT") {
Invalid(Some(" --< Expect: SELECT stmt".to_owned()))
} else if !input.ends_with(';') {
Incomplete
Incomplete(None)
} else {
Valid(None)
};
Expand Down
6 changes: 5 additions & 1 deletion src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
let result = validator.validate(&mut ValidationContext::new(self))?;
let corrected = self.changes.borrow_mut().end();
match result {
ValidationResult::Incomplete => {}
ValidationResult::Incomplete(ref msg) => {
if msg.is_some() {
self.refresh_line_with_msg(msg.as_deref())?;
}
}
ValidationResult::Valid(ref msg) => {
// Accept the line regardless of where the cursor is.
if corrected || self.has_hint() || msg.is_some() {
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,14 +656,17 @@ fn readline_direct(
validate::ValidationResult::Invalid(Some(msg)) => {
writer.write_all(msg.as_bytes())?;
}
validate::ValidationResult::Incomplete => {
validate::ValidationResult::Incomplete(msg) => {
// Add newline and keep on taking input
if trailing_r {
input.push('\r');
}
if trailing_n {
input.push('\n');
}
if let Some(msg) = msg {
writer.write_all(msg.as_bytes())?;
}
}
_ => {}
}
Expand Down
8 changes: 5 additions & 3 deletions src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::Result;
#[non_exhaustive]
pub enum ValidationResult {
/// Incomplete input
Incomplete,
Incomplete(Option<String>),
/// Validation fails with an optional error message. User must fix the
/// input.
Invalid(Option<String>),
Expand All @@ -23,7 +23,9 @@ impl ValidationResult {
pub(crate) fn has_message(&self) -> bool {
matches!(
self,
ValidationResult::Valid(Some(_)) | ValidationResult::Invalid(Some(_))
ValidationResult::Valid(Some(_))
| ValidationResult::Invalid(Some(_))
| ValidationResult::Incomplete(Some(_))
)
}
}
Expand Down Expand Up @@ -142,6 +144,6 @@ fn validate_brackets(input: &str) -> ValidationResult {
if stack.is_empty() {
ValidationResult::Valid(None)
} else {
ValidationResult::Incomplete
ValidationResult::Incomplete(None)
}
}