gpg-rs / gpgme

GPGme bindings for Rust
GNU Lesser General Public License v2.1
83 stars 13 forks source link

Fix with_passphrase_provider example #37

Closed smCloudInTheSky closed 3 years ago

smCloudInTheSky commented 3 years ago

Current example doesn't work and return : Error[E0599]: no method named with_passphrase_provider found for enum std::result::Result<gpgme::Context, gpgme::Error> in the current scope.

Propose a working fix according to what we can find in the test directory

johnschug commented 3 years ago

It works fine for me. How are you running the example? Are you using cargo test in the gpgme repository or did you copy the example somewhere else? If you copied the example, did you make sure you included the "?" at the end?

smCloudInTheSky commented 3 years ago

Hello, Sorry for not answering earlier. I did try with the ? at the end but got the following error : let mut ctx = Context::from_protocol(Protocol::OpenPgp)?; | | ^ cannot use the ? operator in a function that returns () So I looked into your test directory and found you used unwrap instead hence my proposition of modification.

Regards

johnschug commented 3 years ago

unwrap is not generally recommended for error handling (especially for this crate) as it will usually abort the process, but is usually fine for tests. To use the ? operator your function has to return a Result (or an Option) with a compatible error type. Documentation examples have an implicit main function that sort of looks like this:

fn main() -> Result<(), impl Debug> {
    // example code goes here
    Ok(())
}

For more information see the section on error propagation in the rust book along with the next chapter on panicking.

smCloudInTheSky commented 3 years ago

Ok my bad sorry for bothering you and thanks for the explanation.