pub trait FormatSerializer {
type Error: Debug;
// Required methods
fn begin_struct(&mut self) -> Result<(), Self::Error>;
fn field_key(&mut self, key: &str) -> Result<(), Self::Error>;
fn end_struct(&mut self) -> Result<(), Self::Error>;
fn begin_seq(&mut self) -> Result<(), Self::Error>;
fn end_seq(&mut self) -> Result<(), Self::Error>;
fn scalar(&mut self, scalar: ScalarValue<'_>) -> Result<(), Self::Error>;
// Provided methods
fn field_metadata(&mut self, _field: &FieldItem) -> Result<(), Self::Error> { ... }
fn struct_metadata(&mut self, _shape: &Shape) -> Result<(), Self::Error> { ... }
fn preferred_field_order(&self) -> FieldOrdering { ... }
fn raw_serialize_shape(&self) -> Option<&'static Shape> { ... }
fn raw_scalar(&mut self, content: &str) -> Result<(), Self::Error> { ... }
}Expand description
Low-level serializer interface implemented by each format backend.
This is intentionally event-ish: the shared serializer logic owns traversal (struct/enum/seq decisions), while formats own representation details.
Required Associated Types§
Required Methods§
Sourcefn begin_struct(&mut self) -> Result<(), Self::Error>
fn begin_struct(&mut self) -> Result<(), Self::Error>
Begin a map/object/struct.
Sourcefn field_key(&mut self, key: &str) -> Result<(), Self::Error>
fn field_key(&mut self, key: &str) -> Result<(), Self::Error>
Emit a field key within a struct.
Sourcefn end_struct(&mut self) -> Result<(), Self::Error>
fn end_struct(&mut self) -> Result<(), Self::Error>
End a map/object/struct.
Provided Methods§
Sourcefn field_metadata(&mut self, _field: &FieldItem) -> Result<(), Self::Error>
fn field_metadata(&mut self, _field: &FieldItem) -> Result<(), Self::Error>
Optional: Provide field metadata before field_key is called. This allows formats like XML to extract namespace information. Default implementation does nothing.
Sourcefn struct_metadata(&mut self, _shape: &Shape) -> Result<(), Self::Error>
fn struct_metadata(&mut self, _shape: &Shape) -> Result<(), Self::Error>
Optional: Provide struct/enum type metadata when beginning to serialize it. This allows formats to extract container-level attributes like xml::ns_all. Default implementation does nothing.
Sourcefn preferred_field_order(&self) -> FieldOrdering
fn preferred_field_order(&self) -> FieldOrdering
Preferred field ordering for this format. Formats like XML can request attributes-first ordering to avoid buffering. Default is declaration order.
Sourcefn raw_serialize_shape(&self) -> Option<&'static Shape>
fn raw_serialize_shape(&self) -> Option<&'static Shape>
Returns the shape of the format’s raw capture type for serialization.
When serializing a value whose shape matches this, the serializer will
extract the inner string and call FormatSerializer::raw_scalar instead of normal
serialization.