A command line tool and library for generating serde-compatible Rust types from Avro schemas. The apache-avro crate, which is re-exported, provides a way to read and write Avro data with such types.
Download the latest release or install with:
sh
cargo install rsgen-avro
Available options:
```
Usage:
rsgen-avro [options]
Options:
--fmt Run rustfmt on the resulting
As a libray, 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.gen(&source, &mut out).unwrap(); ```
This will generate the following output:
```text
pub struct Test { #[serde(default = "defaulttesta")] pub a: i64, pub b: String, }
fn defaulttesta() -> i64 { 42 } ```
Various Schema
sources can be used with Generator
's .gen(..)
method:
rust
pub enum Source<'a> {
Schema(&'a rsgen_avro::Schema), // Enum re-exported from `apache-avro`
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 g = Generator::builder().precision(2).build().unwrap();
The derive_builders
option will use the derive-builder crate to derive builders for the generated structs.
The builders will only be derived for those structs that are generated from Avro records.
namespace
fields are ignored, therefore names from a single schema must
not conflict.