AvroSchemaComponent

Trait AvroSchemaComponent 

Source
pub trait AvroSchemaComponent {
    // Required method
    fn get_schema_in_ctxt(
        named_schemas: &mut Names,
        enclosing_namespace: &Namespace,
    ) -> Schema;
}
Expand description

Trait for types that serve as fully defined components inside an Avro data model. Derive implementation available through derive feature. This is what is implemented by the derive(AvroSchema) macro.

§Implementation guide

§Simple implementation

To construct a non named simple schema, it is possible to ignore the input argument making the general form implementation look like

impl AvroSchemaComponent for AType {
    fn get_schema_in_ctxt(_: &mut Names, _: &Namespace) -> Schema {
       Schema::?
   }
}

§Passthrough implementation

To construct a schema for a Type that acts as in “inner” type, such as for smart pointers, simply pass through the arguments to the inner type

impl AvroSchemaComponent for PassthroughType {
    fn get_schema_in_ctxt(named_schemas: &mut Names, enclosing_namespace: &Namespace) -> Schema {
       InnerType::get_schema_in_ctxt(named_schemas, enclosing_namespace)
   }
}

§Complex implementation

To implement this for Named schema there is a general form needed to avoid creating invalid schemas or infinite loops.

impl AvroSchemaComponent for ComplexType {
    fn get_schema_in_ctxt(named_schemas: &mut Names, enclosing_namespace: &Namespace) -> Schema {
        // Create the fully qualified name for your type given the enclosing namespace
        let name =  apache_avro::schema::Name::new("MyName")
            .expect("Unable to parse schema name")
            .fully_qualified_name(enclosing_namespace);
        let enclosing_namespace = &name.namespace;
        // Check, if your name is already defined, and if so, return a ref to that name
        if named_schemas.contains_key(&name) {
            apache_avro::schema::Schema::Ref{name: name.clone()}
        } else {
            named_schemas.insert(name.clone(), apache_avro::schema::Schema::Ref{name: name.clone()});
            // YOUR SCHEMA DEFINITION HERE with the name equivalent to "MyName".
            // For non-simple sub types delegate to their implementation of AvroSchemaComponent
        }
   }
}

Required Methods§

Source

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl AvroSchemaComponent for bool

Source§

impl AvroSchemaComponent for char

Source§

impl AvroSchemaComponent for f32

Source§

impl AvroSchemaComponent for f64

Source§

impl AvroSchemaComponent for i8

Source§

impl AvroSchemaComponent for i16

Source§

impl AvroSchemaComponent for i32

Source§

impl AvroSchemaComponent for i64

Source§

impl AvroSchemaComponent for i128

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

The schema is Schema::Fixed of size 16 with the name i128.

Source§

impl AvroSchemaComponent for str

Source§

impl AvroSchemaComponent for u8

Source§

impl AvroSchemaComponent for u16

Source§

impl AvroSchemaComponent for u32

Source§

impl AvroSchemaComponent for u64

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

The schema is Schema::Fixed of size 8 with the name u64.

Source§

impl AvroSchemaComponent for u128

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

The schema is Schema::Fixed of size 16 with the name u128.

Source§

impl AvroSchemaComponent for String

Source§

impl AvroSchemaComponent for Duration

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

The schema is Schema::Duration with the name duration.

This is a lossy conversion as this Avro type does not store the amount of nanoseconds.

Source§

impl<T> AvroSchemaComponent for Cow<'_, T>

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for Option<T>

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for &T

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for &mut T

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for [T]

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for Map<String, T>

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for Box<T>

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for Vec<T>

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for HashMap<String, T>

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<T> AvroSchemaComponent for Mutex<T>

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Source§

impl<const N: usize, T> AvroSchemaComponent for [T; N]

Source§

fn get_schema_in_ctxt( named_schemas: &mut Names, enclosing_namespace: &Namespace, ) -> Schema

Implementors§