Skip to content

Command line and library for generating Rust types from Avro schemas

License

Notifications You must be signed in to change notification settings

0x003e/rsgen-avro

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rsgen-avro   latest doc

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:

cargo install rsgen-avro

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.
  --derive-builders Derive builders 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:

#[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:

pub enum Source<'a> {
    Schema(&'a avro_rs::Schema), // from re-exported `avro-rs` crate
    SchemaStr(&'a str),          // schema as a json string
    GlobPattern(&'a str),        // pattern to 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.

About

Command line and library for generating Rust types from Avro schemas

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%