#avro #code-generation #serde

bin+lib rsgen-avro

Command line and library for generating Rust types from Avro schemas

35 releases

0.11.7 Nov 7, 2022
0.11.5 Sep 27, 2022
0.10.1 Jul 13, 2022
0.9.9 Jan 5, 2022
0.2.0 Nov 4, 2018

#135 in Command line utilities

Download history 192/week @ 2022-11-26 181/week @ 2022-12-03 283/week @ 2022-12-10 327/week @ 2022-12-17 160/week @ 2022-12-24 251/week @ 2022-12-31 269/week @ 2023-01-07 450/week @ 2023-01-14 403/week @ 2023-01-21 264/week @ 2023-01-28 319/week @ 2023-02-04 335/week @ 2023-02-11 337/week @ 2023-02-18 278/week @ 2023-02-25 280/week @ 2023-03-04 194/week @ 2023-03-11

1,132 downloads per month
Used in 2 crates

MIT license

76KB
1.5K SLoC

rsgen-avro   latest doc

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.

Command line usage

Download the latest release or install with:

cargo install rsgen-avro

Available options:

Usage:
  rsgen-avro [options] <schema-files-glob-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].
  --union-deser      Custom deserialization for apache-avro multi-valued union types.
  --derive-builders  Derive builders for generated record structs.
  --derive-schemas   Derive AvroSchema for generated record structs.
  -V, --version      Show version.
  -h, --help         Show this screen.

Library usage

As a libray, the basic usage is:

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:

#[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's .gen(..) method:

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:

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.

Limitations

  • Avro schema namespace fields are ignored, therefore names from a single schema must not conflict.

Dependencies

~11MB
~223K SLoC