crossflow_derive 0.0.6

Procedural macros for crossflow
Documentation
/*
 * Copyright (C) 2024 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

mod derive_buffer;
use derive_buffer::{impl_buffer_key_map, impl_joined_value};

mod derive_section;
use derive_section::impl_section;

mod derive_stream;
use derive_stream::impl_derive_stream;

mod derive_stream_pack;
use derive_stream_pack::impl_stream_pack;

use proc_macro::TokenStream;
use quote::quote;
use syn::{DeriveInput, ItemStruct, parse_macro_input};

#[proc_macro_derive(Stream)]
pub fn derive_stream(item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as ItemStruct);
    match impl_derive_stream(&input) {
        Ok(tokens) => tokens.into(),
        Err(msg) => quote! {
            compile_error!(#msg);
        }
        .into(),
    }
}

#[proc_macro_derive(DeliveryLabel)]
pub fn delivery_label_macro(item: TokenStream) -> TokenStream {
    let ast: DeriveInput = syn::parse(item).unwrap();
    let struct_name = &ast.ident;
    let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();

    quote! {
        impl #impl_generics ::crossflow::DeliveryLabel for #struct_name #type_generics #where_clause {
            fn dyn_debug(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                ::std::fmt::Debug::fmt(self, f)
            }

            fn as_dyn_eq(&self) -> &dyn ::crossflow::service_utils::DynEq {
                self
            }

            fn dyn_hash(&self, mut state: &mut dyn ::std::hash::Hasher) {
                let ty_id = ::std::any::TypeId::of::<Self>();
                ::std::hash::Hash::hash(&ty_id, &mut state);
                ::std::hash::Hash::hash(self, &mut state);
            }
        }
    }
    .into()
}

/// The result error is the compiler error message to be displayed.
type Result<T> = std::result::Result<T, String>;

#[proc_macro_derive(Joined, attributes(joined))]
pub fn derive_joined_value(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);
    match impl_joined_value(&input) {
        Ok(tokens) => tokens.into(),
        Err(msg) => quote! {
            compile_error!(#msg);
        }
        .into(),
    }
}

#[proc_macro_derive(Accessor, attributes(key))]
pub fn derive_buffer_key_map(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);
    match impl_buffer_key_map(&input) {
        Ok(tokens) => tokens.into(),
        Err(msg) => quote! {
            compile_error!(#msg);
        }
        .into(),
    }
}

#[proc_macro_derive(Section, attributes(message))]
pub fn derive_section(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);
    match impl_section(&input) {
        Ok(tokens) => tokens.into(),
        Err(msg) => quote! {
            compile_error!(#msg);
        }
        .into(),
    }
}

#[proc_macro_derive(StreamPack, attributes(stream))]
pub fn derive_stream_pack(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as ItemStruct);
    match impl_stream_pack(&input) {
        Ok(ok) => ok.into(),
        Err(msg) => quote! {
            compile_error!(#msg);
        }
        .into(),
    }
}