Skip to main content

apache_avro/serde/deser_schema/
tuple.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::{borrow::Borrow, io::Read};
19
20use serde::de::{DeserializeSeed, SeqAccess};
21
22use crate::{
23    Error, Schema,
24    schema::RecordSchema,
25    serde::deser_schema::{Config, SchemaAwareDeserializer},
26};
27
28pub struct OneTupleDeserializer<'s, 'r, R: Read, S: Borrow<Schema>> {
29    reader: &'r mut R,
30    schema: &'s Schema,
31    config: Config<'s, S>,
32    field_read: bool,
33}
34
35impl<'s, 'r, R: Read, S: Borrow<Schema>> OneTupleDeserializer<'s, 'r, R, S> {
36    pub fn new(
37        reader: &'r mut R,
38        schema: &'s Schema,
39        config: Config<'s, S>,
40    ) -> Result<Self, Error> {
41        let schema = if let Schema::Ref { name } = schema {
42            config.get_schema(name)?
43        } else {
44            schema
45        };
46        Ok(Self {
47            reader,
48            schema,
49            config,
50            field_read: false,
51        })
52    }
53}
54
55impl<'de, 's, 'r, R: Read, S: Borrow<Schema>> SeqAccess<'de>
56    for OneTupleDeserializer<'s, 'r, R, S>
57{
58    type Error = Error;
59
60    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
61    where
62        T: DeserializeSeed<'de>,
63    {
64        if self.field_read {
65            Ok(None)
66        } else {
67            let val = seed.deserialize(SchemaAwareDeserializer::new(
68                self.reader,
69                self.schema,
70                self.config,
71            )?)?;
72            self.field_read = true;
73            Ok(Some(val))
74        }
75    }
76
77    fn size_hint(&self) -> Option<usize> {
78        Some(1 - usize::from(self.field_read))
79    }
80}
81
82pub struct ManyTupleDeserializer<'s, 'r, R: Read, S: Borrow<Schema>> {
83    reader: &'r mut R,
84    schema: &'s RecordSchema,
85    config: Config<'s, S>,
86    current_field: usize,
87}
88
89impl<'s, 'r, R: Read, S: Borrow<Schema>> ManyTupleDeserializer<'s, 'r, R, S> {
90    pub fn new(reader: &'r mut R, schema: &'s RecordSchema, config: Config<'s, S>) -> Self {
91        Self {
92            reader,
93            schema,
94            config,
95            current_field: 0,
96        }
97    }
98}
99
100impl<'de, 's, 'r, R: Read, S: Borrow<Schema>> SeqAccess<'de>
101    for ManyTupleDeserializer<'s, 'r, R, S>
102{
103    type Error = Error;
104
105    fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
106    where
107        T: DeserializeSeed<'de>,
108    {
109        if self.current_field < self.schema.fields.len() {
110            let schema = &self.schema.fields[self.current_field].schema;
111            let val = seed.deserialize(SchemaAwareDeserializer::new(
112                self.reader,
113                schema,
114                self.config,
115            )?)?;
116            self.current_field += 1;
117            Ok(Some(val))
118        } else {
119            Ok(None)
120        }
121    }
122
123    fn size_hint(&self) -> Option<usize> {
124        Some(self.schema.fields.len() - self.current_field)
125    }
126}