serde_avro_fast 1.0.1

An idiomatic implementation of serde/avro (de)serialization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Defines a fully-safe counterpart of the [`Schema`](crate::Schema) that is
//! used for its initialization

mod canonical_form;
mod check_for_cycles;
mod parsing;
mod rabin;
mod serialize;

use super::{Fixed, Name, SchemaError};

pub use check_for_cycles::UnconditionalCycle;

/// An editable representation of an Avro schema
///
/// In there, references to other nodes are represented as [`SchemaKey`], which
/// allow to index into [`SchemaMut`].
///
/// It is useful to implement it this way because, due to how referencing via
/// [Names](https://avro.apache.org/docs/current/specification/#names) works in Avro,
/// the most performant representation of an Avro schema is not a tree but a
/// possibly-cyclic general directed graph.
///
/// For details about the meaning of the fields, see the
/// [`SchemaNode`](crate::schema::SchemaNode) documentation.
#[derive(Clone, Debug)]
pub struct SchemaMut {
	// First node in the array is considered to be the root
	pub(super) nodes: Vec<SchemaNode>,
	pub(super) schema_json: Option<String>,
}

impl SchemaMut {
	/// Obtain the underlying graph storage
	///
	/// The first node (index `0`) is the root of the schema.
	///
	/// [`SchemaKey`]s can be converted to indexes of this `Vec`.
	pub fn nodes(&self) -> &[SchemaNode] {
		&self.nodes
	}

	/// Obtain the underlying graph storage mutably
	///
	/// This loses the original JSON. If obtaining it again (for e.g. object
	/// container file encoding) it will be re-generated and will lose all
	/// non-stored schema fields (`doc`, `aliases`, `default`, ...).
	///
	/// The first node (index `0`) is the root of the schema.
	///
	/// [`SchemaKey`]s can be converted to/from indexes of this `Vec`.
	pub fn nodes_mut(&mut self) -> &mut Vec<SchemaNode> {
		self.schema_json = None;
		&mut self.nodes
	}

	/// Obtain the root of the Schema
	///
	/// It is the first node of the `nodes` `Vec`.
	///
	/// Panics if the `nodes` `Vec` is empty.
	/// This can only happen if you have updated it through
	/// [`nodes_mut`](Self::nodes_mut), as parsing otherwise guarantees that
	/// this cannot happen.
	pub fn root(&self) -> &SchemaNode {
		self.nodes.first().expect(
			"Schema should have nodes - have you updated it \
				in such a way that all of its nodes were removed?",
		)
	}

	/// Initialize a [`SchemaMut`] from a set of nodes.
	///
	/// The first node (index `0`) is the root of the schema.
	pub fn from_nodes(nodes: Vec<SchemaNode>) -> Self {
		Self {
			nodes,
			schema_json: None,
		}
	}

	/// Turn this [`SchemaMut`] into a [`Schema`](crate::Schema)
	///
	/// [`Schema`](crate::Schema) is necessary for use with the serializer and
	/// deserializer.
	///
	/// This will fail if the schema is invalid (e.g. incorrect [`SchemaKey`]`).
	pub fn freeze(self) -> Result<super::Schema, SchemaError> {
		self.try_into()
	}

	/// Try to get the node at the given [`SchemaKey`]
	///
	/// (or return `None` if the key is invalid)
	///
	/// If you want to panic on invalid keys, use `schema[key]`
	/// instead.
	pub fn get(&self, key: SchemaKey) -> Option<&SchemaNode> {
		self.nodes.get(key.idx)
	}
}

/// The location of a node in a [`SchemaMut`]
///
/// This can be used to [`Index`](std::ops::Index) into the [`SchemaMut`].
///
/// (Note that `Index`ing into a `SchemaMut` with an invalid index would cause a
/// panic.)
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SchemaKey {
	pub(super) idx: usize,
}

