use serde::ser::{self, Serialize};
pub(super) const PDF_NUMBER_BOUND: f32 = 2_147_483_648.0;
const UNWRITTEN_FIELDS: [(&str, &str); 8] = [
("FlowPaint", "items"),
("FlowPaint", "lines"),
("PlanePaint", "items"),
("BoxDecoration", "radii"),
("BoxDecoration", "corner_shapes"),
("BoxDecoration", "outline"),
("FilterPaint", "functions"),
("BackdropPaint", "functions"),
];
const CALCULATION_VARIANT: &str = "Calc";
pub(super) fn unwritable_number(value: &impl Serialize) -> Option<f64> {
match value.serialize(Scan) {
Ok(()) => None,
Err(Unwritable(number)) => Some(number),
}
}
#[derive(Debug)]
struct Unwritable(f64);
impl std::fmt::Display for Unwritable {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "unwritable number {}", self.0)
}
}
impl std::error::Error for Unwritable {}
impl ser::Error for Unwritable {
fn custom<T: std::fmt::Display>(_: T) -> Self {
Self(f64::NAN)
}
}
struct Scan;
fn number(value: f64) -> Result<(), Unwritable> {
if value.is_finite() && value.abs() < f64::from(PDF_NUMBER_BOUND) {
Ok(())
} else {
Err(Unwritable(value))
}
}
macro_rules! accept {
($($method:ident($type:ty)),* $(,)?) => {
$(fn $method(self, _: $type) -> Result<(), Unwritable> { Ok(()) })*
};
}
impl ser::Serializer for Scan {
type Ok = ();
type Error = Unwritable;
type SerializeSeq = Self;
type SerializeTuple = Self;
type SerializeTupleStruct = Self;
type SerializeTupleVariant = Self;
type SerializeMap = Self;
type SerializeStruct = Fields;
type SerializeStructVariant = Fields;
accept!(
serialize_bool(bool),
serialize_i8(i8),
serialize_i16(i16),
serialize_i32(i32),
serialize_i64(i64),
serialize_u8(u8),
serialize_u16(u16),
serialize_u32(u32),
serialize_u64(u64),
serialize_char(char),
serialize_str(&str),
serialize_bytes(&[u8]),
serialize_unit_struct(&'static str),
);
fn serialize_f32(self, value: f32) -> Result<(), Unwritable> {
number(f64::from(value))
}
fn serialize_f64(self, value: f64) -> Result<(), Unwritable> {
number(value)
}
fn serialize_none(self) -> Result<(), Unwritable> {
Ok(())
}
fn serialize_some<T: Serialize + ?Sized>(self, value: &T) -> Result<(), Unwritable> {
value.serialize(self)
}
fn serialize_unit(self) -> Result<(), Unwritable> {
Ok(())
}
fn serialize_unit_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
) -> Result<(), Unwritable> {
Ok(())
}
fn serialize_newtype_struct<T: Serialize + ?Sized>(
self,
_: &'static str,
value: &T,
) -> Result<(), Unwritable> {
value.serialize(self)
}
fn serialize_newtype_variant<T: Serialize + ?Sized>(
self,
_: &'static str,
_: u32,
variant: &'static str,
value: &T,
) -> Result<(), Unwritable> {
if variant == CALCULATION_VARIANT {
return Ok(());
}
value.serialize(self)
}
fn serialize_seq(self, _: Option<usize>) -> Result<Self, Unwritable> {
Ok(self)
}
fn serialize_tuple(self, _: usize) -> Result<Self, Unwritable> {
Ok(self)
}
fn serialize_tuple_struct(self, _: &'static str, _: usize) -> Result<Self, Unwritable> {
Ok(self)
}
fn serialize_tuple_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
_: usize,
) -> Result<Self, Unwritable> {
Ok(self)
}
fn serialize_map(self, _: Option<usize>) -> Result<Self, Unwritable> {
Ok(self)
}
fn serialize_struct(self, name: &'static str, _: usize) -> Result<Fields, Unwritable> {
Ok(Fields(name))
}
fn serialize_struct_variant(
self,
name: &'static str,
_: u32,
_: &'static str,
_: usize,
) -> Result<Fields, Unwritable> {
Ok(Fields(name))
}
}
macro_rules! elements {
($($trait:ident::$method:ident),* $(,)?) => {
$(impl ser::$trait for Scan {
type Ok = ();
type Error = Unwritable;
fn $method<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Unwritable> {
value.serialize(Scan)
}
fn end(self) -> Result<(), Unwritable> {
Ok(())
}
})*
};
}
elements!(
SerializeSeq::serialize_element,
SerializeTuple::serialize_element,
SerializeTupleStruct::serialize_field,
SerializeTupleVariant::serialize_field,
);
struct Fields(&'static str);
macro_rules! fields {
($($trait:ident),* $(,)?) => {
$(impl ser::$trait for Fields {
type Ok = ();
type Error = Unwritable;
fn serialize_field<T: Serialize + ?Sized>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Unwritable> {
if UNWRITTEN_FIELDS.contains(&(self.0, key)) {
return Ok(());
}
value.serialize(Scan)
}
fn end(self) -> Result<(), Unwritable> {
Ok(())
}
})*
};
}
fields!(SerializeStruct, SerializeStructVariant);
impl ser::SerializeMap for Scan {
type Ok = ();
type Error = Unwritable;
fn serialize_key<T: Serialize + ?Sized>(&mut self, key: &T) -> Result<(), Unwritable> {
key.serialize(Scan)
}
fn serialize_value<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Unwritable> {
value.serialize(Scan)
}
fn end(self) -> Result<(), Unwritable> {
Ok(())
}
}