Skip to content

Instantly share code, notes, and snippets.

@RGafiyatullin
Created March 15, 2019 00:39
Show Gist options
  • Save RGafiyatullin/4fec518e3cd8e3c6fe718ca976a4cdc5 to your computer and use it in GitHub Desktop.
Save RGafiyatullin/4fec518e3cd8e3c6fe718ca976a4cdc5 to your computer and use it in GitHub Desktop.
extern crate avro_rs;
#[macro_use]
extern crate log;
extern crate serde;
use serde::{Deserialize, Serialize};
use avro_rs::Schema;
type Res = Result<(), Box<dyn std::error::Error>>;
#[derive(Debug, Serialize, Deserialize)]
struct UnitStructure;
impl UnitStructure {
fn schema_raw() -> &'static str {
r#"
{"type": "null"}
"#
}
fn test() -> Res {
type T = UnitStructure;
let input = UnitStructure;
info!("==== UnitStructure === ");
let schema = Schema::parse_str(Self::schema_raw())?;
info!("schema: {:?}", schema);
info!("input: {:?}", input);
let avro = avro_rs::to_value(input)?;
info!("avro: {:?}", avro);
assert!(avro.validate(&schema));
let output = avro_rs::from_value::<T>(&avro)?;
info!("output: {:?}", output);
Ok(())
}
}
#[derive(Debug, Serialize, Deserialize)]
struct NewtypeStruct(bool, i32, String);
impl NewtypeStruct {
fn schema_raw() -> &'static str {
r#"
{"type": "array", "items": [
"boolean",
"int",
"string"
]}
"#
}
fn test() -> Res {
type T = NewtypeStruct;
let input = NewtypeStruct(true, 42, "hello".into());
info!("==== NewtypeStruct === ");
let schema = Schema::parse_str(Self::schema_raw())?;
info!("schema: {:?}", schema);
info!("input: {:?}", input);
let avro = avro_rs::to_value(input)?;
info!("avro: {:?}", avro);
// assert!(avro.validate(&schema));
let output = avro_rs::from_value::<T>(&avro)?;
info!("output: {:?}", output);
Ok(())
}
}
#[derive(Debug, Serialize, Deserialize)]
struct Record1 {
boolean: bool,
int: i32,
string: String,
unit_structure: UnitStructure,
}
impl Record1 {
fn schema_raw() -> &'static str {
r#"
{"type": "record", "name": "Record1", "fields": [
{"name": "boolean", "type": "boolean"},
{"name": "int", "type": "int"},
{"name": "string", "type": "string"},
{"name": "unit_structure", "type": "null"}
]}
"#
}
fn test() -> Res {
type T = Record1;
let input = Record1 {
boolean: true,
int: 42,
string: "hello".into(),
unit_structure: UnitStructure,
};
info!("==== Record1 === ");
let schema = Schema::parse_str(Self::schema_raw())?;
info!("schema: {:?}", schema);
info!("input: {:?}", input);
let avro = avro_rs::to_value(input)?;
info!("avro: {:?}", avro);
assert!(avro.validate(&schema));
let output = avro_rs::from_value::<T>(&avro)?;
info!("output: {:?}", output);
Ok(())
}
}
#[derive(Debug, Serialize, Deserialize)]
enum Enum {
UnitStructure,
NewtypeStruct(bool, i32, String),
Record1 {
boolean: bool,
int: i32,
string: String,
unit_structure: UnitStructure,
},
}
impl Enum {
fn schema_raw() -> &'static str {
r#"
[
{"type": "null"},
{"type": "array", "items": [
"boolean",
"int",
"string"
]},
{"type": "record", "name": "Record1", "fields": [
{"name": "boolean", "type": "boolean"},
{"name": "int", "type": "int"},
{"name": "string", "type": "string"},
{"name": "unit_structure", "type": "null"}
]}
]
"#
}
fn test() -> Res {
type T = Enum;
let inputs = vec![
Enum::Record1 {
boolean: true,
int: 42,
string: "hello".into(),
unit_structure: UnitStructure,
},
Enum::NewtypeStruct(true, 42, "hello".into()),
Enum::UnitStructure,
];
info!("==== Enum === ");
let schema = Schema::parse_str(Self::schema_raw())?;
info!("schema: {:?}", schema);
for input in inputs {
info!("input: {:?}", input);
let avro = avro_rs::to_value(input)?;
info!("avro: {:?}", avro);
// assert!(avro.validate(&schema));
let output_result = avro_rs::from_value::<T>(&avro);
info!("output: {:?}", output_result);
}
Ok(())
}
}
#[derive(Debug, Serialize, Deserialize)]
enum Enum2 {
Noop,
Hello { message: String, },
Bye { message: String, },
}
impl Enum2 {
fn schema_raw() -> &'static str {
r#"
[
{"type": "null"},
{"type": "record", "name": "Hello", "fields": [
{"name": "message", "type": "string"}
]},
{"type": "record", "name": "Bye", "fields": [
{"name": "message", "type": "string"}
]}
]
"#
}
fn test() -> Res {
type T = Enum2;
let inputs = vec![
Enum2::Hello { message: "Hai!".into(), },
Enum2::Noop,
Enum2::Bye { message: "Tschuss!".into(), },
];
info!("=== Enum2 ===");
let schema = Schema::parse_str(Self::schema_raw())?;
info!("schema: {:?}", schema);
for input in inputs {
info!("input: {:?}", input);
let avro = avro_rs::to_value(input)?;
info!("avro: {:?}", avro);
assert!(avro.validate(&schema));
let output_result = avro_rs::from_value::<T>(&avro);
info!("output: {:?}", output_result);
}
Ok(())
}
}
fn main() -> Res {
raffineria::use_dotenv();
raffineria::init()?;
UnitStructure::test()?;
NewtypeStruct::test()?;
Record1::test()?;
Enum::test()?;
Enum2::test()?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment