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.
Available options rsgen-avro --help
:
```text Generate Rust types from Avro schemas
Usage: rsgen-avro [OPTIONS]
Arguments:
Options:
--fmt Run rustfmt on the resulting 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
--derive-schemas Derive AvroSchema for generated record structs
-h, --help Print help
-V, --version Print version
``` 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 Note also that the See GeneratorBuilder documentation for all available options.Library usage
[derive(Debug, PartialEq, Eq, Clone, serde::Deserialize, serde::Serialize)]
[inline(always)]
Schema
sources can be used with Generator::gen(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
}
Generator
can be customized with a builder:rust
let gen = rsgen_avro::Generator::builder()
.precision(2)
.build()
.unwrap();
Limitations
namespace
fields are ignored, therefore record names within a schema
(and across schemas) must not conflict (i.e. must be unique).Option<T>
are supported through Avro unions having "null"
in their first
position only (See #39)