summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWiktor Kwapisiewicz <wiktor@metacode.biz>2021-03-25 08:23:31 +0100
committerWiktor Kwapisiewicz <wiktor@metacode.biz>2021-03-31 11:14:03 +0200
commit4516d5b4c39a765583f9f2fe4bad54bbe70de0c9 (patch)
tree817b0f730ef5d1461e0ee3534176821fd751b698
parent4ccb927639dfbdc47f1cd229b5400282e6c77421 (diff)
ipc: Simplify error handling in examples.
- Rewrite any `expect`s and `unwrap`s into the `?` operator. - This loses the error detail. It could be recovered using `context` but for simplicity sake of the example the `?` operator suffices. - Additionally the `crate::` prefix has been removed as it is not necessary.
-rw-r--r--ipc/examples/gpg-agent-decrypt.rs38
-rw-r--r--ipc/examples/gpg-agent-sign.rs37
2 files changed, 36 insertions, 39 deletions
diff --git a/ipc/examples/gpg-agent-decrypt.rs b/ipc/examples/gpg-agent-decrypt.rs
index 8b5ead32..98b0f721 100644
--- a/ipc/examples/gpg-agent-decrypt.rs
+++ b/ipc/examples/gpg-agent-decrypt.rs
@@ -7,11 +7,11 @@ use clap;
use sequoia_openpgp as openpgp;
use sequoia_ipc as ipc;
-use crate::openpgp::cert::prelude::*;
-use crate::openpgp::crypto::SessionKey;
-use crate::openpgp::types::SymmetricAlgorithm;
-use crate::openpgp::packet::key;
-use crate::openpgp::parse::{
+use openpgp::cert::prelude::*;
+use openpgp::crypto::SessionKey;
+use openpgp::types::SymmetricAlgorithm;
+use openpgp::packet::key;
+use openpgp::parse::{
Parse,
stream::{
DecryptionHelper,
@@ -23,11 +23,11 @@ use crate::openpgp::parse::{
MessageLayer,
},
};
-use crate::openpgp::policy::Policy;
-use crate::openpgp::policy::StandardPolicy as P;
-use crate::ipc::gnupg::{Context, KeyPair};
+use openpgp::policy::Policy;
+use openpgp::policy::StandardPolicy as P;
+use ipc::gnupg::{Context, KeyPair};
-fn main() {
+fn main() -> openpgp::Result<()> {
let p = &P::new();
let matches = clap::App::new("gpg-agent-decrypt")
@@ -43,25 +43,25 @@ fn main() {
.get_matches();
let ctx = if let Some(homedir) = matches.value_of("homedir") {
- Context::with_homedir(homedir).unwrap()
+ Context::with_homedir(homedir)?
} else {
- Context::new().unwrap()
+ Context::new()?
};
// Read the Certs from the given files.
let certs =
- matches.values_of("cert").expect("required").map(|f| {
- openpgp::Cert::from_file(f)
- .expect("Failed to read key")
- }).collect();
+ matches.values_of("cert").expect("required").map(
+ openpgp::Cert::from_file
+ ).collect::<Result<_, _>>()?;
// Now, create a decryptor with a helper using the given Certs.
- let mut decryptor = DecryptorBuilder::from_reader(io::stdin()).unwrap()
- .with_policy(p, None, Helper::new(&ctx, p, certs)).unwrap();
+ let mut decryptor = DecryptorBuilder::from_reader(io::stdin())?
+ .with_policy(p, None, Helper::new(&ctx, p, certs))?;
// Finally, stream the decrypted data to stdout.
- io::copy(&mut decryptor, &mut io::stdout())
- .expect("Decryption failed");
+ io::copy(&mut decryptor, &mut io::stdout())?;
+
+ Ok(())
}
/// This helper provides secrets for the decryption, fetches public
diff --git a/ipc/examples/gpg-agent-sign.rs b/ipc/examples/gpg-agent-sign.rs
index 1b474ca5..f77e8b6f 100644
--- a/ipc/examples/gpg-agent-sign.rs
+++ b/ipc/examples/gpg-agent-sign.rs
@@ -6,12 +6,12 @@ use clap;
use sequoia_openpgp as openpgp;
use sequoia_ipc as ipc;
-use crate::openpgp::parse::Parse;
-use crate::openpgp::serialize::stream::{Armorer, Message, LiteralWriter, Signer};
-use crate::openpgp::policy::StandardPolicy as P;
-use crate::ipc::gnupg::{Context, KeyPair};
+use openpgp::parse::Parse;
+use openpgp::serialize::stream::{Armorer, Message, LiteralWriter, Signer};
+use openpgp::policy::StandardPolicy as P;
+use ipc::gnupg::{Context, KeyPair};
-fn main() {
+fn main() -> openpgp::Result<()> {
let p = &P::new();
let matches = clap::App::new("gpg-agent-sign")
@@ -27,17 +27,16 @@ fn main() {
.get_matches();
let ctx = if let Some(homedir) = matches.value_of("homedir") {
- Context::with_homedir(homedir).unwrap()
+ Context::with_homedir(homedir)?
} else {
- Context::new().unwrap()
+ Context::new()?
};
// Read the Certs from the given files.
let certs =
- matches.values_of("cert").expect("required").map(|f| {
- openpgp::Cert::from_file(f)
- .expect("Failed to read key")
- }).collect::<Vec<_>>();
+ matches.values_of("cert").expect("required").map(
+ openpgp::Cert::from_file
+ ).collect::<Result<Vec<_>, _>>()?;
// Construct a KeyPair for every signing-capable (sub)key.
let mut signers = certs.iter().flat_map(|cert| {
@@ -54,8 +53,7 @@ fn main() {
let message = Message::new(io::stdout());
// We want the output to be ASCII armored.
- let message = Armorer::new(message).build()
- .expect("Failed to create the armorer.");
+ let message = Armorer::new(message).build()?;
// Now, create a signer that emits the signature(s).
let mut signer =
@@ -63,18 +61,17 @@ fn main() {
for s in signers {
signer = signer.add_signer(s);
}
- let signer = signer.build().expect("Failed to create signer");
+ let signer = signer.build()?;
// Then, create a literal writer to wrap the data in a literal
// message packet.
- let mut literal = LiteralWriter::new(signer).build()
- .expect("Failed to create literal writer");
+ let mut literal = LiteralWriter::new(signer).build()?;
// Copy all the data.
- io::copy(&mut io::stdin(), &mut literal)
- .expect("Failed to sign data");
+ io::copy(&mut io::stdin(), &mut literal)?;
// Finally, teardown the stack to ensure all the data is written.
- literal.finalize()
- .expect("Failed to write data");
+ literal.finalize()?;
+
+ Ok(())
}