summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHeiko <heiko@schaefer.name>2021-05-16 16:30:05 +0200
committerHeiko <heiko@schaefer.name>2021-05-16 16:30:05 +0200
commitab09a69eea71ec88a0c0b80748b4e6f2447d64d4 (patch)
treec15c2491d8ee472b91996b29d646ad84fd145891
parent74d21c4c693091a9c02866f80d48b81510803bec (diff)
downloadopenpgp-ca-ab09a69eea71ec88a0c0b80748b4e6f2447d64d4.tar.gz
Use TryFrom/From traits instead of other methodnames.
-rw-r--r--openpgp-ca-restd/src/cert_info.rs51
-rw-r--r--openpgp-ca-restd/src/process_certs.rs9
-rw-r--r--openpgp-ca-restd/src/restd.rs3
3 files changed, 34 insertions, 29 deletions
diff --git a/openpgp-ca-restd/src/cert_info.rs b/openpgp-ca-restd/src/cert_info.rs
index 449d6ea..1224270 100644
--- a/openpgp-ca-restd/src/cert_info.rs
+++ b/openpgp-ca-restd/src/cert_info.rs
@@ -19,6 +19,7 @@ use sequoia_openpgp::packet::key;
use sequoia_openpgp::packet::Signature;
use sequoia_openpgp::policy::StandardPolicy;
use sequoia_openpgp::Cert;
+use std::convert::{TryFrom, TryInto};
use std::time::SystemTime;
const POLICY: &StandardPolicy = &StandardPolicy::new();
@@ -84,22 +85,26 @@ pub struct Revocation {
pub time: Option<DateTime<Utc>>,
}
-impl CertInfo {
- pub fn from_cert(cert: &Cert) -> Result<CertInfo, anyhow::Error> {
+impl TryFrom<&Cert> for CertInfo {
+ type Error = anyhow::Error;
+
+ fn try_from(cert: &Cert) -> Result<Self, Self::Error> {
let mut user_ids: Vec<UserId> = vec![];
for userid in cert.userids() {
- let uid = UserId::from_component_amalgamation(&userid)?;
-
- user_ids.push(uid)
+ user_ids.push((&userid).try_into()?)
}
- let primary = Key::from_key_amalgamation(&cert.primary_key().into());
+ let ka: ErasedKeyAmalgamation<_> = cert.primary_key().into();
+ let primary: Key = (&ka).into();
let subkeys = cert
.keys()
.subkeys()
- .map(|ka| Key::from_key_amalgamation(&ka.into()))
+ .map(|ka| {
+ let ka: ErasedKeyAmalgamation<_> = ka.into();
+ (&ka).into()
+ })
.collect();
let ci = CertInfo {
@@ -112,10 +117,14 @@ impl CertInfo {
}
}
-impl UserId {
- fn from_component_amalgamation(
+impl TryFrom<&ComponentAmalgamation<'_, sequoia_openpgp::packet::UserID>>
+ for UserId
+{
+ type Error = anyhow::Error;
+
+ fn try_from(
uid: &ComponentAmalgamation<sequoia_openpgp::packet::UserID>,
- ) -> Result<Self, anyhow::Error> {
+ ) -> Result<Self, Self::Error> {
let email =
uid.email().context("ERROR while converting userid.email")?;
@@ -123,10 +132,8 @@ impl UserId {
let raw = String::from_utf8(uid.value().to_vec()).ok();
- let revocations: Vec<_> = uid
- .self_revocations()
- .map(|rev| Revocation::from_sig(rev))
- .collect();
+ let revocations: Vec<_> =
+ uid.self_revocations().map(|rev| rev.into()).collect();
let revocations = if revocations.is_empty() {
None
@@ -143,10 +150,8 @@ impl UserId {
}
}
-impl Key {
- fn from_key_amalgamation(
- ka: &ErasedKeyAmalgamation<key::PublicParts>,
- ) -> Self {
+impl From<&ErasedKeyAmalgamation<'_, key::PublicParts>> for Key {
+ fn from(ka: &ErasedKeyAmalgamation<key::PublicParts>) -> Self {
let (expiration, flags) =
if let Ok(valid_sk) = ka.clone().with_policy(POLICY, None) {
(valid_sk.key_expiration_time(), valid_sk.key_flags())
@@ -186,10 +191,8 @@ impl Key {
None
};
- let revocations: Vec<_> = ka
- .self_revocations()
- .map(|rev| Revocation::from_sig(rev))
- .collect();
+ let revocations: Vec<_> =
+ ka.self_revocations().map(|rev| rev.into()).collect();
let revocations = if revocations.is_empty() {
None
@@ -210,8 +213,8 @@ impl Key {
}
}
-impl Revocation {
- fn from_sig(rev: &Signature) -> Self {
+impl From<&Signature> for Revocation {
+ fn from(rev: &Signature) -> Self {
let rfr = rev.reason_for_revocation();
if let Some(r) = rfr {
diff --git a/openpgp-ca-restd/src/process_certs.rs b/openpgp-ca-restd/src/process_certs.rs
index d411111..b49c57e 100644
--- a/openpgp-ca-restd/src/process_certs.rs
+++ b/openpgp-ca-restd/src/process_certs.rs
@@ -27,6 +27,7 @@ use crate::cert_info::CertInfo;
use crate::json::*;
use crate::restd;
use crate::util::{is_email_in_domain, split_emails, user_id_filter};
+use std::convert::TryInto;
const STANDARD_POLICY: &StandardPolicy = &StandardPolicy::new();
@@ -107,10 +108,10 @@ pub fn cert_to_warn(cert: &Cert) -> Result<Option<Vec<Warning>>, CertError> {
}
pub fn cert_to_cert_info(cert: &Cert) -> Result<CertInfo, ReturnError> {
- CertInfo::from_cert(cert).map_err(|e| {
+ cert.try_into().map_err(|e| {
ReturnError::new(
ReturnStatus::InternalError,
- format!("Error in CertInfo::from_cert() '{:?}'", e),
+ format!("Error getting CertInfo from Cert '{:?}'", e),
)
})
}
@@ -202,7 +203,7 @@ fn validate_and_strip_user_ids(
}
fn check_cert(cert: &Cert) -> Result<CertInfo, ReturnBadJson> {
- let ci = CertInfo::from_cert(cert).map_err(|e| {
+ let ci = cert.try_into().map_err(|e| {
ReturnBadJson::new(
CertError::new(
CertStatus::InternalError,
@@ -379,7 +380,7 @@ fn process_cert(
)
.map_err(|e| ReturnBadJson::new(e, Some(cert_info.clone())))?;
- let cert_info_norm = CertInfo::from_cert(&norm)
+ let cert_info_norm: CertInfo = (&norm).try_into()
.map_err(|e| {
CertError::new(
CertStatus::InternalError,
diff --git a/openpgp-ca-restd/src/restd.rs b/openpgp-ca-restd/src/restd.rs
index 7c16101..d7ccb4d 100644
--- a/openpgp-ca-restd/src/restd.rs
+++ b/openpgp-ca-restd/src/restd.rs
@@ -21,6 +21,7 @@ use openpgp_ca_lib::pgp::Pgp;
use crate::cert_info::CertInfo;
use crate::json::*;
use crate::process_certs::{cert_to_cert_info, cert_to_warn, process_certs};
+use std::convert::TryInto;
static DB: OnceCell<Option<String>> = OnceCell::new();
@@ -357,7 +358,7 @@ fn check_expiring(
)
})?;
- let ci = CertInfo::from_cert(&cert).map_err(|e| {
+ let ci = (&cert).try_into().map_err(|e| {
ReturnError::new(
ReturnStatus::InternalError,
format!(