Skip to content
Myr

mod.rs

This file

Path

test_suite/tests/bytes/mod.rs

58 lines · 1.2 KiB · Rust

Last changed by

Not known.

History of this file

Take it

1use serde::de::{Deserializer, Error, SeqAccess, Visitor};2use std::fmt;34pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>5where6    D: Deserializer<'de>,7{8    deserializer.deserialize_byte_buf(ByteBufVisitor)9}1011struct ByteBufVisitor;1213impl<'de> Visitor<'de> for ByteBufVisitor {14    type Value = Vec<u8>;1516    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {17        formatter.write_str("byte array")18    }1920    fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>21    where22        V: SeqAccess<'de>,23    {24        let mut values = Vec::new();25        while let Some(value) = visitor.next_element()? {26            values.push(value);27        }28        Ok(values)29    }3031    fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>32    where33        E: Error,34    {35        Ok(v.to_vec())36    }3738    fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>39    where40        E: Error,41    {42        Ok(v)43    }4445    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>46    where47        E: Error,48    {49        Ok(v.as_bytes().to_vec())50    }5152    fn visit_string<E>(self, v: String) -> Result<Self::Value, E>53    where54        E: Error,55    {56        Ok(v.into_bytes())57    }58}