diff --git a/src/bytearray.rs b/src/bytearray.rs index 14be628..b63cce2 100644 --- a/src/bytearray.rs +++ b/src/bytearray.rs @@ -9,6 +9,8 @@ use core::ops::{Deref, DerefMut}; use serde::de::{Deserialize, Deserializer, Error, SeqAccess, Visitor}; use serde::ser::{Serialize, Serializer}; +const ERROR_STRING: &'static str = "Expected a fixed amount of bytes, got a different amount"; + /// Wrapper around `[u8; N]` to serialize and deserialize efficiently. /// /// ``` @@ -189,10 +191,7 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor { let mut idx = 0; while let Some(b) = visitor.next_element()? { if idx >= N { - return Err(V::Error::custom(format!( - "Expected [u8; {0}], got more than {0} elements", - N - ))); + return Err(V::Error::custom(ERROR_STRING)); } bytes[idx] = b; @@ -200,10 +199,7 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor { } if idx != N { - return Err(V::Error::custom(format!( - "Expected [u8; {}], got [u8; {}]", - N, idx - ))); + return Err(V::Error::custom(ERROR_STRING)); } Ok(ByteArray::from(bytes)) @@ -214,20 +210,7 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor { E: Error, { Ok(ByteArray { - bytes: v - .try_into() - .map_err(|_| E::custom(format!("Expected [u8; {}]", N)))?, - }) - } - - fn visit_byte_buf(self, v: Vec) -> Result, E> - where - E: Error, - { - Ok(ByteArray { - bytes: v - .try_into() - .map_err(|_| E::custom(format!("Expected [u8; {}]", N)))?, + bytes: v.try_into().map_err(|_| E::custom(ERROR_STRING))?, }) } @@ -239,19 +222,7 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor { bytes: v .as_bytes() .try_into() - .map_err(|_| E::custom(format!("Expected [u8; {}]", N)))?, - }) - } - - fn visit_string(self, v: String) -> Result, E> - where - E: Error, - { - Ok(ByteArray { - bytes: v - .as_bytes() - .try_into() - .map_err(|_| E::custom(format!("Expected [u8; {}]", N)))?, + .map_err(|_| E::custom(ERROR_STRING))?, }) } }