Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error in history api #762

Open
Kl4rry opened this issue Feb 8, 2024 · 4 comments
Open

Custom error in history api #762

Kl4rry opened this issue Feb 8, 2024 · 4 comments
Labels

Comments

@Kl4rry
Copy link

Kl4rry commented Feb 8, 2024

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
Copy link
Collaborator

gwenn commented Feb 8, 2024

Indeed, I had to introduce this variant for the SQLite based history:

rustyline/src/error.rs

Lines 33 to 34 in 4a7dfe9

#[cfg(feature = "with-sqlite-history")]
SQLiteError(rusqlite::Error),

@gwenn gwenn added the bug label Feb 8, 2024
@Kl4rry
Copy link
Author

Kl4rry commented Feb 8, 2024

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
Copy link
Collaborator

gwenn commented Feb 24, 2024

Just for reference:

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

@Kl4rry
Copy link
Author

Kl4rry commented Feb 27, 2024

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants