use rustyline::validate::{ValidationContext, ValidationResult, Validator};
use rustyline::{Completer, Editor, Helper, Highlighter, Hinter, Result};
#[derive(Completer, Default, Helper, Highlighter, Hinter)]
struct InputValidator {}
impl Validator for InputValidator {
fn validate(&self, ctx: &mut ValidationContext) -> Result<ValidationResult> {
use ValidationResult::{Incomplete, Invalid, Valid};
let input = ctx.input();
let result = if !input.starts_with("SELECT") {
Invalid(Some(" --< Expect: SELECT stmt".to_owned()))
} else if !input.ends_with(';') {
Incomplete
} else {
Valid(None)
};
Ok(result)
}
}
fn main() -> Result<()> {
let mut rl = Editor::<InputValidator, _>::default()?;
let input = rl.readline("> ")?;
println!("Input: {input}");
Ok(())
}