impl SchemaKey {
	/// Construct a new SchemaKey
	///
	/// This is expected to be an index in the [`nodes`](SchemaMut::nodes_mut)
	/// `Vec` of a [`SchemaMut`].
	///
	///
	/// (Note that [`Index`](std::ops::Index)ing into a `SchemaMut` with an
	/// invalid index would cause a panic.)
	pub const fn from_idx(idx: usize) -> Self {
		Self { idx }
	}
	/// Obtain the index in the [`nodes`](SchemaMut::nodes) `Vec` of a
	/// [`SchemaMut`] that this [`SchemaKey`] points to.
	pub const fn idx(self) -> usize {
		self.idx
	}
	/// Construct a new SchemaKey representing the root of the schema
	///
	/// This is equivalent to `SchemaKey::from_idx(0)`, since the root of the
	/// schema is always simply the first element of the `nodes` array.
	pub const fn root() -> Self {
		Self { idx: 0 }
	}
}
impl std::ops::Index<SchemaKey> for SchemaMut {
	type Output = SchemaNode;
	fn index(&self, key: SchemaKey) -> &Self::Output {
		&self.nodes[key.idx]
	}
}
impl std::fmt::Debug for SchemaKey {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		std::fmt::Debug::fmt(&self.idx, f)
	}
}

/// A node of an avro schema, stored in a [`SchemaMut`].
///
/// More information about Avro schemas can be found in the
/// [Avro Specification](https://avro.apache.org/docs/current/specification/).
///
/// In there, references to other nodes are represented as [`SchemaKey`], which
/// allow to index into [`SchemaMut`].
#[derive(Clone, Debug)]
pub enum SchemaNode {
	RegularType(RegularType),
	LogicalType {
		inner: SchemaKey,
		logical_type: LogicalType,
	},
}

/// A primitive or complex type of an avro schema, stored in a [`SchemaNode`].
///
/// More information about Avro schemas can be found in the
/// [Avro Specification](https://avro.apache.org/docs/current/specification/).
///
/// In there, references to other nodes are represented as [`SchemaKey`], which
/// allow to index into [`SchemaMut`].
#[derive(Clone, Debug)]
pub enum RegularType {
	/// A `null` Avro schema.
	Null,
	/// A `boolean` Avro schema.
	Boolean,
	/// An `int` Avro schema.
	Int,
	/// A `long` Avro schema.
	Long,
	/// A `float` Avro schema.
	Float,
	/// A `double` Avro schema.
	Double,
	/// A `bytes` Avro schema.
	/// `Bytes` represents a sequence of 8-bit unsigned bytes.
	Bytes,
	/// A `string` Avro schema.
	/// `String` represents a unicode character sequence.
	String,
	/// A `array` Avro schema. Avro arrays are required to have the same type
	/// for each element. This variant holds the `Schema` for the array element
	/// type.
	Array(Array),
	/// A `map` Avro schema.
	/// `Map` holds a pointer to the `Schema` of its values, which must all be
	/// the same schema. `Map` keys are assumed to be `string`.
	Map(Map),
	/// A `union` Avro schema.
	///
	/// These can be deserialized into rust enums, where the variant name
	/// should match:
	/// - If it's not a named type, the PascalCase of the type (e.g. `String`,
	///   `Uuid`...)
	/// - If it's a named type, the fully qualified name of the type (e.g for a
	///   record `{"namespace": "foo", "name": "bar"}`, `foo.bar`)
	///
	/// See the `tests/unions.rs` file for more examples.
	Union(Union),
	/// A `record` Avro schema.
	Record(Record),
	/// An `enum` Avro schema.
	///
	/// These can be deserialized into rust enums, matching on the name
	/// as defined in the schema.
	Enum(Enum),
	/// A `fixed` Avro schema.
	Fixed(Fixed),
}

/// Component of a [`SchemaMut`]
#[derive(Clone, Debug)]
pub struct Array {
	pub items: SchemaKey,
	pub(crate) _private: (),
}
impl Array {
	pub fn new(items: SchemaKey) -> Self {
		Self {
			items,
			_private: (),
		}
	}
}

