use std::fmt;
use serde::de::{self, DeserializeOwned, Visitor};
use serde::ser::{self, Impossible};
use serde::{Deserialize, Serialize};
const TEXT: u8 = 1;
const BYTES: u8 = 2;
const JSON: u8 = 3;
const F64: u8 = 4;
const U64: u8 = 5;
const F32: u8 = 6;
fn tagged(tag: u8, body: &[u8]) -> Raw {
let mut row = Vec::with_capacity(body.len() + 1);
row.push(tag);
row.extend_from_slice(body);
Raw::Bytes(row)
}
fn kind_of(tag: u8) -> &'static str {
match tag {
TEXT => "text",
BYTES => "bytes",
JSON => "JSON",
F64 => "an f64",
U64 => "a u64",
F32 => "an f32",
_ => "bytes of no kind",
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Raw {
None,
Int(i64),
Bytes(Vec<u8>),
}
impl Raw {
pub(crate) fn len(&self) -> usize {
match self {
Raw::None => 0,
Raw::Int(_) => 8,
Raw::Bytes(bytes) => bytes.len(),
}
}
}
pub trait Value: Serialize + DeserializeOwned + Send + 'static {}
impl<T: Serialize + DeserializeOwned + Send + 'static> Value for T {}
#[derive(Clone, Default, PartialEq, Eq, Hash)]
pub struct Bytes(pub Vec<u8>);
impl fmt::Debug for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Bytes({} bytes)", self.0.len())
}
}
impl Serialize for Bytes {
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_bytes(&self.0)
}
}
impl<'de> Deserialize<'de> for Bytes {
fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_byte_buf(BytesVisitor)
}
}
struct BytesVisitor;
impl Visitor<'_> for BytesVisitor {
type Value = Bytes;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("bytes")
}
fn visit_bytes<E: de::Error>(self, bytes: &[u8]) -> Result<Bytes, E> {
Ok(Bytes(bytes.to_vec()))
}
fn visit_byte_buf<E: de::Error>(self, bytes: Vec<u8>) -> Result<Bytes, E> {
Ok(Bytes(bytes))
}
}
#[derive(Debug)]
pub(crate) struct Mismatch(pub(crate) String);
impl fmt::Display for Mismatch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for Mismatch {}
impl de::Error for Mismatch {
fn custom<T: fmt::Display>(message: T) -> Self {
Mismatch(message.to_string())
}
}
pub(crate) fn encode<V: Serialize>(value: &V) -> Result<Raw, Mismatch> {
match value.serialize(Probe) {
Ok(raw) => Ok(raw),
Err(Probed::Compound) => {
serde_json::to_vec(value).map(|json| tagged(JSON, &json)).map_err(|error| Mismatch(error.to_string()))
}
Err(Probed::Failed(mismatch)) => Err(mismatch),
}
}
pub(crate) fn decode<V: DeserializeOwned>(raw: &Raw) -> Result<V, Mismatch> {
V::deserialize(Row(raw))
}
struct Probe;
#[derive(Debug)]
enum Probed {
Compound,
Failed(Mismatch),
}
impl fmt::Display for Probed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Probed::Compound => f.write_str("a compound value"),
Probed::Failed(mismatch) => mismatch.fmt(f),
}
}
}
impl std::error::Error for Probed {}
impl ser::Error for Probed {
fn custom<T: fmt::Display>(message: T) -> Self {
Probed::Failed(Mismatch(message.to_string()))
}
}
macro_rules! compound {
($($method:ident($($arg:ty),*) -> $kind:ty;)*) => {
$(fn $method(self, $(_: $arg),*) -> Result<$kind, Probed> {
Err(Probed::Compound)
})*
};
}
impl ser::Serializer for Probe {
type Ok = Raw;
type Error = Probed;
type SerializeSeq = Impossible<Raw, Probed>;
type SerializeTuple = Impossible<Raw, Probed>;
type SerializeTupleStruct = Impossible<Raw, Probed>;
type SerializeTupleVariant = Impossible<Raw, Probed>;
type SerializeMap = Impossible<Raw, Probed>;
type SerializeStruct = Impossible<Raw, Probed>;
type SerializeStructVariant = Impossible<Raw, Probed>;
fn serialize_bool(self, value: bool) -> Result<Raw, Probed> {
Ok(Raw::Int(i64::from(value)))
}
fn serialize_i8(self, value: i8) -> Result<Raw, Probed> {
Ok(Raw::Int(value.into()))
}
fn serialize_i16(self, value: i16) -> Result<Raw, Probed> {
Ok(Raw::Int(value.into()))
}
fn serialize_i32(self, value: i32) -> Result<Raw, Probed> {
Ok(Raw::Int(value.into()))
}
fn serialize_i64(self, value: i64) -> Result<Raw, Probed> {
Ok(Raw::Int(value))
}
fn serialize_u8(self, value: u8) -> Result<Raw, Probed> {
Ok(Raw::Int(value.into()))
}
fn serialize_u16(self, value: u16) -> Result<Raw, Probed> {
Ok(Raw::Int(value.into()))
}
fn serialize_u32(self, value: u32) -> Result<Raw, Probed> {
Ok(Raw::Int(value.into()))
}
fn serialize_u64(self, value: u64) -> Result<Raw, Probed> {
Ok(tagged(U64, &value.to_be_bytes()))
}
fn serialize_f32(self, value: f32) -> Result<Raw, Probed> {
Ok(tagged(F32, &value.to_bits().to_be_bytes()))
}
fn serialize_f64(self, value: f64) -> Result<Raw, Probed> {
Ok(tagged(F64, &value.to_bits().to_be_bytes()))
}
fn serialize_char(self, value: char) -> Result<Raw, Probed> {
Ok(tagged(TEXT, value.to_string().as_bytes()))
}
fn serialize_str(self, value: &str) -> Result<Raw, Probed> {
Ok(tagged(TEXT, value.as_bytes()))
}
fn serialize_bytes(self, value: &[u8]) -> Result<Raw, Probed> {
Ok(tagged(BYTES, value))
}
fn serialize_none(self) -> Result<Raw, Probed> {
Ok(Raw::None)
}
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Raw, Probed> {
match value.serialize(self)? {
Raw::None => Err(Probed::Failed(Mismatch(
"a Some of nothing, a nested Option or a unit inside one, which no row tells from None".to_owned(),
))),
raw => Ok(raw),
}
}
fn serialize_unit(self) -> Result<Raw, Probed> {
Ok(Raw::None)
}
fn serialize_unit_struct(self, _: &'static str) -> Result<Raw, Probed> {
Ok(Raw::None)
}
fn serialize_newtype_struct<T: ?Sized + Serialize>(self, _: &'static str, value: &T) -> Result<Raw, Probed> {
value.serialize(self)
}
fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<Raw, Probed> {
Err(Probed::Compound)
}
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_: &'static str,
_: u32,
_: &'static str,
_: &T,
) -> Result<Raw, Probed> {
Err(Probed::Compound)
}
compound! {
serialize_seq(Option<usize>) -> Self::SerializeSeq;
serialize_tuple(usize) -> Self::SerializeTuple;
serialize_tuple_struct(&'static str, usize) -> Self::SerializeTupleStruct;
serialize_tuple_variant(&'static str, u32, &'static str, usize) -> Self::SerializeTupleVariant;
serialize_map(Option<usize>) -> Self::SerializeMap;
serialize_struct(&'static str, usize) -> Self::SerializeStruct;
serialize_struct_variant(&'static str, u32, &'static str, usize) -> Self::SerializeStructVariant;
}
}
struct Row<'de>(&'de Raw);
impl<'de> Row<'de> {
fn body(&self, tag: u8, wanted: &str) -> Result<&'de [u8], Mismatch> {
match self.0 {
Raw::Bytes(bytes) => match bytes.split_first() {
Some((&first, body)) if first == tag => Ok(body),
Some((&first, _)) => Err(Mismatch(format!("{wanted} from a row of {}", kind_of(first)))),
None => Err(Mismatch(format!("{wanted} from a row of no bytes"))),
},
other => Err(Mismatch(format!("{wanted} from a row holding {}", shown(other)))),
}
}
fn int(&self, wanted: &str) -> Result<i64, Mismatch> {
match self.0 {
Raw::Int(int) => Ok(*int),
other => Err(Mismatch(format!("{wanted} from a row holding {}", shown(other)))),
}
}
fn fixed<const N: usize>(&self, tag: u8, wanted: &str) -> Result<[u8; N], Mismatch> {
let bytes = self.body(tag, wanted)?;
bytes.try_into().map_err(|_| Mismatch(format!("{wanted} from {} bytes rather than {N}", bytes.len())))
}
fn text(&self) -> Result<&'de str, Mismatch> {
std::str::from_utf8(self.body(TEXT, "text")?).map_err(|_| Mismatch("text that is not UTF-8".to_owned()))
}
fn json<T>(
&self,
read: impl FnOnce(&mut serde_json::Deserializer<serde_json::de::SliceRead<'de>>) -> serde_json::Result<T>,
) -> Result<T, Mismatch> {
let mut json = serde_json::Deserializer::from_slice(self.body(JSON, "JSON")?);
let value = read(&mut json).map_err(|error| Mismatch(error.to_string()))?;
json.end().map_err(|error| Mismatch(error.to_string()))?;
Ok(value)
}
}
fn shown(raw: &Raw) -> &'static str {
match raw {
Raw::None => "nothing",
Raw::Int(_) => "an integer",
Raw::Bytes(_) => "bytes",
}
}
macro_rules! from_int {
($($method:ident,)*) => {
$(fn $method<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_i64(self.int("an integer")?)
})*
};
}
macro_rules! from_json {
($($method:ident($($arg:ident: $ty:ty),*);)*) => {
$(fn $method<V: Visitor<'de>>(self, $($arg: $ty,)* visitor: V) -> Result<V::Value, Mismatch> {
self.json(|json| de::Deserializer::$method(json, $($arg,)* visitor))
})*
};
}
impl<'de> de::Deserializer<'de> for Row<'de> {
type Error = Mismatch;
fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
match self.0 {
Raw::None => visitor.visit_unit(),
Raw::Int(int) => visitor.visit_i64(*int),
Raw::Bytes(bytes) => match bytes.first() {
Some(&TEXT) => visitor.visit_borrowed_str(self.text()?),
Some(&BYTES) => visitor.visit_borrowed_bytes(self.body(BYTES, "bytes")?),
Some(&F64) => visitor.visit_f64(f64::from_bits(u64::from_be_bytes(self.fixed(F64, "an f64")?))),
Some(&U64) => visitor.visit_u64(u64::from_be_bytes(self.fixed(U64, "a u64")?)),
Some(&F32) => visitor.visit_f32(f32::from_bits(u32::from_be_bytes(self.fixed(F32, "an f32")?))),
_ => self.json(|json| de::Deserializer::deserialize_any(json, visitor)),
},
}
}
fn deserialize_bool<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
match self.int("a bool")? {
0 => visitor.visit_bool(false),
1 => visitor.visit_bool(true),
other => Err(Mismatch(format!("a bool from the integer {other}"))),
}
}
from_int! {
deserialize_i8, deserialize_i16, deserialize_i32, deserialize_i64,
deserialize_u8, deserialize_u16, deserialize_u32,
}
fn deserialize_u64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_u64(u64::from_be_bytes(self.fixed(U64, "a u64")?))
}
fn deserialize_f32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_f32(f32::from_bits(u32::from_be_bytes(self.fixed(F32, "an f32")?)))
}
fn deserialize_f64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_f64(f64::from_bits(u64::from_be_bytes(self.fixed(F64, "an f64")?)))
}
fn deserialize_char<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_borrowed_str(self.text()?)
}
fn deserialize_str<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_borrowed_str(self.text()?)
}
fn deserialize_string<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_borrowed_str(self.text()?)
}
fn deserialize_bytes<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_borrowed_bytes(self.body(BYTES, "bytes")?)
}
fn deserialize_byte_buf<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_borrowed_bytes(self.body(BYTES, "bytes")?)
}
fn deserialize_option<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
match self.0 {
Raw::None => visitor.visit_none(),
_ => visitor.visit_some(self),
}
}
fn deserialize_unit<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Mismatch> {
match self.0 {
Raw::None => visitor.visit_unit(),
other => Err(Mismatch(format!("nothing from a row holding {}", shown(other)))),
}
}
fn deserialize_unit_struct<V: Visitor<'de>>(self, _: &'static str, visitor: V) -> Result<V::Value, Mismatch> {
self.deserialize_unit(visitor)
}
fn deserialize_newtype_struct<V: Visitor<'de>>(self, _: &'static str, visitor: V) -> Result<V::Value, Mismatch> {
visitor.visit_newtype_struct(self)
}
from_json! {
deserialize_seq();
deserialize_tuple(len: usize);
deserialize_tuple_struct(name: &'static str, len: usize);
deserialize_map();
deserialize_struct(name: &'static str, fields: &'static [&'static str]);
deserialize_enum(name: &'static str, variants: &'static [&'static str]);
deserialize_identifier();
deserialize_ignored_any();
}
}
#[cfg(test)]
#[path = "value_tests.rs"]
mod tests;