elastic-mapping-macro 0.1.0

Procedural macros for elastic-mapping - generate Elasticsearch mappings from Rust types
Documentation
use crate::{
    DocumentVariant, MappingProperty,
    codegen::{IterableProperties, gen_string_mapping},
    error::GeneratorError,
    rename::RenameCasing,
};

/// Generates mapping for internally tagged enums (#[serde(tag = "...")])
pub(crate) fn gen_internally_tagged_enum(
    tag: &str,
    variants: &[DocumentVariant],
    rename_all: Option<RenameCasing>,
) -> Result<(Vec<MappingProperty>, Vec<IterableProperties>), GeneratorError> {
    let mut properties = Vec::new();
    let mut iterable = Vec::new();

    // Add the tag field
    properties.push(MappingProperty {
        name: tag.to_string(),
        mapping: gen_string_mapping(),
    });

    // Process each variant's fields
    for variant in variants {
        match variant.fields.style {
            darling::ast::Style::Tuple => {
                // Tuples here are newtypes, use the fields of the type
                for field in &variant.fields.fields {
                    let ty = &field.ty;
                    iterable.push(IterableProperties {
                        source: quote::quote! {
                            <#ty as elastic_mapping::MappingType>::fields()
                        },
                    });
                }
            }
            darling::ast::Style::Struct => {
                // For struct variants pass each field
                for field in &variant.fields.fields {
                    properties.push(field.gen_mapping(rename_all));
                }
            }
            darling::ast::Style::Unit => {
                return Err(GeneratorError::Custom(
                    "Unit variants are not supported in tagged enums".to_string(),
                ));
            }
        }
    }

    Ok((properties, iterable))
}