Skip to main content

impl_adapter_builder

Macro impl_adapter_builder 

Source
macro_rules! impl_adapter_builder {
    (
        builder_name: $Builder:ident,
        adapter_name: $Adapter:ident,
        config_type: $Config:ty,
        adapter_type: $adapter_type_str:literal,
        adapter_desc: $adapter_desc:literal,
        task_type: $TaskType:ident,

        fields: {
            $($field_vis:vis $field_name:ident : $field_ty:ty = $field_default:expr),*
            $(,)?
        },

        methods: {
            $($method:item)*
        }

        build: $build_closure:expr,
    ) => { ... };
    (
        builder_name: $Builder:ident,
        adapter_name: $Adapter:ident,
        config_type: $Config:ty,
        adapter_type: $adapter_type_str:literal,
        adapter_desc: $adapter_desc:literal,
        task_type: $TaskType:ident,

        methods: {
            $($method:item)*
        }

        build: $build_closure:expr,
    ) => { ... };
    (
        builder_name: $Builder:ident,
        adapter_name: $Adapter:ident,
        config_type: $Config:ty,
        adapter_type: $adapter_type_str:literal,
        adapter_desc: $adapter_desc:literal,
        task_type: $TaskType:ident,

        fields: {
            $($field_vis:vis $field_name:ident : $field_ty:ty = $field_default:expr),*
            $(,)?
        }

        build: $build_closure:expr,
    ) => { ... };
    (
        builder_name: $Builder:ident,
        adapter_name: $Adapter:ident,
        config_type: $Config:ty,
        adapter_type: $adapter_type_str:literal,
        adapter_desc: $adapter_desc:literal,
        task_type: $TaskType:ident

        build: $build_closure:expr,
    ) => { ... };
    (
        builder_name: $Builder:ident,
        adapter_name: $Adapter:ident,
        config_type: $Config:ty,
        adapter_type: $adapter_type_str:literal,
        adapter_desc: $adapter_desc:literal,
        task_type: $TaskType:ident,

        fields: {
            $($field_vis:vis $field_name:ident : $field_ty:ty = $field_default:expr),*
            $(,)?
        },

        methods: {
            $($method:item)*
        }

        overrides: {
            with_config: $with_config_closure:expr,
            adapter_type: $adapter_type_closure:expr,
        }

        build: $build_closure:expr,
    ) => { ... };
    (
        builder_name: $Builder:ident,
        adapter_name: $Adapter:ident,
        config_type: $Config:ty,
        adapter_type: $adapter_type_str:literal,
        adapter_desc: $adapter_desc:literal,
        task_type: $TaskType:ident,

        fields: {
            $($field_vis:vis $field_name:ident : $field_ty:ty = $field_default:expr),*
            $(,)?
        },

        methods: {
            $($method:item)*
        }

        overrides: {
            with_config: $with_config_closure:expr,
        }

        build: $build_closure:expr,
    ) => { ... };
}
Expand description

Macro to implement common adapter builder boilerplate.

This macro generates the repetitive parts of adapter builders including the build() method. Uses a callback pattern to work around Rust macro hygiene limitations.

Generates:

  • Builder struct with config field plus custom fields
  • new() constructor
  • with_config() convenience method
  • Default trait implementation
  • OrtConfigurable trait implementation
  • AdapterBuilder trait implementation (with callback-based build())

§Syntax

impl_adapter_builder! {
    // Required: Type information
    builder_name: MyAdapterBuilder,
    adapter_name: MyAdapter,
    config_type: MyConfig,
    adapter_type: "MyAdapter",
    adapter_desc: "Description",
    task_type: MyTaskType,

    // Optional: Custom fields
    fields: {
        pub custom_field: Option<String> = None,
    },

    // Optional: Custom methods
    methods: {
        pub fn custom_method(mut self, value: String) -> Self {
            self.custom_field = Some(value);
            self
        }
    }

    // Required: Build closure (use |builder, model_path| { ... })
    build: |builder, model_path| {
        let (task_config, ort_config) = builder.config.into_validated_parts()?;
        let model = apply_ort_config!(
            SomeModelBuilder::new(),
            ort_config
        ).build(model_path)?;
        Ok(MyAdapter::new(model, task_config))
    }
}