Skip to content

Commit

Permalink
refactor: use derive(FromPrimitive)
Browse files Browse the repository at this point in the history
Former-commit-id: 751865f
  • Loading branch information
dignifiedquire committed Nov 8, 2018
1 parent 1a6f9ad commit d84faa7
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 68 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/composed/message.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/crypto/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Digest>`.
Expand Down
4 changes: 1 addition & 3 deletions src/crypto/sym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down
3 changes: 2 additions & 1 deletion src/packet/single.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/packet/tags/privkey.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/packet/tags/pubkey.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/packet/tags/public_key_encrypted_session_key.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use enum_primitive::FromPrimitive;
use nom::be_u8;
use num_traits::FromPrimitive;

use errors::Result;
use packet::types::key::KeyID;
Expand Down
12 changes: 5 additions & 7 deletions src/packet/tags/sig.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
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;
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.
Expand All @@ -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<Utc> {
Expand Down
72 changes: 26 additions & 46 deletions src/packet/types/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ impl From<usize> 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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -192,7 +183,6 @@ pub enum SubpacketType {
SignatureTarget = 31,
EmbeddedSignature = 32,
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Subpacket {
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -492,4 +473,3 @@ pub enum PublicKeyAlgorithm {
Private109 = 109,
Private110 = 110,
}
}

0 comments on commit d84faa7

Please sign in to comment.