summaryrefslogtreecommitdiffstats
path: root/tool/src/commands/key.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-03-09 11:42:45 +0100
committerJustus Winter <justus@sequoia-pgp.org>2020-03-09 18:09:50 +0100
commit391a4b92c977cd64dfd131f3e29b0bc8d756d064 (patch)
treeb5b96ff935853cef9ee22e01890c248a791e724e /tool/src/commands/key.rs
parent58d662c6d37dd1b0dccd4d0ce30290b8ede408e9 (diff)
Switch from failure to anyhow.
- Use the anyhow crate instead of failure to implement the dynamic side of our error handling. anyhow::Error derefs to dyn std::error::Error, allowing better interoperability with other stdlib-based error handling libraries. - Fixes #444.
Diffstat (limited to 'tool/src/commands/key.rs')
-rw-r--r--tool/src/commands/key.rs32
1 files changed, 15 insertions, 17 deletions
diff --git a/tool/src/commands/key.rs b/tool/src/commands/key.rs
index bbde9ceb..688633a8 100644
--- a/tool/src/commands/key.rs
+++ b/tool/src/commands/key.rs
@@ -1,5 +1,4 @@
-use failure;
-use failure::Fail;
+use anyhow::Context as _;
use clap::ArgMatches;
use itertools::Itertools;
use std::time::{SystemTime, Duration};
@@ -66,7 +65,7 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> {
builder = builder.set_cipher_suite(CipherSuite::Cv25519);
}
Some(ref cs) => {
- return Err(format_err!("Unknown cipher suite '{}'", cs));
+ return Err(anyhow::anyhow!("Unknown cipher suite '{}'", cs));
}
None => panic!("argument has a default value"),
}
@@ -79,7 +78,7 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> {
(false, true) => { /* no signing subkey */ }
(true, true) => {
return Err(
- format_err!("Conflicting arguments --can-sign and --cannot-sign"));
+ anyhow::anyhow!("Conflicting arguments --can-sign and --cannot-sign"));
}
}
@@ -101,12 +100,12 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> {
(None, true) => { /* no encryption subkey */ }
(Some(_), true) => {
return Err(
- format_err!("Conflicting arguments --can-encrypt and \
+ anyhow::anyhow!("Conflicting arguments --can-encrypt and \
--cannot-encrypt"));
}
(Some(ref cap), false) => {
return Err(
- format_err!("Unknown encryption capability '{}'", cap));
+ anyhow::anyhow!("Unknown encryption capability '{}'", cap));
}
}
@@ -119,7 +118,7 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> {
if p0 == p1 {
builder = builder.set_password(Some(p0));
} else {
- return Err(failure::err_msg("Passwords do not match."));
+ return Err(anyhow::anyhow!("Passwords do not match."));
}
}
@@ -136,7 +135,7 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> {
("-".to_string(), rp.to_string()),
(Some("-"), None) =>
return Err(
- format_err!("Missing arguments: --rev-cert is mandatory \
+ anyhow::anyhow!("Missing arguments: --rev-cert is mandatory \
if --export is '-'.")),
(Some(ref kp), None) =>
(kp.to_string(), format!("{}.rev", kp)),
@@ -146,7 +145,7 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> {
(kp.to_string(), rp.to_string()),
_ =>
return Err(
- format_err!("Conflicting arguments --rev-cert and \
+ anyhow::anyhow!("Conflicting arguments --rev-cert and \
--export")),
};
@@ -178,7 +177,7 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> {
}
} else {
return Err(
- format_err!("Saving generated key to the store isn't implemented \
+ anyhow::anyhow!("Saving generated key to the store isn't implemented \
yet."));
}
@@ -205,19 +204,18 @@ fn parse_duration(expiry: &str) -> Result<Duration> {
let junk = expiry.collect::<String>();
if digits == "" {
- return Err(format_err!(
+ return Err(anyhow::anyhow!(
"--expiry: missing count \
(try: '2y' for 2 years)"));
}
let count = match digits.parse::<i32>() {
Ok(count) if count < 0 =>
- return Err(format_err!(
+ return Err(anyhow::anyhow!(
"--expiry: Expiration can't be in the past")),
Ok(count) => count as u64,
Err(err) =>
- return Err(err.context(
- "--expiry: count is out of range").into()),
+ return Err(err).context("--expiry: count is out of range"),
};
let factor = match suffix {
@@ -226,19 +224,19 @@ fn parse_duration(expiry: &str) -> Result<Duration> {
Some('w') | Some('W') => 7 * SECONDS_IN_DAY,
Some('d') | Some('D') => SECONDS_IN_DAY,
None =>
- return Err(format_err!(
+ return Err(anyhow::anyhow!(
"--expiry: missing suffix \
(try: '{}y', '{}m', '{}w' or '{}d' instead)",
digits, digits, digits, digits)),
Some(suffix) =>
- return Err(format_err!(
+ return Err(anyhow::anyhow!(
"--expiry: invalid suffix '{}' \
(try: '{}y', '{}m', '{}w' or '{}d' instead)",
suffix, digits, digits, digits, digits)),
};
if junk != "" {
- return Err(format_err!(
+ return Err(anyhow::anyhow!(
"--expiry: contains trailing junk ('{:?}') \
(try: '{}{}')",
junk, count, factor));