pub fn deserialize_check_len<'a, 'de, D>(deserializer: D, len: ExpectedLen<'a>) -> Result<usize, D::Error>
where
	D: Deserializer<'de>,
{
	struct Visitor<'a> {
		len: ExpectedLen<'a>,
	}

	impl<'a, 'b> de::Visitor<'b> for Visitor<'a> {
		type Value = usize;

		fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
			write!(formatter, "a (both 0x-prefixed or not) hex string with {}", self.len)
		}

		fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
			let (v, stripped) = v.strip_prefix("0x").map_or((v, false), |v| (v, true));

			let len = v.len();
			let is_len_valid = match self.len {
				ExpectedLen::Exact(ref slice) => len == 2 * slice.len(),
				ExpectedLen::Between(min, ref slice) => len <= 2 * slice.len() && len > 2 * min,
			};

			if !is_len_valid {
				return Err(E::invalid_length(v.len(), &self))
			}

			let bytes = match self.len {
				ExpectedLen::Exact(slice) => slice,
				ExpectedLen::Between(_, slice) => slice,
			};

			from_hex_raw(v, bytes, stripped).map_err(E::custom)
		}

		fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
			self.visit_str(&v)
		}
	}

	deserializer.deserialize_str(Visitor { len })
}
/// Deserialize into vector of bytes with additional size check.
/// Returns number of bytes written.
Deserialize into vector of bytes with additional size check.
Returns number of bytes written.
Deserialize into vector of bytes with additional size check . Returns number of bytes written .
pub fn deserialize_check_len<'a, 'de, D>(deserializer: D, len: ExpectedLen<'a>) -> Result<usize, D::Error>
where
	D: Deserializer<'de>,
{
	struct Visitor<'a> {
		len: ExpectedLen<'a>,
	}
	impl<'a, 'b> de::Visitor<'b> for Visitor<'a> {
		type Value = usize;
		fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
			write!(formatter, "a (both 0x-prefixed or not) hex string with {}", self.len)
		}
		fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
			let (v, stripped) = v.strip_prefix("0x").map_or((v, false), |v| (v, true));
			let len = v.len();
			let is_len_valid = match self.len {
				ExpectedLen::Exact(ref slice) => len == 2 * slice.len(),
				ExpectedLen::Between(min, ref slice) => len <= 2 * slice.len() && len > 2 * min,
			};
			if !is_len_valid {
				return Err(E::invalid_length(v.len(), &self))
			}
			let bytes = match self.len {
				ExpectedLen::Exact(slice) => slice,
				ExpectedLen::Between(_, slice) => slice,
			};
			from_hex_raw(v, bytes, stripped).map_err(E::custom)
		}
		fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
			self.visit_str(&v)
		}
	}
	deserializer.deserialize_str(Visitor { len })
}
pub fn deserialize_check_len < ' a , ' de , D > ( deserializer : D , len : ExpectedLen < ' a > ) -> Result < usize , D :: Error > where D : Deserializer < ' de > , { struct Visitor < ' a > { len : ExpectedLen < ' a > , } impl < ' a , ' b > de :: Visitor < ' b > for Visitor < ' a > { type Value = usize ; fn expecting ( & self , formatter : & mut fmt :: Formatter ) -> fmt :: Result { write ! ( formatter " " self len ) } fn visit_str < E : de :: Error > ( self , v : & str ) -> Result < Self :: Value , E > { let ( v , stripped ) = v . strip_prefix ( " " ) . map_or ( ( v , false ) , | v | ( v , true ) ) ; let len = v . len ( ) ; let is_len_valid = match self . len { ExpectedLen :: Exact ( ref slice ) => len == 2 * slice . len ( ) , ExpectedLen :: Between ( min , ref slice ) => len <= 2 * slice . len ( ) && len > 2 * min , } ; if ! is_len_valid { return Err ( E :: invalid_length ( v . len ( ) , & self ) ) } let bytes = match self . len { ExpectedLen :: Exact ( slice ) => slice , ExpectedLen :: Between ( _ , slice ) => slice , } ; from_hex_raw ( v , bytes , stripped ) . map_err ( E :: custom ) } fn visit_string < E : de :: Error > ( self , v : String ) -> Result < Self :: Value , E > { self . visit_str ( & v ) } } deserializer . deserialize_str ( Visitor { len } ) }
Deserialize into vector of bytes with additional size check.
Deserialize into vector of bytes with additional size check .
deserializer
D
len
ExpectedLen<'a>
deserializer
D
len
ExpectedLen<'a>