From b0a474e012632be9d6e328e31f6b9b9a4c919c62 Mon Sep 17 00:00:00 2001 From: Piet Geursen Date: Fri, 26 Nov 2021 18:32:03 +1300 Subject: [PATCH] rename properly --- src/lib.rs | 132 ++++++++++++++++++++++++++--------------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c73f2a2..066837a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ -//! yamf-hash +//! yasmf-hash //! -//! Encode and decode [yamf-hashes](https://github.com/AljoschaMeyer/yamf-hash) +//! Encode and decode [yasmf-hashes](https://github.com/AljoschaMeyer/yamf-hash) //! #![cfg_attr(not(feature = "std"), no_std)] @@ -40,9 +40,9 @@ pub const BLAKE3_NUMERIC_ID: u64 = 0; /// This is unlikely to cause a problem until there are hundreds of variants. pub const MAX_YAMF_HASH_SIZE: usize = BLAKE3_HASH_SIZE + 2; -/// Variants of `YamfHash` +/// Variants of `YasmfHash` #[derive(Deserialize, Serialize, Debug, Eq)] -pub enum YamfHash> { +pub enum YasmfHash> { #[cfg_attr( feature = "std", serde(serialize_with = "hex_from_bytes", deserialize_with = "vec_from_hex") @@ -51,47 +51,47 @@ pub enum YamfHash> { Blake3(T), } -impl, B2: Borrow<[u8]>> PartialEq> for YamfHash { - fn eq(&self, other: &YamfHash) -> bool { +impl, B2: Borrow<[u8]>> PartialEq> for YasmfHash { + fn eq(&self, other: &YasmfHash) -> bool { match (self, other) { - (YamfHash::Blake3(vec), YamfHash::Blake3(vec2)) => vec.borrow() == vec2.borrow(), + (YasmfHash::Blake3(vec), YasmfHash::Blake3(vec2)) => vec.borrow() == vec2.borrow(), } } } -pub fn new_blake3(bytes: &[u8]) -> YamfHash> { +pub fn new_blake3(bytes: &[u8]) -> YasmfHash> { let hash_bytes = blake3(bytes); let vec_bytes: ArrayVec<[u8; BLAKE3_HASH_SIZE]> = ArrayVec::from_iter(hash_bytes.as_bytes().iter().map(|b| *b)); - YamfHash::Blake3(vec_bytes) + YasmfHash::Blake3(vec_bytes) } -impl<'a> From<&'a YamfHash>> for YamfHash<&'a [u8]> { - fn from(hash: &YamfHash>) -> YamfHash<&[u8]> { +impl<'a> From<&'a YasmfHash>> for YasmfHash<&'a [u8]> { + fn from(hash: &YasmfHash>) -> YasmfHash<&[u8]> { match hash { - YamfHash::Blake3(bytes) => YamfHash::Blake3(&bytes[..]), + YasmfHash::Blake3(bytes) => YasmfHash::Blake3(&bytes[..]), } } } -impl<'a> From for YamfHash> { +impl<'a> From for YasmfHash> { fn from(hash: Blake3Hash) -> Self { let vec_bytes: ArrayVec<[u8; BLAKE3_HASH_SIZE]> = ArrayVec::from_iter(hash.as_bytes().iter().map(|b| *b)); - YamfHash::Blake3(vec_bytes) + YasmfHash::Blake3(vec_bytes) } } -impl> YamfHash { - /// Encode a YamfHash into the out buffer. +impl> YasmfHash { + /// Encode a YasmfHash into the out buffer. pub fn encode(&self, out: &mut [u8]) -> Result { let encoded_size = self.encoding_length(); match (self, out.len()) { - (YamfHash::Blake3(vec), len) if len >= encoded_size => { + (YasmfHash::Blake3(vec), len) if len >= encoded_size => { varu64_encode(BLAKE3_NUMERIC_ID, &mut out[0..1]); varu64_encode(BLAKE3_HASH_SIZE as u64, &mut out[1..2]); out[2..encoded_size].copy_from_slice(vec.borrow()); @@ -103,7 +103,7 @@ impl> YamfHash { pub fn encoding_length(&self) -> usize { match self { - YamfHash::Blake3(_) => { + YasmfHash::Blake3(_) => { encoding_length(BLAKE3_NUMERIC_ID) + encoding_length(BLAKE3_HASH_SIZE as u64) + BLAKE3_HASH_SIZE @@ -111,40 +111,40 @@ impl> YamfHash { } } - /// Decode the `bytes` as a `YamfHash` - pub fn decode<'a>(bytes: &'a [u8]) -> Result<(YamfHash<&'a [u8]>, &'a [u8]), Error> { + /// Decode the `bytes` as a `YasmfHash` + pub fn decode<'a>(bytes: &'a [u8]) -> Result<(YasmfHash<&'a [u8]>, &'a [u8]), Error> { match varu64_decode(&bytes) { Ok((BLAKE3_NUMERIC_ID, remaining_bytes)) if remaining_bytes.len() >= 33 => { let hash = &remaining_bytes[1..33]; - Ok((YamfHash::Blake3(hash), &remaining_bytes[33..])) + Ok((YasmfHash::Blake3(hash), &remaining_bytes[33..])) } Err((_, _)) => Err(Error::DecodeVaru64Error), _ => Err(Error::DecodeError {}), } } - /// Decode the `bytes` as a `YamfHash` + /// Decode the `bytes` as a `YasmfHash` pub fn decode_owned<'a>( bytes: &'a [u8], - ) -> Result<(YamfHash>, &'a [u8]), Error> { + ) -> Result<(YasmfHash>, &'a [u8]), Error> { match varu64_decode(&bytes) { Ok((BLAKE3_NUMERIC_ID, remaining_bytes)) if remaining_bytes.len() >= 33 => { let mut vec = ArrayVec::new(); let slice = &remaining_bytes[1..33]; vec.try_extend_from_slice(slice).unwrap(); - Ok((YamfHash::Blake3(vec), &remaining_bytes[33..])) + Ok((YasmfHash::Blake3(vec), &remaining_bytes[33..])) } Err((_, _)) => Err(Error::DecodeVaru64Error), _ => Err(Error::DecodeError {}), } } - /// Encode a YamfHash into the writer. + /// Encode a YasmfHash into the writer. #[cfg(feature = "std")] pub fn encode_write(&self, mut w: W) -> Result<(), Error> { let mut out = [0; 2]; match self { - YamfHash::Blake3(vec) => { + YasmfHash::Blake3(vec) => { varu64_encode(BLAKE3_NUMERIC_ID, &mut out[0..1]); varu64_encode(BLAKE3_HASH_SIZE as u64, &mut out[1..2]); w.write_all(&out).map_err(|_| Error::EncodeWriteError)?; @@ -160,64 +160,64 @@ impl> YamfHash { mod tests { use crate::MAX_YAMF_HASH_SIZE; - use super::{new_blake3, blake3, Error, YamfHash, BLAKE3_HASH_SIZE}; + use super::{new_blake3, blake3, Error, YasmfHash, BLAKE3_HASH_SIZE}; use arrayvec::ArrayVec; use core::iter::FromIterator; #[test] - fn encode_yamf() { + fn encode_yasmf() { let hash_bytes = vec![0xFF; BLAKE3_HASH_SIZE]; - let yamf_hash = YamfHash::Blake3(hash_bytes); + let yasmf_hash = YasmfHash::Blake3(hash_bytes); let mut encoded = vec![0; MAX_YAMF_HASH_SIZE]; - let length = yamf_hash.encode(&mut encoded).unwrap(); + let length = yasmf_hash.encode(&mut encoded).unwrap(); assert_eq!(length, MAX_YAMF_HASH_SIZE); assert_eq!(encoded[0], 0); assert_eq!(encoded[1], 32); } #[test] - fn encode_yamf_write() { + fn encode_yasmf_write() { let hash_bytes = vec![0xFF; BLAKE3_HASH_SIZE]; - let yamf_hash = YamfHash::Blake3(hash_bytes); + let yasmf_hash = YasmfHash::Blake3(hash_bytes); let mut encoded = Vec::new(); - yamf_hash.encode_write(&mut encoded).unwrap(); + yasmf_hash.encode_write(&mut encoded).unwrap(); assert_eq!(encoded.len(), 34); assert_eq!(encoded[0], 0); assert_eq!(encoded[1], 32); } #[test] - fn encode_yamf_not_enough_bytes_for_varu() { + fn encode_yasmf_not_enough_bytes_for_varu() { let hash_bytes = vec![0xFF; 4]; - let yamf_hash = YamfHash::Blake3(hash_bytes); + let yasmf_hash = YasmfHash::Blake3(hash_bytes); let mut encoded = [0; 2]; - match yamf_hash.encode_write(&mut encoded[..]) { + match yasmf_hash.encode_write(&mut encoded[..]) { Err(Error::EncodeWriteError) => {} _ => panic!("Go ok, expected error"), } } #[test] - fn encode_yamf_not_enough_bytes_for_hash() { + fn encode_yasmf_not_enough_bytes_for_hash() { let hash_bytes = vec![0xFF; 4]; - let yamf_hash = YamfHash::Blake3(hash_bytes); + let yasmf_hash = YasmfHash::Blake3(hash_bytes); let mut encoded = [0; 4]; - match yamf_hash.encode_write(&mut encoded[..]) { + match yasmf_hash.encode_write(&mut encoded[..]) { Err(Error::EncodeWriteError) => {} _ => panic!("Go ok, expected error"), } } #[test] - fn decode_yamf() { + fn decode_yasmf() { let mut hash_bytes = vec![0xFF; 35]; hash_bytes[0] = 0; hash_bytes[1] = 32; hash_bytes[34] = 0xAA; - let result = YamfHash::<&[u8]>::decode(&hash_bytes); + let result = YasmfHash::<&[u8]>::decode(&hash_bytes); match result { - Ok((YamfHash::Blake3(vec), remaining_bytes)) => { + Ok((YasmfHash::Blake3(vec), remaining_bytes)) => { assert_eq!(vec.len(), 32); assert_eq!(vec, &hash_bytes[2..34]); assert_eq!(remaining_bytes, &[0xAA]); @@ -226,13 +226,13 @@ mod tests { } } #[test] - fn decode_yamf_varu_error() { + fn decode_yasmf_varu_error() { let mut hash_bytes = vec![0xFF; 67]; hash_bytes[0] = 248; hash_bytes[1] = 1; hash_bytes[2] = 32; hash_bytes[66] = 0xAA; - let result = YamfHash::<&[u8]>::decode(&hash_bytes); + let result = YasmfHash::<&[u8]>::decode(&hash_bytes); match result { Err(Error::DecodeVaru64Error) => {} @@ -240,11 +240,11 @@ mod tests { } } #[test] - fn decode_yamf_not_enough_bytes_error() { + fn decode_yasmf_not_enough_bytes_error() { let mut hash_bytes = vec![0xFF; BLAKE3_HASH_SIZE]; hash_bytes[0] = 0; hash_bytes[1] = 32; - let result = YamfHash::<&[u8]>::decode(&hash_bytes); + let result = YasmfHash::<&[u8]>::decode(&hash_bytes); match result { Err(Error::DecodeError {}) => {} @@ -253,85 +253,85 @@ mod tests { } #[test] - fn blake_yamf_hash() { + fn blake_yasmf_hash() { let lam = || { let hash_bytes = blake3(&[1, 2]); let vec_bytes: ArrayVec<[u8; BLAKE3_HASH_SIZE]> = ArrayVec::from_iter(hash_bytes.as_bytes().iter().map(|b| *b)); - YamfHash::Blake3(vec_bytes) + YasmfHash::Blake3(vec_bytes) }; let _ = lam(); } #[test] - fn blake2b_yamf_hash_eq() { + fn blake2b_yasmf_hash_eq() { let lam = || { let hash_bytes = blake3(&[1, 2]); let vec_bytes: ArrayVec<[u8; BLAKE3_HASH_SIZE]> = ArrayVec::from_iter(hash_bytes.as_bytes().iter().map(|b| *b)); - YamfHash::Blake3(vec_bytes) + YasmfHash::Blake3(vec_bytes) }; let result = lam(); let hash_bytes = blake3(&[1, 2]); - let result2 = YamfHash::Blake3(&hash_bytes.as_bytes()[..]); + let result2 = YasmfHash::Blake3(&hash_bytes.as_bytes()[..]); assert_eq!(result, result2); assert_eq!(result2, result); } #[test] - fn owned_yamf_hash() { + fn owned_yasmf_hash() { let lam = || { let mut hash_bytes = ArrayVec::<[u8; BLAKE3_HASH_SIZE]>::new(); hash_bytes.push(1); hash_bytes.push(64); - YamfHash::Blake3(hash_bytes) + YasmfHash::Blake3(hash_bytes) }; let _ = lam(); } #[test] - fn ref_yamf_hash() { + fn ref_yasmf_hash() { let mut hash_bytes = ArrayVec::<[u8; BLAKE3_HASH_SIZE * 2]>::new(); hash_bytes.push(1); hash_bytes.push(64); - YamfHash::Blake3(hash_bytes); + YasmfHash::Blake3(hash_bytes); } #[test] - fn from_owned_to_ref_yamf_hash() { + fn from_owned_to_ref_yasmf_hash() { let lam = || { let mut hash_bytes = ArrayVec::<[u8; BLAKE3_HASH_SIZE]>::new(); hash_bytes.push(1); hash_bytes.push(64); - YamfHash::Blake3(hash_bytes) + YasmfHash::Blake3(hash_bytes) }; let result = lam(); - let _: YamfHash<&[u8]> = YamfHash::from(&result); + let _: YasmfHash<&[u8]> = YasmfHash::from(&result); } #[test] fn encode_decode_blake2b() { let bytes = vec![1, 2, 3]; - let yamf_hash = new_blake3(&bytes); + let yasmf_hash = new_blake3(&bytes); let mut encoded = Vec::new(); - yamf_hash.encode_write(&mut encoded).unwrap(); + yasmf_hash.encode_write(&mut encoded).unwrap(); - let (decoded, _) = YamfHash::>::decode_owned(&encoded).unwrap(); + let (decoded, _) = YasmfHash::>::decode_owned(&encoded).unwrap(); - assert_eq!(decoded, yamf_hash); + assert_eq!(decoded, yasmf_hash); } #[test] fn encode_decode_blake3() { let bytes = vec![1, 2, 3]; - let yamf_hash = new_blake3(&bytes); + let yasmf_hash = new_blake3(&bytes); let mut encoded = Vec::new(); - yamf_hash.encode_write(&mut encoded).unwrap(); + yasmf_hash.encode_write(&mut encoded).unwrap(); - let (decoded, _) = YamfHash::>::decode_owned(&encoded).unwrap(); + let (decoded, _) = YasmfHash::>::decode_owned(&encoded).unwrap(); - assert_eq!(decoded, yamf_hash); + assert_eq!(decoded, yasmf_hash); } }