https://github.com/lerouxrgd/rsgen-avro
Command line and library for generating Rust types from Avro schemas
https://github.com/lerouxrgd/rsgen-avro
avro-schema code-generation command-line rust
Last synced: 4 months ago
JSON representation
Command line and library for generating Rust types from Avro schemas
- Host: GitHub
- URL: https://github.com/lerouxrgd/rsgen-avro
- Owner: lerouxrgd
- License: mit
- Created: 2018-10-03T20:16:36.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2026-01-14T23:46:58.000Z (6 months ago)
- Last Synced: 2026-01-17T17:51:04.939Z (6 months ago)
- Topics: avro-schema, code-generation, command-line, rust
- Language: Rust
- Homepage:
- Size: 487 KB
- Stars: 44
- Watchers: 3
- Forks: 33
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rsgen-avro [![latest]][crates.io] [![doc]][docs.rs]
[latest]: https://img.shields.io/crates/v/rsgen-avro.svg
[crates.io]: https://crates.io/crates/rsgen-avro
[doc]: https://docs.rs/rsgen-avro/badge.svg
[docs.rs]: https://docs.rs/rsgen-avro
A command line tool and library for generating [serde][]-compatible Rust types from
[Avro schemas][schemas]. The [apache-avro][] crate, which is re-exported, provides a way to
read and write Avro data with such types.
## Command line usage
Download the latest [release](https://github.com/lerouxrgd/rsgen-avro/releases).
Available options `rsgen-avro --help`:
```text
Generate Rust types from Avro schemas
Usage: rsgen-avro [OPTIONS]
Arguments:
Glob pattern to select Avro schema files
The file where Rust types will be written, '-' for stdout
Options:
--fmt Run rustfmt on the resulting
--nullable Replace null fields with their default value when deserializing
--precision
Precision for f32/f64 default values that aren't round numbers [default: 3]
--union-deser Custom deserialization for apache-avro multi-valued union types
--chrono-dates Use chrono::NaiveDateTime for date/timestamps logical types
--derive-builders Derive builders for generated record structs
--impl-schemas Implement AvroSchema for generated record structs [default: none] [possible values: derive, copy-build-schema, none]
--extra-derives Extract Derives for generated record structs, comma separated, e.g. `std::fmt::Display,std::string::ToString`
-h, --help Print help (see more with '--help')
-V, --version Print version
```
## Library usage
As a library, the basic usage is:
```rust
use rsgen_avro::{Source, Generator};
let raw_schema = r#"
{
"type": "record",
"name": "test",
"fields": [
{"name": "a", "type": "long", "default": 42},
{"name": "b", "type": "string"}
]
}
"#;
let source = Source::SchemaStr(&raw_schema);
let mut out = std::io::stdout();
let g = Generator::new().unwrap();
g.generate(&source, &mut out).unwrap();
```
This will generate the following output:
```text
#[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
pub struct Test {
#[serde(default = "default_test_a")]
pub a: i64,
pub b: String,
}
#[inline(always)]
fn default_test_a() -> i64 { 42 }
```
Various `Schema` sources can be used with `Generator::generate(source, output)` method:
```rust
pub enum Source<'a> {
Schema(&'a rsgen_avro::Schema), // Avro schema enum re-exported from `apache-avro`
Schemas(&'a [rsgen_avro::Schema]), // A slice of Avro schema enums
SchemaStr(&'a str), // Schema as a json string
GlobPattern(&'a str), // Glob pattern to select schema files
}
```
Note also that the `Generator` can be customized with a builder:
```rust
let generator = rsgen_avro::Generator::builder()
.precision(2)
.build()
.unwrap();
```
See [GeneratorBuilder][gen-builder-doc] documentation for all available options.
[gen-builder-doc]: https://docs.rs/rsgen-avro/latest/rsgen_avro/struct.GeneratorBuilder.html
## Limitations
- Avro schema `namespace` fields are ignored, therefore record names within a schema
(and across schemas) must not conflict (i.e. must be unique).
- Rust `Option` are supported through Avro unions having `"null"` in their first
position only (See [#39](https://github.com/lerouxrgd/rsgen-avro/issues/39))
[schemas]: https://avro.apache.org/docs/current/spec.html
[apache-avro]: https://github.com/apache/avro/tree/master/lang/rust
[serde]: https://serde.rs