oxblade 0.2.0

Laravel Blade templates for Rust: Blade's markup and directives with Rust expressions, rendering the same HTML as Laravel. Community package, not affiliated with Laravel.
Documentation
use serde::ser::{Error as _, Impossible, SerializeMap, Serializer};

use super::{Result, SerializeError, TreeSerializer};
use serde::Serialize;

pub(super) struct KeySerializer(pub(super) TreeSerializer);

impl KeySerializer {
    fn scalar(value: impl Serialize) -> Result<String> {
        // Delegate primitive spelling, including floating-point keys, to serde_json.
        let mut map = serde_json::value::Serializer.serialize_map(Some(1))?;
        map.serialize_key(&value)?;
        map.serialize_value(&())?;
        let serde_json::Value::Object(map) = map.end()? else {
            return Err(SerializeError::custom("expected a JSON object for map key"));
        };
        map.into_iter()
            .next()
            .map(|(key, _)| key)
            .ok_or_else(|| SerializeError::custom("missing JSON map key"))
    }
}

fn invalid<T>() -> Result<T> {
    Err(SerializeError::custom("key must be a string"))
}

macro_rules! scalar_keys {
    ($($method:ident($ty:ty)),* $(,)?) => {$(
        fn $method(self, value: $ty) -> Result<String> {
            Self::scalar(value)
        }
    )*};
}

macro_rules! invalid_keys {
    ($($method:ident($($arg:ident: $ty:ty),*) -> $output:ty),* $(,)?) => {$(
        fn $method(self, $($arg: $ty),*) -> Result<$output> {
            invalid()
        }
    )*};
}

impl Serializer for KeySerializer {
    type Ok = String;
    type Error = SerializeError;
    type SerializeSeq = Impossible<String, SerializeError>;
    type SerializeTuple = Self::SerializeSeq;
    type SerializeTupleStruct = Self::SerializeSeq;
    type SerializeTupleVariant = Self::SerializeSeq;
    type SerializeMap = Self::SerializeSeq;
    type SerializeStruct = Self::SerializeSeq;
    type SerializeStructVariant = Self::SerializeSeq;

    scalar_keys! {
        serialize_bool(bool), serialize_i8(i8), serialize_i16(i16), serialize_i32(i32),
        serialize_i64(i64), serialize_i128(i128), serialize_u8(u8), serialize_u16(u16),
        serialize_u32(u32), serialize_u64(u64), serialize_u128(u128), serialize_f32(f32),
        serialize_f64(f64), serialize_char(char), serialize_str(&str),
    }

    fn serialize_unit_variant(
        self,
        _: &'static str,
        _: u32,
        variant: &'static str,
    ) -> Result<String> {
        Ok(variant.to_owned())
    }

    fn serialize_newtype_struct<T: Serialize + ?Sized>(
        self,
        _: &'static str,
        value: &T,
    ) -> Result<String> {
        value.serialize(Self(self.0.descend(1)?))
    }

    fn serialize_some<T: Serialize + ?Sized>(self, _: &T) -> Result<String> {
        invalid()
    }

    fn serialize_newtype_variant<T: Serialize + ?Sized>(
        self,
        _: &'static str,
        _: u32,
        _: &'static str,
        _: &T,
    ) -> Result<String> {
        invalid()
    }

    invalid_keys! {
        serialize_bytes(_bytes: &[u8]) -> String,
        serialize_unit() -> String,
        serialize_none() -> String,
        serialize_unit_struct(_name: &'static str) -> String,
        serialize_seq(_len: Option<usize>) -> Self::SerializeSeq,
        serialize_tuple(_len: usize) -> Self::SerializeTuple,
        serialize_tuple_struct(_name: &'static str, _len: usize) -> Self::SerializeTupleStruct,
        serialize_tuple_variant(_name: &'static str, _index: u32, _variant: &'static str, _len: usize) -> Self::SerializeTupleVariant,
        serialize_map(_len: Option<usize>) -> Self::SerializeMap,
        serialize_struct(_name: &'static str, _len: usize) -> Self::SerializeStruct,
        serialize_struct_variant(_name: &'static str, _index: u32, _variant: &'static str, _len: usize) -> Self::SerializeStructVariant,
    }
}