rsgen-avro

Crates.io Docs MIT licensed

A command line tool and library for generating serde-compatible Rust types from Avro schemas. The avro-rs crate provides a way to read and write Avro data with such types.

Command line usage

Install with:

sh cargo install rsgen-avro

Available options:

``` Usage: rsgen-avro [options] rsgen-avro (-h | --help) rsgen-avro (-V | --version)

Options: --fmt Run rustfmt on the resulting --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]. --append Open in append mode. By default it is truncated. -V, --version Show version. -h, --help Show this screen. ```

Library usage

As a libray, the basic usage is:

```rust extern crate rsgen_avro;

use std::io::{stdout, Write}; use avrors::Schema; use rsgenavro::{Source, Generator};

let raw_schema = r#" { "type": "record", "name": "test", "fields": [ {"name": "a", "type": "long", "default": 42}, {"name": "b", "type": "string"} ] } "#;

let schema = Schema::parsestr(&rawschema).unwrap(); let source = Source::Schema(&schema);

let mut out: Box = Box::new(stdout());

let g = Generator::new().unwrap(); g.gen(&source, &mut out).unwrap(); ```

This will generate the following output:

```text use serde::{Deserialize, Serialize};

[serde(default)]

[derive(Debug, PartialEq, PartialOrd, Clone, Deserialize, 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:

rust pub enum Source<'a> { Schema(&'a Schema), SchemaStr(&'a str), FilePath(&'a Path), DirPath(&'a Path), }

Note also that the Generator can be customized with a builder:

rust let g = Generator::builder().precision(2).build().unwrap();

Limitations

Tests

Some tests capture stdout and must be run with only one thread:

sh cargo test -- --test-threads=1