/// Component of a [`SchemaMut`]
#[derive(Clone, Debug)]
pub struct Map {
	pub values: SchemaKey,
	pub(crate) _private: (),
}
impl Map {
	pub fn new(values: SchemaKey) -> Self {
		Self {
			values,
			_private: (),
		}
	}
}

/// Component of a [`SchemaMut`]
#[derive(Clone, Debug)]
pub struct Union {
	pub variants: Vec<SchemaKey>,
	pub(crate) _private: (),
}
impl Union {
	pub fn new(variants: Vec<SchemaKey>) -> Self {
		Self {
			variants,
			_private: (),
		}
	}
}

/// Component of a [`SchemaMut`]
#[derive(Clone, Debug)]
pub struct Record {
	pub fields: Vec<RecordField>,
	pub name: Name,
	pub(crate) _private: (),
}
impl Record {
	pub fn new(name: Name, fields: Vec<RecordField>) -> Self {
		Self {
			fields,
			name,
			_private: (),
		}
	}
}

/// Component of a [`SchemaMut`]
#[derive(Clone, Debug)]
pub struct RecordField {
	pub name: String,
	pub type_: SchemaKey,
	pub(crate) _private: (),
}
impl RecordField {
	pub fn new(name: impl Into<String>, schema: SchemaKey) -> Self {
		Self {
			name: name.into(),
			type_: schema,
			_private: (),
		}
	}
}

/// Component of a [`SchemaMut`]
#[derive(Clone, Debug)]
pub struct Enum {
	pub symbols: Vec<String>,
	pub name: Name,
	pub(crate) _private: (),
}
impl Enum {
	pub fn new(name: Name, symbols: Vec<String>) -> Self {
		Self {
			symbols,
			name,
			_private: (),
		}
	}
}

/// Logical type
///
/// <https://avro.apache.org/docs/current/specification/#logical-types>
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum LogicalType {
	/// Logical type which represents `Decimal` values. The underlying type is
	/// serialized and deserialized as `Schema::Bytes` or `Schema::Fixed`.
	///
	/// `scale` defaults to 0 and is an integer greater than or equal to 0 and
	/// `precision` is an integer greater than 0.
	///
	/// <https://avro.apache.org/docs/current/specification/#decimal>
	Decimal(Decimal),
	/// A universally unique identifier, annotating a string.
	Uuid,
	/// Logical type which represents the number of days since the unix epoch.
	/// Serialization format is `Schema::Int`.
	Date,
	/// The time of day in number of milliseconds after midnight with no
	/// reference any calendar, time zone or date in particular.
	///
	/// Annotates an [`Int`](RegularType::Int).
	TimeMillis,
	/// The time of day in number of microseconds after midnight with no
	/// reference any calendar, time zone or date in particular.
	///
	/// Annotates a [`Long`](RegularType::Long).
	TimeMicros,
	/// An instant in time represented as the number of milliseconds after the
	/// UNIX epoch.
	///
	/// Annotates a [`Long`](RegularType::Long).
	///
	/// You probably want to use
	/// [`TimestampMilliSeconds`](https://docs.rs/serde_with/latest/serde_with/struct.TimestampMilliSeconds.html)
	/// from [`serde_with`](https://docs.rs/serde_with/latest/serde_with/index.html#examples) when deserializing this.
	TimestampMillis,
	/// An instant in time represented as the number of microseconds after the
	/// UNIX epoch.
	///
	/// Annotates a [`Long`](RegularType::Long).
	///
	/// You probably want to use
	/// [`TimestampMicroSeconds`](https://docs.rs/serde_with/latest/serde_with/struct.TimestampMicroSeconds.html)
	/// from [`serde_with`](https://docs.rs/serde_with/latest/serde_with/index.html#examples) when deserializing this.
	TimestampMicros,
	/// An amount of time defined by a number of months, days and milliseconds.
	///
	/// This deserializes to a struct that has the `months`, `days`, and
	/// `milliseconds` fields declared as `u32`s, or to a `(u32, u32, u32)`
	/// tuple, or to its raw representation [as defined by the specification](https://avro.apache.org/docs/current/specification/#duration)
	/// if the deserializer is hinted this way ([`serde_bytes`](https://docs.rs/serde_bytes/latest/serde_bytes/)).
	Duration,
	/// A logical type that is not known or not handled in any particular way
	/// by this library.
	///
	/// **You should not match on this variant.** (See below.)
	///
	/// This is the string that is used in the schema JSON to refer to this
	/// logical type.
	///
	/// Logical types of this variant may turn into known logical types from one
	/// release to another, as new logical types get added, so you should not
	/// match on this variant, and if you need to check for a specific unknown
	/// logical type, you should use [`as_str`](Self::as_str) instead, as this
	/// is guaranteed to keep working from one release to another:
	///
	/// ```rust
	/// # use serde_avro_fast::schema::LogicalType;
	/// # let logical_type = LogicalType::Unknown(serde_avro_fast::schema::UnknownLogicalType::new("foo"));
	/// match logical_type {
	/// 	LogicalType::Uuid => { /* ... */ }
	/// 	LogicalType::TimestampMillis => { /* ... */ }
	/// 	_ => match logical_type.as_str() {
	/// 		"some-unknown-logical-type" => { /* ... */ }
	/// 		"some-other-unknown-logical-type" => { /* ... */ }
	/// 		_ => { /* ... */ }
	/// 	},
	/// }
	/// ```
	///
	/// However, you may construct an instance of this variant if you need to
	/// build a [`SchemaMut`] with a logical type that is not known to this
	/// library.
	Unknown(UnknownLogicalType),
}

