pinecone 0.2.5

A no_std + serde compatible message library for Rust
Documentation
use serde::{ser, Serialize};

use crate::error::{Error, Result};
use crate::ser::output::SerOutput;
use crate::varint::VarintUsize;

/// A `serde` compatible serializer
pub struct Serializer<F>
where
    F: SerOutput,
{
    pub(crate) output: F,
}

impl<'a, F> ser::Serializer for &'a mut Serializer<F>
where
    F: SerOutput,
{
    type Ok = ();

    type Error = Error;

    // Associated types for keeping track of additional state while serializing
    // compound data structures like sequences and maps. In this case no
    // additional state is required beyond what is already stored in the
    // Serializer struct.
    type SerializeSeq = Self;
    type SerializeTuple = Self;
    type SerializeTupleStruct = Self;
    type SerializeTupleVariant = Self;
    type SerializeMap = Self;
    type SerializeStruct = Self;
    type SerializeStructVariant = Self;

    fn serialize_bool(self, v: bool) -> Result<()> {
        self.serialize_u8(if v { 1 } else { 0 })
    }

    fn serialize_i8(self, v: i8) -> Result<()> {
        self.serialize_u8(v.to_le_bytes()[0])
    }

    fn serialize_i16(self, v: i16) -> Result<()> {
        self.output
            .try_extend(&v.to_le_bytes())
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_i32(self, v: i32) -> Result<()> {
        self.output
            .try_extend(&v.to_le_bytes())
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_i64(self, v: i64) -> Result<()> {
        self.output
            .try_extend(&v.to_le_bytes())
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_u8(self, v: u8) -> Result<()> {
        self.output
            .try_push(v)
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_u16(self, v: u16) -> Result<()> {
        self.output
            .try_extend(&v.to_le_bytes())
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_u32(self, v: u32) -> Result<()> {
        self.output
            .try_extend(&v.to_le_bytes())
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_u64(self, v: u64) -> Result<()> {
        self.output
            .try_extend(&v.to_le_bytes())
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_f32(self, v: f32) -> Result<()> {
        self.output
            .try_extend(&v.to_le_bytes())
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_f64(self, v: f64) -> Result<()> {
        self.output
            .try_extend(&v.to_le_bytes())
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_char(self, v: char) -> Result<()> {
        self.serialize_u32(v as u32)
    }

    fn serialize_str(self, v: &str) -> Result<()> {
        VarintUsize(v.len()).serialize(&mut *self)?;
        self.output
            .try_extend(v.as_bytes())
            .map_err(|_| Error::SerializeBufferFull)?;
        Ok(())
    }

    fn serialize_bytes(self, v: &[u8]) -> Result<()> {
        self.output
            .try_extend(v)
            .map_err(|_| Error::SerializeBufferFull)
    }

    fn serialize_none(self) -> Result<()> {
        self.serialize_u8(0)
    }

    fn serialize_some<T>(self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        self.serialize_u8(1)?;
        value.serialize(self)
    }

    fn serialize_unit(self) -> Result<()> {
        Ok(())
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
        Ok(())
    }

    fn serialize_unit_variant(
        self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
    ) -> Result<()> {
        VarintUsize(variant_index as usize).serialize(self)
    }

    fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(self)
    }

    fn serialize_newtype_variant<T>(
        self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
        value: &T,
    ) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        VarintUsize(variant_index as usize).serialize(&mut *self)?;
        value.serialize(self)
    }

    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
        VarintUsize(len.ok_or(Error::SerializeLengthUnknown)?).serialize(&mut *self)?;
        Ok(self)
    }

    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
        Ok(self)
    }

    fn serialize_tuple_struct(
        self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleStruct> {
        Ok(self)
    }

    fn serialize_tuple_variant(
        self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant> {
        VarintUsize(variant_index as usize).serialize(&mut *self)?;
        Ok(self)
    }

    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
        VarintUsize(len.ok_or(Error::SerializeLengthUnknown)?).serialize(&mut *self)?;
        Ok(self)
    }

    fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
        Ok(self)
    }

    fn serialize_struct_variant(
        self,
        _name: &'static str,
        variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant> {
        VarintUsize(variant_index as usize).serialize(&mut *self)?;
        Ok(self)
    }

    fn collect_str<T: ?Sized>(self, _value: &T) -> Result<Self::Ok>
    where
        T: core::fmt::Display,
    {
        unreachable!()
    }
}

impl<'a, F> ser::SerializeSeq for &'a mut Serializer<F>
where
    F: SerOutput,
{
    // Must match the `Ok` type of the serializer.
    type Ok = ();
    // Must match the `Error` type of the serializer.
    type Error = Error;

    // Serialize a single element of the sequence.
    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    // Close the sequence.
    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a, F> ser::SerializeTuple for &'a mut Serializer<F>
where
    F: SerOutput,
{
    type Ok = ();
    type Error = Error;

    fn serialize_element<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a, F> ser::SerializeTupleStruct for &'a mut Serializer<F>
where
    F: SerOutput,
{
    type Ok = ();
    type Error = Error;

    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a, F> ser::SerializeTupleVariant for &'a mut Serializer<F>
where
    F: SerOutput,
{
    type Ok = ();
    type Error = Error;

    fn serialize_field<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a, F> ser::SerializeMap for &'a mut Serializer<F>
where
    F: SerOutput,
{
    type Ok = ();
    type Error = Error;

    fn serialize_key<T>(&mut self, key: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        key.serialize(&mut **self)
    }

    fn serialize_value<T>(&mut self, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a, F> ser::SerializeStruct for &'a mut Serializer<F>
where
    F: SerOutput,
{
    type Ok = ();
    type Error = Error;

    fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}

impl<'a, F> ser::SerializeStructVariant for &'a mut Serializer<F>
where
    F: SerOutput,
{
    type Ok = ();
    type Error = Error;

    fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<()>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<()> {
        Ok(())
    }
}