weavatrix-refactor-plan 0.1.1

Evidence metadata, validation profiles, and canonical fingerprints for Weavatrix refactor plans
Documentation
//! Object member-name capture with the key shapes the previous encoder
//! accepted: strings, characters, and stringified numbers or booleans.

use super::text::{U64_TEXT_CAPACITY, format_u64};
use super::{CanonicalError, CanonicalSerializer};
use serde::{Serialize, ser};
use std::io;

pub(super) struct KeySink<'a, W> {
    serializer: &'a mut CanonicalSerializer<W>,
}

impl<'a, W: io::Write> KeySink<'a, W> {
    pub(super) const fn new(serializer: &'a mut CanonicalSerializer<W>) -> Self {
        Self { serializer }
    }

    #[expect(clippy::unnecessary_wraps, reason = "keeps serializer methods uniform")]
    fn accept_text(self, value: &str) -> Result<(), CanonicalError> {
        self.serializer.buf.extend_from_slice(value.as_bytes());
        Ok(())
    }

    #[expect(clippy::unnecessary_wraps, reason = "keeps serializer methods uniform")]
    fn accept_u64(self, value: u64) -> Result<(), CanonicalError> {
        let mut buffer = [0_u8; U64_TEXT_CAPACITY];
        self.serializer
            .buf
            .extend_from_slice(format_u64(value, &mut buffer));
        Ok(())
    }

    fn accept_i64(self, value: i64) -> Result<(), CanonicalError> {
        if value < 0 {
            self.serializer.buf.push(b'-');
        }
        self.accept_u64(value.unsigned_abs())
    }
}

impl<W: io::Write> ser::Serializer for KeySink<'_, W> {
    type Ok = ();
    type Error = CanonicalError;
    type SerializeSeq = ser::Impossible<(), CanonicalError>;
    type SerializeTuple = ser::Impossible<(), CanonicalError>;
    type SerializeTupleStruct = ser::Impossible<(), CanonicalError>;
    type SerializeTupleVariant = ser::Impossible<(), CanonicalError>;
    type SerializeMap = ser::Impossible<(), CanonicalError>;
    type SerializeStruct = ser::Impossible<(), CanonicalError>;
    type SerializeStructVariant = ser::Impossible<(), CanonicalError>;

    fn serialize_bool(self, value: bool) -> Result<(), CanonicalError> {
        self.accept_text(if value { "true" } else { "false" })
    }

    fn serialize_i8(self, value: i8) -> Result<(), CanonicalError> {
        self.accept_i64(i64::from(value))
    }

    fn serialize_i16(self, value: i16) -> Result<(), CanonicalError> {
        self.accept_i64(i64::from(value))
    }

    fn serialize_i32(self, value: i32) -> Result<(), CanonicalError> {
        self.accept_i64(i64::from(value))
    }

    fn serialize_i64(self, value: i64) -> Result<(), CanonicalError> {
        self.accept_i64(value)
    }

    fn serialize_u8(self, value: u8) -> Result<(), CanonicalError> {
        self.accept_u64(u64::from(value))
    }

    fn serialize_u16(self, value: u16) -> Result<(), CanonicalError> {
        self.accept_u64(u64::from(value))
    }

    fn serialize_u32(self, value: u32) -> Result<(), CanonicalError> {
        self.accept_u64(u64::from(value))
    }

    fn serialize_u64(self, value: u64) -> Result<(), CanonicalError> {
        self.accept_u64(value)
    }

    fn serialize_f32(self, value: f32) -> Result<(), CanonicalError> {
        self.serialize_f64(f64::from(value))
    }

    fn serialize_f64(self, value: f64) -> Result<(), CanonicalError> {
        if !value.is_finite() {
            return Err(CanonicalError::NonFiniteNumber);
        }
        let mut buffer = ryu_js::Buffer::new();
        self.accept_text(buffer.format_finite(value))
    }

    fn serialize_char(self, value: char) -> Result<(), CanonicalError> {
        let mut buffer = [0_u8; 4];
        self.accept_text(value.encode_utf8(&mut buffer))
    }

    fn serialize_str(self, value: &str) -> Result<(), CanonicalError> {
        self.accept_text(value)
    }

    fn serialize_bytes(self, _value: &[u8]) -> Result<(), CanonicalError> {
        Err(CanonicalError::InvalidKey)
    }

    fn serialize_none(self) -> Result<(), CanonicalError> {
        Err(CanonicalError::InvalidKey)
    }

    fn serialize_some<T>(self, _value: &T) -> Result<(), CanonicalError>
    where
        T: ?Sized + Serialize,
    {
        Err(CanonicalError::InvalidKey)
    }

    fn serialize_unit(self) -> Result<(), CanonicalError> {
        Err(CanonicalError::InvalidKey)
    }

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

    fn serialize_unit_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        variant: &'static str,
    ) -> Result<(), CanonicalError> {
        self.accept_text(variant)
    }

    fn serialize_newtype_struct<T>(
        self,
        _name: &'static str,
        value: &T,
    ) -> Result<(), CanonicalError>
    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<(), CanonicalError>
    where
        T: ?Sized + Serialize,
    {
        Err(CanonicalError::InvalidKey)
    }

    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, CanonicalError> {
        Err(CanonicalError::InvalidKey)
    }

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

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

    fn serialize_tuple_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant, CanonicalError> {
        Err(CanonicalError::InvalidKey)
    }

    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, CanonicalError> {
        Err(CanonicalError::InvalidKey)
    }

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

    fn serialize_struct_variant(
        self,
        _name: &'static str,
        _variant_index: u32,
        _variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant, CanonicalError> {
        Err(CanonicalError::InvalidKey)
    }
}