[][src]Crate rsgen_avro

rsgen-avro

Apache Avro is a data serialization system which provides rich data structures and a compact, fast, binary data format.

All data in Avro is schematized, as in the following example:

{
    "type": "record",
    "name": "test",
    "fields": [
        {"name": "a", "type": "long", "default": 42},
        {"name": "b", "type": "string"}
    ]
}

The avro-rs crate provides a way to read and write Avro data with both Avro-specialized and Rust serde-compatible types.

rsgen-avro provides a way to generate Rust serde-compatible types based on Avro schemas. Both a command line tool and library crate are available.

Using the library

Add to your Cargo.toml:

[dependencies]
rsgen-avro = "x.y.z"

Then, the basic usage is:

use std::io::{stdout, Write};
use avro_rs::Schema;
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 schema = Schema::parse_str(&raw_schema).unwrap();
let source = Source::Schema(&schema);

let mut out: Box<Write> = Box::new(stdout());

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

This will generate the following output:

use serde::{Deserialize, Serialize};

#[serde(default)]
#[derive(Debug, PartialEq, 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:

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:

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

Structs

Generator

The main component of this library. It is stateless can be reused many times.

GeneratorBuilder

A builder class to customize Generator.

RsgenError

Describes errors happened while generating Rust code.

Enums

Source

Represents a schema input source.