#avro-schema #codegen #avro #serde

bin+lib rsgen-avro

Command line and library for generating Rust types from Avro schemas

58 releases (18 breaking)

Uses new Rust 2024

0.19.1 Nov 30, 2025
0.18.3 Jul 24, 2025
0.16.1 Mar 16, 2025
0.15.3 Oct 27, 2024
0.2.0 Nov 4, 2018

#180 in Encoding

Download history 2437/week @ 2025-08-27 2464/week @ 2025-09-03 2090/week @ 2025-09-10 2480/week @ 2025-09-17 3854/week @ 2025-09-24 5191/week @ 2025-10-01 4663/week @ 2025-10-08 4620/week @ 2025-10-15 4769/week @ 2025-10-22 3879/week @ 2025-10-29 3457/week @ 2025-11-05 3344/week @ 2025-11-12 4157/week @ 2025-11-19 4704/week @ 2025-11-26 4144/week @ 2025-12-03 6028/week @ 2025-12-10

19,653 downloads per month
Used in 2 crates

MIT license

110KB
2.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.

Available options rsgen-avro --help:

Generate Rust types from Avro schemas

Usage: rsgen-avro [OPTIONS] <GLOB_PATTERN> <OUTPUT_FILE>

Arguments:
  <GLOB_PATTERN>  Glob pattern to select Avro schema files
  <OUTPUT_FILE>   The file where Rust types will be written, '-' for stdout

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
      --chrono-dates             Use chrono::NaiveDateTime for date/timestamps logical types
      --derive-builders          Derive builders for generated record structs
      --impl-schemas <METHOD>    Implement AvroSchema for generated record structs [default: none] [possible values: derive, copy-build-schema, none]
      --extra-derives <DERIVES>  Extract Derives for generated record structs, comma separated, e.g. `std::fmt::Display,std::string::ToString`
  -h, --help                     Print help (see more with '--help')
  -V, --version                  Print version

Library usage

As a library, 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.generate(&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::generate(source, output) 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 generator = rsgen_avro::Generator::builder()
    .precision(2)
    .build()
    .unwrap();

See GeneratorBuilder documentation for all available options.

Limitations

  • Avro schema namespace fields are ignored, therefore record names within a schema (and across schemas) must not conflict (i.e. must be unique).
  • Rust Option<T> are supported through Avro unions having "null" in their first position only (See #39)

Dependencies

~13–20MB
~407K SLoC