kkawakam / rustyline

Readline Implementation in Rust
https://crates.io/crates/rustyline/
MIT License
1.5k stars 173 forks source link

Custom error in history api #762

Open Kl4rry opened 5 months ago

Kl4rry commented 5 months ago

Currently the history trait requires the error return to be a readline error. That means that one cannot return other error types that are not included in:

pub enum ReadlineError {
    Io(Error),
    Eof,
    Interrupted,
    Errno(Error),
    WindowResized,
}

This can be fixed by either adding a Box<dyn std::error::Error> variant that can be used to return any std::error::Error or by adding a optional generic error type like this:

pub enum ReadlineError<E = std::convert::Infallible> {
    Io(Error),
    Eof,
    Interrupted,
    Errno(Error),
    WindowResized,
    UserError(E),
}
gwenn commented 5 months ago

Indeed, I had to introduce this variant for the SQLite based history: https://github.com/kkawakam/rustyline/blob/4a7dfe90864638c2c8e68523bddc8949586323f0/src/error.rs#L33-L34

Kl4rry commented 5 months ago

Another possible solution is to make History generic over the error type and having a trait bound that it must implement into for ReadlineError.

gwenn commented 4 months ago

Just for reference:

As far as I know, other rust libraries doesn't support custom History impl.

Kl4rry commented 4 months ago

Termwiz kinda supports every error type as you can just to .ok() on every error type. There is a work around that can be used in both reedline and rustyline and that is to put the error in a IO error like this std::io::Error::new(std::io::ErrorKind::InvalidData, Box::new(custom_error))