Kriskras99 commented on code in PR #512: URL: https://github.com/apache/avro-rs/pull/512#discussion_r2969321379
########## avro/src/serde/ser_schema/tuple.rs: ########## @@ -0,0 +1,283 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::{borrow::Borrow, io::Write}; + +use serde::{ + Serialize, + ser::{SerializeTuple, SerializeTupleStruct, SerializeTupleVariant}, +}; + +use super::{Config, SchemaAwareSerializer, union::UnionSerializer}; +use crate::{ + Error, Schema, + error::Details, + schema::{RecordSchema, UnionSchema}, +}; + +#[expect( + private_interfaces, + reason = "One{,Union}TupleSerializer should not be used directly" +)] +pub enum TupleSerializer<'s, 'w, W: Write, S: Borrow<Schema>> { + Unit(usize), + One(OneTupleSerializer<'s, 'w, W, S>), + /// This exists because we can't create a `&Schema::Union` from a `&UnionSchema` + OneUnion(OneUnionTupleSerializer<'s, 'w, W, S>), + Many(ManyTupleSerializer<'s, 'w, W, S>), +} + +impl<'s, 'w, W: Write, S: Borrow<Schema>> TupleSerializer<'s, 'w, W, S> { + pub fn unit(bytes_written: Option<usize>) -> Self { + Self::Unit(bytes_written.unwrap_or(0)) + } + + pub fn one( + writer: &'w mut W, + schema: &'s Schema, + config: Config<'s, S>, + bytes_written: Option<usize>, + ) -> Self { + Self::One(OneTupleSerializer::new( + writer, + schema, + config, + bytes_written, + )) + } + + pub fn one_union(writer: &'w mut W, union: &'s UnionSchema, config: Config<'s, S>) -> Self { + Self::OneUnion(OneUnionTupleSerializer::new(writer, union, config)) + } + + pub fn many( + writer: &'w mut W, + schema: &'s RecordSchema, + config: Config<'s, S>, + bytes_written: Option<usize>, + ) -> Self { + Self::Many(ManyTupleSerializer::new( + writer, + schema, + config, + bytes_written, + )) + } +} + +impl<'s, 'w, W: Write, S: Borrow<Schema>> SerializeTuple for TupleSerializer<'s, 'w, W, S> { + type Ok = usize; + type Error = Error; + + fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ?Sized + Serialize, + { + match self { + TupleSerializer::Unit(_) => Err(Error::new(Details::SerializeValueWithSchema { + value_type: "tuple", + value: "Expected no elements for the unit tuple".into(), + schema: Schema::Null, + })), + TupleSerializer::One(one) => one.serialize_element(value), + TupleSerializer::OneUnion(one) => one.serialize_element(value), + TupleSerializer::Many(many) => many.serialize_element(value), + } + } + + fn end(self) -> Result<Self::Ok, Self::Error> { + match self { + TupleSerializer::Unit(bytes_written) => Ok(bytes_written), + TupleSerializer::One(one) => one.end(), + TupleSerializer::OneUnion(one) => one.end(), + TupleSerializer::Many(many) => SerializeTuple::end(many), + } + } +} + +pub struct ManyTupleSerializer<'s, 'w, W: Write, S: Borrow<Schema>> { + writer: &'w mut W, + schema: &'s RecordSchema, + config: Config<'s, S>, + field_position: usize, + bytes_written: usize, +} + +impl<'s, 'w, W: Write, S: Borrow<Schema>> ManyTupleSerializer<'s, 'w, W, S> { + pub fn new( + writer: &'w mut W, + schema: &'s RecordSchema, + config: Config<'s, S>, + bytes_written: Option<usize>, + ) -> Self { + Self { + writer, + schema, + config, + field_position: 0, + bytes_written: bytes_written.unwrap_or(0), + } + } +} + +impl<'s, 'w, W: Write, S: Borrow<Schema>> SerializeTuple for ManyTupleSerializer<'s, 'w, W, S> { + type Ok = usize; + type Error = Error; + + fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error> + where + T: ?Sized + Serialize, + { + let schema = &self.schema.fields[self.field_position].schema; + self.bytes_written += value.serialize(SchemaAwareSerializer::new( + self.writer, + schema, + self.config, + )?)?; + self.field_position += 1; + Ok(()) + } + + fn end(self) -> Result<Self::Ok, Self::Error> { + assert_eq!(self.field_position, self.schema.fields.len()); Review Comment: Converted to an error, originally chose a panic because this can only happen for invalid Serialize implementations. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
