A command line tool and library for generating serde-compatible Rust types from Avro schemas. The avro-rs crate, which is re-exported, provides a way to read and write Avro data with such types.
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 { pub a: i64, pub b: String, }
impl Default for Test { fn default() -> Test { Test { a: 42, b: String::default(), } } } ```
Various Schema
sources can be used with Generator
's .gen(..)
method:
rust
pub enum Source<'a> {
Schema(&'a avro_rs::Schema), // from re-exported `avro-rs` crate
SchemaStr(&'a str),
FilePath(&'a std::path::Path),
DirPath(&'a std::path::Path),
}
Note also that the Generator
can be customized with a builder:
rust
let g = Generator::builder().precision(2).build().unwrap();
namespace
fields are ignored, therefore names from a single schema must
not clash.