avro_schema/schema/
mod.rs

1//! Contains structs defining Avro's logical types
2mod de;
3mod se;
4
5/// An Avro Schema. It describes all _physical_ and _logical_ types.
6/// See [the spec](https://avro.apache.org/docs/current/spec.html) for details.
7#[derive(Debug, Clone, PartialEq, Hash)]
8pub enum Schema {
9    /// A null type
10    Null,
11    /// Boolean (physically represented as a single byte)
12    Boolean,
13    /// 32 bit signed integer (physically represented as a zigzag encoded variable number of bytes)
14    Int(Option<IntLogical>),
15    /// 64 bit signed integer (physically represented as a zigzag encoded variable number of bytes)
16    Long(Option<LongLogical>),
17    /// 32 bit float (physically represented as 4 bytes in little endian)
18    Float,
19    /// 64 bit float (physically represented as 8 bytes in little endian)
20    Double,
21    /// variable length bytes (physically represented by a zigzag encoded positive integer followed by its number of bytes)
22    Bytes(Option<BytesLogical>),
23    /// variable length utf8 (physically represented by a zigzag encoded positive integer followed by its number of bytes)
24    String(Option<StringLogical>),
25    /// Record
26    Record(Record),
27    /// Enum with a known number of variants
28    Enum(Enum),
29    /// Array of a uniform type with N entries
30    Array(Box<Schema>),
31    /// A map String -> type
32    Map(Box<Schema>),
33    /// A union of a heterogeneous number of types
34    Union(Vec<Schema>),
35    /// todo
36    Fixed(Fixed),
37}
38
39/// Order of a [`Field`].
40#[derive(Debug, Clone, Copy, PartialEq, Hash)]
41pub enum Order {
42    /// Ascending order
43    Ascending,
44    /// Descending order
45    Descending,
46    /// Order is to be ignored
47    Ignore,
48}
49
50/// An Avro field.
51/// See [the spec](https://avro.apache.org/docs/current/spec.html) for details.
52#[derive(Debug, Clone, PartialEq, Hash)]
53pub struct Field {
54    /// Its name
55    pub name: String,
56    /// Its optional documentation
57    pub doc: Option<String>,
58    /// Its Schema
59    pub schema: Schema,
60    /// Its default value
61    pub default: Option<Schema>,
62    /// Its optional order
63    pub order: Option<Order>,
64    /// Its aliases
65    pub aliases: Vec<String>,
66}
67
68impl Field {
69    /// Returns a new [`Field`] without a doc, default, order or aliases
70    pub fn new<I: Into<String>>(name: I, schema: Schema) -> Self {
71        Self {
72            name: name.into(),
73            doc: None,
74            schema,
75            default: None,
76            order: None,
77            aliases: vec![],
78        }
79    }
80}
81
82/// Struct to hold data from a [`Schema::Record`].
83#[derive(Debug, Clone, PartialEq, Hash)]
84pub struct Record {
85    /// Its name
86    pub name: String,
87    /// Its optional namespace
88    pub namespace: Option<String>,
89    /// Its optional documentation
90    pub doc: Option<String>,
91    /// Its aliases
92    pub aliases: Vec<String>,
93    /// Its children fields
94    pub fields: Vec<Field>,
95}
96
97impl Record {
98    /// Returns a new [`Record`] without a namespace, doc or aliases
99    pub fn new<I: Into<String>>(name: I, fields: Vec<Field>) -> Self {
100        Self {
101            name: name.into(),
102            namespace: None,
103            doc: None,
104            fields,
105            aliases: vec![],
106        }
107    }
108}
109
110/// Struct to hold data from a [`Schema::Fixed`].
111#[derive(Debug, Clone, PartialEq, Hash)]
112pub struct Fixed {
113    /// Its name
114    pub name: String,
115    /// Its optional namespace
116    pub namespace: Option<String>,
117    /// Its optional documentation
118    pub doc: Option<String>,
119    /// Its aliases
120    pub aliases: Vec<String>,
121    /// Its size
122    pub size: usize,
123    /// Its optional logical type
124    pub logical: Option<FixedLogical>,
125}
126
127impl Fixed {
128    /// Returns a new [`Fixed`] without a namespace, doc or aliases
129    pub fn new<I: Into<String>>(name: I, size: usize) -> Self {
130        Self {
131            name: name.into(),
132            namespace: None,
133            doc: None,
134            size,
135            aliases: vec![],
136            logical: None,
137        }
138    }
139}
140
141/// Struct to hold data from a [`Schema::Enum`].
142#[derive(Debug, Clone, PartialEq, Hash)]
143pub struct Enum {
144    /// Its name
145    pub name: String,
146    /// Its optional namespace
147    pub namespace: Option<String>,
148    /// Its aliases
149    pub aliases: Vec<String>,
150    /// Its optional documentation
151    pub doc: Option<String>,
152    /// Its set of symbols
153    pub symbols: Vec<String>,
154    /// Its default symbol
155    pub default: Option<String>,
156}
157
158impl Enum {
159    /// Returns a minimal [`Enum`].
160    pub fn new<I: Into<String>>(name: I, symbols: Vec<String>) -> Self {
161        Self {
162            name: name.into(),
163            namespace: None,
164            doc: None,
165            symbols,
166            aliases: vec![],
167            default: None,
168        }
169    }
170}
171
172impl From<Enum> for Schema {
173    fn from(enum_: Enum) -> Self {
174        Schema::Enum(enum_)
175    }
176}
177
178impl From<Record> for Schema {
179    fn from(record: Record) -> Self {
180        Schema::Record(record)
181    }
182}
183
184impl From<Fixed> for Schema {
185    fn from(fixed: Fixed) -> Self {
186        Schema::Fixed(fixed)
187    }
188}
189
190/// Enum of all logical types of [`Schema::Int`]
191#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
192pub enum IntLogical {
193    /// A date
194    Date,
195    /// A time
196    Time,
197}
198
199/// Enum of all logical types of [`Schema::Long`]
200#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
201pub enum LongLogical {
202    /// A time
203    Time,
204    /// A timestamp
205    TimestampMillis,
206    /// A timestamp
207    TimestampMicros,
208    /// A timestamp without timezone
209    LocalTimestampMillis,
210    /// A timestamp without timezone
211    LocalTimestampMicros,
212}
213
214/// Enum of all logical types of [`Schema::String`]
215#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
216pub enum StringLogical {
217    /// A UUID
218    Uuid,
219}
220
221/// Enum of all logical types of [`Schema::Fixed`]
222#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
223pub enum FixedLogical {
224    /// A decimal
225    Decimal(usize, usize),
226    /// A duration
227    Duration,
228}
229
230/// Enum of all logical types of [`Schema::Bytes`]
231#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
232pub enum BytesLogical {
233    /// A decimal
234    Decimal(usize, usize),
235}