Skip to content

Commit ea25369

Browse files
committed
clippy fixes
Signed-off-by: Dave Huseby <[email protected]>
1 parent a513612 commit ea25369

File tree

12 files changed

+170
-173
lines changed

12 files changed

+170
-173
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "multikey"
3-
version = "1.0.6"
3+
version = "1.0.7"
44
edition = "2021"
55
authors = ["Dave Grantham <[email protected]>"]
66
description = "Multikey self-describing cryptographic key data"

src/attrid.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub enum AttrId {
3838
impl AttrId {
3939
/// Get the code for the attribute id
4040
pub fn code(&self) -> u8 {
41-
self.clone().into()
41+
(*self).into()
4242
}
4343

4444
/// Convert the attribute id to &str
@@ -60,9 +60,9 @@ impl AttrId {
6060
}
6161
}
6262

63-
impl Into<u8> for AttrId {
64-
fn into(self) -> u8 {
65-
self as u8
63+
impl From<AttrId> for u8 {
64+
fn from(val: AttrId) -> Self {
65+
val as u8
6666
}
6767
}
6868

@@ -88,9 +88,9 @@ impl TryFrom<u8> for AttrId {
8888
}
8989
}
9090

91-
impl Into<Vec<u8>> for AttrId {
92-
fn into(self) -> Vec<u8> {
93-
let v: u8 = self.into();
91+
impl From<AttrId> for Vec<u8> {
92+
fn from(val: AttrId) -> Self {
93+
let v: u8 = val.into();
9494
v.encode_into()
9595
}
9696
}

src/kdf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl Builder {
2525
/// initialize from a multikey with kdf attributes in it
2626
pub fn try_from_multikey(mut self, mk: &Multikey) -> Result<Self, Error> {
2727
// try to look up the kdf codec in the multikey attributes
28-
if let Some(v) = mk.attributes.get(&(AttrId::KdfCodec.into())) {
28+
if let Some(v) = mk.attributes.get(&AttrId::KdfCodec) {
2929
if let Ok(codec) = Codec::try_from(v.as_slice()) {
3030
self.codec = codec;
3131
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Idnetifier: Apache-2.0
2-
//!
2+
//! multikey
33
#![warn(missing_docs)]
44
#![deny(
55
trivial_casts,

src/mk.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,15 @@ impl Builder {
404404
let key_bytes = match codec {
405405
Codec::Ed25519Priv => Ed25519Keypair::random(rng).private.to_bytes().to_vec(),
406406
Codec::P256Priv => EcdsaKeypair::random(rng, EcdsaCurve::NistP256)
407-
.map_err(|e| ConversionsError::SshKey(e))?
407+
.map_err(ConversionsError::SshKey)?
408408
.private_key_bytes()
409409
.to_vec(),
410410
Codec::P384Priv => EcdsaKeypair::random(rng, EcdsaCurve::NistP384)
411-
.map_err(|e| ConversionsError::SshKey(e))?
411+
.map_err(ConversionsError::SshKey)?
412412
.private_key_bytes()
413413
.to_vec(),
414414
Codec::P521Priv => EcdsaKeypair::random(rng, EcdsaCurve::NistP521)
415-
.map_err(|e| ConversionsError::SshKey(e))?
415+
.map_err(ConversionsError::SshKey)?
416416
.private_key_bytes()
417417
.to_vec(),
418418
Codec::Secp256K1Priv => k256::SecretKey::random(rng).to_bytes().to_vec(),
@@ -592,7 +592,7 @@ impl Builder {
592592
..Default::default()
593593
})
594594
}
595-
s => return Err(ConversionsError::UnsupportedAlgorithm(s.to_string()).into()),
595+
s => Err(ConversionsError::UnsupportedAlgorithm(s.to_string()).into()),
596596
},
597597
Ed25519 => {
598598
let key_bytes = match sshkey.key_data() {
@@ -780,7 +780,7 @@ impl Builder {
780780
..Default::default()
781781
})
782782
}
783-
s => return Err(ConversionsError::UnsupportedAlgorithm(s.to_string()).into()),
783+
s => Err(ConversionsError::UnsupportedAlgorithm(s.to_string()).into()),
784784
},
785785
Ed25519 => {
786786
let key_bytes = match sshkey.key_data() {
@@ -819,7 +819,7 @@ impl Builder {
819819

820820
fn with_attribute(mut self, attr: AttrId, data: &Vec<u8>) -> Self {
821821
let mut attributes = self.attributes.unwrap_or_default();
822-
attributes.insert(attr, data.clone().into());
822+
attributes.insert(attr, data.to_owned().into());
823823
self.attributes = Some(attributes);
824824
self
825825
}
@@ -831,7 +831,8 @@ impl Builder {
831831

832832
/// add in the threshold value
833833
pub fn with_threshold(self, threshold: usize) -> Self {
834-
self.with_attribute(AttrId::Threshold, &Varuint(threshold).into())
834+
let v: Vec<u8> = Varuint(threshold).into();
835+
self.with_attribute(AttrId::Threshold, &v)
835836
}
836837

837838
/// add in the limit value
@@ -861,7 +862,7 @@ impl Builder {
861862
pub fn try_build_encoded(self) -> Result<EncodedMultikey, Error> {
862863
Ok(BaseEncoded::new(
863864
self.base_encoding
864-
.unwrap_or_else(|| Multikey::preferred_encoding()),
865+
.unwrap_or_else(Multikey::preferred_encoding),
865866
self.try_build()?,
866867
))
867868
}

src/nonce.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ impl Nonce {
2525
pub fn len(&self) -> usize {
2626
self.nonce.len()
2727
}
28+
29+
/// return if the nonce is empty
30+
pub fn is_empty(&self) -> bool {
31+
self.nonce.is_empty()
32+
}
2833
}
2934

3035
impl CodecInfo for Nonce {
@@ -121,8 +126,7 @@ pub struct Builder {
121126
impl Builder {
122127
/// build from random source
123128
pub fn new_from_random_bytes(size: usize, rng: &mut (impl RngCore + CryptoRng)) -> Self {
124-
let mut bytes = Vec::with_capacity(size);
125-
bytes.resize(size, 0u8);
129+
let mut bytes = vec![0; size];
126130
rng.fill_bytes(bytes.as_mut());
127131
Self {
128132
bytes,
@@ -148,7 +152,7 @@ impl Builder {
148152
pub fn try_build_encoded(&self) -> Result<EncodedNonce, Error> {
149153
Ok(EncodedNonce::new(
150154
self.base_encoding
151-
.unwrap_or_else(|| Nonce::preferred_encoding()),
155+
.unwrap_or_else(Nonce::preferred_encoding),
152156
self.try_build()?,
153157
))
154158
}

src/serde/de.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'de> Deserialize<'de> for Nonce {
1818
where
1919
D: Deserializer<'de>,
2020
{
21-
const FIELDS: &'static [&'static str] = &["nonce"];
21+
const FIELDS: &[&str] = &["nonce"];
2222

2323
#[derive(Deserialize)]
2424
#[serde(field_identifier, rename_all = "lowercase")]
@@ -84,28 +84,28 @@ impl<'de> Deserialize<'de> for AttrId {
8484
where
8585
E: Error,
8686
{
87-
Ok(AttrId::try_from(c).map_err(|e| Error::custom(e.to_string()))?)
87+
AttrId::try_from(c).map_err(|e| Error::custom(e.to_string()))
8888
}
8989

9090
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
9191
where
9292
E: Error,
9393
{
94-
Ok(AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))?)
94+
AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))
9595
}
9696

9797
fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E>
9898
where
9999
E: Error,
100100
{
101-
Ok(AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))?)
101+
AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))
102102
}
103103

104104
fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
105105
where
106106
E: Error,
107107
{
108-
Ok(AttrId::try_from(s.as_str()).map_err(|e| Error::custom(e.to_string()))?)
108+
AttrId::try_from(s.as_str()).map_err(|e| Error::custom(e.to_string()))
109109
}
110110
}
111111

@@ -119,7 +119,7 @@ impl<'de> Deserialize<'de> for Multikey {
119119
where
120120
D: Deserializer<'de>,
121121
{
122-
const FIELDS: &'static [&'static str] = &["codec", "comment", "attributes"];
122+
const FIELDS: &[&str] = &["codec", "comment", "attributes"];
123123

124124
#[derive(Deserialize)]
125125
#[serde(field_identifier, rename_all = "lowercase")]

src/views/bcrypt.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl<'a> KdfView for View<'a> {
3131
/// generates a new Multikey by copying the viewed Multikey (self) and
3232
/// storing the derived key and attributes in the new Multikey
3333
fn derive_key(&self, passphrase: &[u8]) -> Result<Multikey, Error> {
34-
let kdf = self.kdf.ok_or_else(|| KdfError::MissingCodec)?;
34+
let kdf = self.kdf.ok_or(KdfError::MissingCodec)?;
3535

3636
// get the salt data and rounds attribute
3737
let (salt, rounds) = {
@@ -42,21 +42,21 @@ impl<'a> KdfView for View<'a> {
4242
// get the key length from the viewed Multikey
4343
let key_length = {
4444
let cattr = self.mk.cipher_attr_view()?;
45-
let key_length = cattr.key_length()?;
46-
key_length
45+
46+
cattr.key_length()?
4747
};
4848

4949
// heap allocate a buffer to receive the derived key
5050
let mut key: Zeroizing<Vec<u8>> = vec![0; key_length].into();
5151

5252
// derive the key
5353
bcrypt_pbkdf::bcrypt_pbkdf(
54-
passphrase.as_ref(),
54+
passphrase,
5555
&salt,
5656
rounds as u32,
5757
key.as_mut_slice(),
5858
)
59-
.map_err(|e| KdfError::Bcrypt(e))?;
59+
.map_err(KdfError::Bcrypt)?;
6060

6161
// prepare the attributes
6262
let kdf_codec: Vec<u8> = kdf.codec.into();
@@ -85,7 +85,7 @@ impl<'a> KdfAttrView for View<'a> {
8585
.mk
8686
.attributes
8787
.get(&AttrId::KdfSalt)
88-
.ok_or_else(|| KdfError::MissingSalt)?;
88+
.ok_or(KdfError::MissingSalt)?;
8989
if salt.len() != SALT_LENGTH {
9090
Err(KdfError::InvalidSaltLen.into())
9191
} else {

0 commit comments

Comments
 (0)