use serde::de::{Deserializer, Error, SeqAccess, Visitor}; use std::fmt; pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { deserializer.deserialize_byte_buf(ByteBufVisitor) } struct ByteBufVisitor; impl<'de> Visitor<'de> for ByteBufVisitor { type Value = Vec; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("byte array") } fn visit_seq(self, mut visitor: V) -> Result where V: SeqAccess<'de>, { let mut values = Vec::new(); while let Some(value) = visitor.next_element()? { values.push(value); } Ok(values) } fn visit_bytes(self, v: &[u8]) -> Result where E: Error, { Ok(v.to_vec()) } fn visit_byte_buf(self, v: Vec) -> Result where E: Error, { Ok(v) } fn visit_str(self, v: &str) -> Result where E: Error, { Ok(v.as_bytes().to_vec()) } fn visit_string(self, v: String) -> Result where E: Error, { Ok(v.into_bytes()) } }