From d84faa7bfe1876c7e2c65ee2489237b870df8433 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 8 Nov 2018 20:23:06 +0100 Subject: [PATCH] refactor: use derive(FromPrimitive) Former-commit-id: 751865f885cec99a61ee49fa213bb4dec2b17de5 --- Cargo.toml | 3 +- src/composed/message.rs | 2 +- src/crypto/hash.rs | 4 +- src/crypto/sym.rs | 4 +- src/lib.rs | 5 +- src/packet/single.rs | 3 +- src/packet/tags/privkey.rs | 2 +- src/packet/tags/pubkey.rs | 2 +- .../tags/public_key_encrypted_session_key.rs | 2 +- src/packet/tags/sig.rs | 12 ++-- src/packet/types/packet.rs | 72 +++++++------------ 11 files changed, 43 insertions(+), 68 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b6a07443..8128f22e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,12 +34,13 @@ block-modes = "^0.1" hex = "^0.3" cfb-mode = "^0.1" flate2 = { version = "1.0.4", default-features = false, features = ["rust_backend"] } +num-derive = "0.2.3" +num-traits = "0.2.6" [dependencies.nom] version = "^4.0" [dev-dependencies] -num-traits = "0.2.6" rand = "0.5.5" hex-literal = "^0.1" serde = "^1.0" diff --git a/src/composed/message.rs b/src/composed/message.rs index 6a7849e0..a1536460 100644 --- a/src/composed/message.rs +++ b/src/composed/message.rs @@ -1,8 +1,8 @@ use std::boxed::Box; use byteorder::{BigEndian, ReadBytesExt}; -use enum_primitive::FromPrimitive; use flate2::read::DeflateDecoder; +use num_traits::FromPrimitive; use rsa::padding::PaddingScheme; use composed::key::PrivateKey; diff --git a/src/crypto/hash.rs b/src/crypto/hash.rs index b472ecc2..5f840c31 100644 --- a/src/crypto/hash.rs +++ b/src/crypto/hash.rs @@ -9,8 +9,7 @@ use sha2::{Sha224, Sha256, Sha384, Sha512}; use errors::Result; -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] /// Available hash algorithms. /// Ref: https://tools.ietf.org/html/rfc4880.html#section-9.4 pub enum HashAlgorithm { @@ -22,7 +21,6 @@ pub enum HashAlgorithm { SHA512 = 10, SHA224 = 11, } -} /// Trait to work around the fact that the `Digest` trait from rustcrypto can not /// be used as `Box`. diff --git a/src/crypto/sym.rs b/src/crypto/sym.rs index e7478f56..46db3d67 100644 --- a/src/crypto/sym.rs +++ b/src/crypto/sym.rs @@ -36,8 +36,7 @@ macro_rules! encrypt_regular { mode.encrypt($plaintext); }}; } -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] /// Available symmetric key algorithms. pub enum SymmetricKeyAlgorithm { /// Plaintext or unencrypted data @@ -55,7 +54,6 @@ pub enum SymmetricKeyAlgorithm { /// Twofish with 256-bit key [TWOFISH] Twofish = 10, } -} impl SymmetricKeyAlgorithm { /// The size of a single block in bytes. diff --git a/src/lib.rs b/src/lib.rs index f8f043ca..e7ca14be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ extern crate byteorder; extern crate crc24; extern crate rsa; #[macro_use] -extern crate enum_primitive; +extern crate num_derive; extern crate chrono; #[macro_use] extern crate failure; @@ -21,13 +21,12 @@ extern crate generic_array; extern crate itertools; extern crate md5; extern crate num_bigint; +extern crate num_traits; extern crate ripemd160; extern crate sha1; extern crate sha2; extern crate twofish; -#[cfg(test)] -extern crate num_traits; #[cfg(test)] extern crate rand; #[cfg(test)] diff --git a/src/packet/single.rs b/src/packet/single.rs index 8ee6ab8f..9a613fb4 100644 --- a/src/packet/single.rs +++ b/src/packet/single.rs @@ -1,5 +1,6 @@ -use enum_primitive::FromPrimitive; use nom::rest; +use num_traits::FromPrimitive; + use util::{u16_as_usize, u32_as_usize, u8_as_usize}; use super::types::{Packet, PacketLength, Tag, Version}; diff --git a/src/packet/tags/privkey.rs b/src/packet/tags/privkey.rs index 22495682..8817650f 100644 --- a/src/packet/tags/privkey.rs +++ b/src/packet/tags/privkey.rs @@ -1,6 +1,6 @@ -use enum_primitive::FromPrimitive; use nom::{self, be_u16, be_u32, be_u8}; use num_bigint::BigUint; +use num_traits::FromPrimitive; use composed; use crypto::hash::HashAlgorithm; diff --git a/src/packet/tags/pubkey.rs b/src/packet/tags/pubkey.rs index d3830b32..2ae4f3b4 100644 --- a/src/packet/tags/pubkey.rs +++ b/src/packet/tags/pubkey.rs @@ -1,5 +1,5 @@ -use enum_primitive::FromPrimitive; use nom::{self, be_u16, be_u32, be_u8}; +use num_traits::FromPrimitive; use composed; use packet::types::ecc_curve::ecc_curve_from_oid; diff --git a/src/packet/tags/public_key_encrypted_session_key.rs b/src/packet/tags/public_key_encrypted_session_key.rs index 4507426c..d441e2ff 100644 --- a/src/packet/tags/public_key_encrypted_session_key.rs +++ b/src/packet/tags/public_key_encrypted_session_key.rs @@ -1,5 +1,5 @@ -use enum_primitive::FromPrimitive; use nom::be_u8; +use num_traits::FromPrimitive; use errors::Result; use packet::types::key::KeyID; diff --git a/src/packet/tags/sig.rs b/src/packet/tags/sig.rs index aa32833b..0e9e071c 100644 --- a/src/packet/tags/sig.rs +++ b/src/packet/tags/sig.rs @@ -1,8 +1,8 @@ +use std::str; + use chrono::{DateTime, NaiveDateTime, Utc}; -use enum_primitive::FromPrimitive; use nom::{be_u16, be_u32, be_u8, rest, IResult}; -use std::str; -use util::mpi; +use num_traits::FromPrimitive; use crypto::hash::HashAlgorithm; use crypto::sym::SymmetricKeyAlgorithm; @@ -10,10 +10,9 @@ use packet::types::{ self, CompressionAlgorithm, PublicKeyAlgorithm, RevocationCode, Signature, SignatureType, SignatureVersion, Subpacket, SubpacketType, }; -use util::{clone_into_array, packet_length}; +use util::{clone_into_array, mpi, packet_length}; -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(FromPrimitive)] /// Available key flags pub enum KeyFlag { /// This key may be used to certify other keys. @@ -31,7 +30,6 @@ pub enum KeyFlag { /// The private component of this key may be in the possession of more than one person. SharedPrivateKey = 0x80, } -} /// Convert an epoch timestamp to a `DateTime` fn dt_from_timestamp(ts: u32) -> DateTime { diff --git a/src/packet/types/packet.rs b/src/packet/types/packet.rs index 9a2ac795..e1bc738b 100644 --- a/src/packet/types/packet.rs +++ b/src/packet/types/packet.rs @@ -33,8 +33,7 @@ impl From for PacketLength { } } -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] pub enum Tag { /// Public-Key Encrypted Session Key Packet PublicKeyEncryptedSessionKey = 1, @@ -71,17 +70,14 @@ pub enum Tag { /// Modification Detection Code Packet ModDetectionCode = 19, } -} -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] pub enum Version { /// Old Packet Format Old = 0, /// New Packet Format New = 1, } -} #[derive(Debug, PartialEq, Eq, Clone)] /// Available user attribute types @@ -111,8 +107,7 @@ impl UserAttribute { } } -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] /// Codes for revocation reasons pub enum RevocationCode { /// No reason specified (key revocations or cert revocations) @@ -126,28 +121,25 @@ pub enum RevocationCode { /// User ID information is no longer valid (cert revocations) CertUserIdInvalid = 32, } -} -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] /// Available String-To-Key types - pub enum StringToKeyType { - Simple = 0, - Salted = 1, - Reserved = 2, - IteratedAndSalted = 3, - Private100 = 100, - Private101 = 101, - Private102 = 102, - Private103 = 103, - Private104 = 104, - Private105 = 105, - Private106 = 106, - Private107 = 107, - Private108 = 108, - Private109 = 109, - Private110 = 110, - } +pub enum StringToKeyType { + Simple = 0, + Salted = 1, + Reserved = 2, + IteratedAndSalted = 3, + Private100 = 100, + Private101 = 101, + Private102 = 102, + Private103 = 103, + Private104 = 104, + Private105 = 105, + Private106 = 106, + Private107 = 107, + Private108 = 108, + Private109 = 109, + Private110 = 110, } impl StringToKeyType { @@ -164,8 +156,7 @@ impl StringToKeyType { } } -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] /// Available signature subpacket types pub enum SubpacketType { SignatureCreationTime = 2, @@ -192,7 +183,6 @@ pub enum SubpacketType { SignatureTarget = 31, EmbeddedSignature = 32, } -} #[derive(Debug, PartialEq, Eq, Clone)] pub enum Subpacket { @@ -226,8 +216,7 @@ pub enum Subpacket { ExportableCertification(bool), } -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] /// Available compression algorithms. /// Ref: https://tools.ietf.org/html/rfc4880.html#section-9.3 pub enum CompressionAlgorithm { @@ -236,10 +225,8 @@ pub enum CompressionAlgorithm { ZLIB = 2, BZip2 = 3, } -} -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] pub enum SignatureType { /// Signature of a binary document. /// This means the signer owns it, created it, or certifies that ithas not been modified. @@ -334,17 +321,14 @@ pub enum SignatureType { /// document) that cannot include a target subpacket. ThirdParty = 0x50, } -} -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, FromPrimitive)] pub enum SignatureVersion { /// Deprecated V2 = 2, V3 = 3, V4 = 4, } -} #[derive(Debug, PartialEq, Eq, Clone)] pub struct RevocationKey { @@ -446,17 +430,14 @@ impl User { } } -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, FromPrimitive)] pub enum KeyVersion { V2 = 2, V3 = 3, V4 = 4, } -} -enum_from_primitive!{ -#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, PartialEq, Eq, Clone, Copy, FromPrimitive)] pub enum PublicKeyAlgorithm { /// RSA (Encrypt and Sign) [HAC] RSA = 1, @@ -492,4 +473,3 @@ pub enum PublicKeyAlgorithm { Private109 = 109, Private110 = 110, } -}