How do I implement an external trait for an external struct?

Yep, worked. Thanks for reminding me that I didn't pay enough attention before. :smiley: I am getting a bit overloaded but definitely not stopping!

I am much more ahead now. Currently grappling with how to serialize a rustler::error::Error to a normal Rust Error.

Huh, the rustler::Error error type apparently doesn't implement the Error trait from std. Maybe it's for errors that are meant to be returned to erlang instead of to Rust?

It could be. I noticed several Rustler examples where people directly use the ? operator (which means it'll just transparently return Err(whatever)).

I am still not getting the whole "translate one error to another" thing correctly.

Here's a snippet from a sample project from Rustler's author:

fn io_error_to_term<'a>(env: Env<'a>, err: &IoError) -> Term<'a> {
    let error = match err.kind() {
        IoErrorKind::NotFound => atoms::enoent().encode(env),
        IoErrorKind::PermissionDenied => atoms::eacces().encode(env),
        IoErrorKind::BrokenPipe => atoms::epipe().encode(env),
        IoErrorKind::AlreadyExists => atoms::eexist().encode(env),
        _ => format!("{}", err).encode(env),
    };

    (atoms::error(), error).encode(env)
}

macro_rules! handle_io_error {
    ($env:expr, $e:expr) => {
        match $e {
            Ok(inner) => inner,
            Err(ref error) => return Ok(io_error_to_term($env, error)),
        }
    };
}

Earlier, you also alluded to writing my own function to map between those different errors.

Still looking how to do the translation.


EDIT: @alice I hit a new user posting limit. Here's what I tried to reply to your comment below:

I got a bit disoriented. In this particular case I have to pattern-match on all 4 enum variants of rustler::Error and just return Erlang string or atom depending.

My mistake, apologies.

What are you missing? The posted snippet seems rather complete for conversion from IO error to rustler term.

Thanks in part to intense back-and-forth session with @OvermindDL1 who, besides providing a PR, also provided a lot of extra explanations on top of what @alice did, the initial phase of the project is now a success:

Link to the file with the exact commit.

Thanks so much to the amazing community. :heart:

2 Likes

@OvermindDL1's PR that pointed me in the right direction: Get the rust code compiling by OvermindDL1 ยท Pull Request #4 ยท dimitarvp/xqlite ยท GitHub

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.