ot-tools-io-derive 0.2.0

Derive proc macros used in the `ot-tools-io` library crate.
Documentation
/*
SPDX-License-Identifier: GPL-3.0-or-later
Copyright © 2024 Mike Robeson [dijksterhuis]
*/

//! Derive macros for boilerplate trait implementation in the `ot_tools_io`
//! library crate

#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;

extern crate proc_macro;

use proc_macro::TokenStream;
use syn::DeriveInput;

/// Adds the `ot_tools_io::ReadableFileType` trait to a type -- indicating that
/// the type directly corresponds to a binary data file used by the Elektron Octatrack.
#[proc_macro_derive(ReadableFile)]
pub fn readable_file_type(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    // get the name of the type we want to implement the trait for
    let name = &input.ident;

    let expanded = quote! {
        impl crate::ReadableFileType for #name {}
    };
    TokenStream::from(expanded)
}

/// Macro to create derivable trait for the standard implementation of
/// `ot_tools_io::Encode` on a type (i.e. calling `bincode::serialise`).
#[proc_macro_derive(Encodeable)]
pub fn encode_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    // get the name of the type we want to implement the trait for
    let name = &input.ident;

    let expanded = quote! {
        impl crate::Encode for #name {}
    };
    TokenStream::from(expanded)
}

/// Macro to create derivable trait for the standard implementation of
/// `ot_tools_io::Decode` on a type (i.e. calling `bincode::deserialise`)
#[proc_macro_derive(Decodeable)]
pub fn decode_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    // get the name of the type we want to implement the trait for
    let name = &input.ident;

    let expanded = quote! {
        impl crate::Decode for #name {}
    };
    TokenStream::from(expanded)
}

/// Macro to create derivable trait for the standard implementation of
/// `ot_tools_io::DefaultsArray` on a type (i.e. an array with inferred
/// length based on type hints)
#[proc_macro_derive(DefaultsAsArray)]
pub fn defaults_as_array_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    // get the name of the type we want to implement the trait for
    let name = &input.ident;

    let expanded = quote! {
        impl crate::DefaultsArray for #name {}
    };
    TokenStream::from(expanded)
}

/// Macro to create derivable trait for the standard implementation of
/// `ot_tools_io::DefaultsBoxedArray` on a type (i.e. a Boxed
/// serde-big-array Array with inferred length based on type hints)
#[proc_macro_derive(DefaultsAsBoxedBigArray)]
pub fn defaults_as_boxed_array_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    // get the name of the type we want to implement the trait for
    let name = &input.ident;

    let expanded = quote! {
        impl DefaultsArrayBoxed for #name {}
    };
    TokenStream::from(expanded)
}