/// Component of a [`SchemaMut`]
#[derive(Clone, Debug)]
pub struct Decimal {
	pub scale: u32,
	pub precision: usize,
	pub(crate) _private: (),
}
impl Decimal {
	pub fn new(scale: u32, precision: usize) -> Self {
		Self {
			precision,
			scale,
			_private: (),
		}
	}
}

/// Component of a [`SchemaMut`]
///
/// Represents a logical type that is not known or not handled in any particular
/// way by this library.
#[derive(Clone, Debug)]
pub struct UnknownLogicalType {
	pub logical_type_name: String,
	_private: (),
}
impl UnknownLogicalType {
	pub fn new(logical_type_name: impl Into<String>) -> Self {
		Self {
			logical_type_name: logical_type_name.into(),
			_private: (),
		}
	}

	pub fn as_str(&self) -> &str {
		&self.logical_type_name
	}
}

impl LogicalType {
	/// The name of the logical type
	///
	/// This is the string that is used in the schema JSON to refer to this
	/// logical type.
	///
	/// For example, the `Decimal` logical type is named `decimal`.
	pub fn as_str(&self) -> &str {
		match self {
			LogicalType::Decimal(_) => "decimal",
			LogicalType::Uuid => "uuid",
			LogicalType::Date => "date",
			LogicalType::TimeMillis => "time-millis",
			LogicalType::TimeMicros => "time-micros",
			LogicalType::TimestampMillis => "timestamp-millis",
			LogicalType::TimestampMicros => "timestamp-micros",
			LogicalType::Duration => "duration",
			LogicalType::Unknown(unknown_logical_type) => &unknown_logical_type.logical_type_name,
		}
	}
}

impl From<RegularType> for SchemaNode {
	fn from(regular_type: RegularType) -> Self {
		Self::RegularType(regular_type)
	}
}

macro_rules! impl_froms_for_regular_type {
	($($variant: ident)*) => {
		$(
			impl From<$variant> for RegularType {
				fn from(variant: $variant) -> Self {
					Self::$variant(variant)
				}
			}
			impl From<$variant> for SchemaNode {
				fn from(variant: $variant) -> Self {
					SchemaNode::RegularType(RegularType::$variant(variant))
				}
			}
		)*
	};
}
impl_froms_for_regular_type! { Array Map Union Record Enum Fixed }