rsgen-avro

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.
Command line usage
Install with:
Available options:
Usage:
rsgen-avro [options] <schema-file-pattern> <output-file>
rsgen-avro (-h | --help)
rsgen-avro (-V | --version)
Options:
--fmt Run rustfmt on the resulting <output-file>
--nullable Replace null fields with their default value when deserializing.
--precision=P Precision for f32/f64 default values that aren't round numbers [default: 3].
--variant-access Derive the traits in the variant_access_traits crate on union types.
--union-deser Custom deserialization for avro-rs multi-valued union types.
-V, --version Show version.
-h, --help Show this screen.
Library usage
As a libray, the basic usage is:
use ;
let raw_schema = r#"
{
"type": "record",
"name": "test",
"fields": [
{"name": "a", "type": "long", "default": 42},
{"name": "b", "type": "string"}
]
}
"#;
let source = SchemaStr;
let mut out = stdout;
let g = new.unwrap;
g.gen.unwrap;
This will generate the following output:
#[serde(default)]
#[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
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:
Note also that the Generator can be customized with a builder:
let g = builder.precision.build.unwrap;
Limitations
- Avro schema
namespacefields are ignored, therefore names from a single schema must not conflict.