actix_telepathy_derive 0.4.0

Macros for cluster extension for the actix actor framework
Documentation
use log::*;
use proc_macro::TokenStream;
use quote::quote;
use serde_derive::{Deserialize, Serialize};
use std::fs::File;
use syn::{DeriveInput, Result, parse_macro_input};

const TELEPATHY_CONFIG_FILE: &str = "telepathy.yaml";
const WITH_SOURCE: &str = "with_source";

#[derive(Debug, Serialize, Deserialize)]
struct Config {
    pub custom_serializer: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            custom_serializer: "DefaultSerialization".to_string(),
        }
    }
}

fn load_config_yaml() -> Config {
    let f = File::open(TELEPATHY_CONFIG_FILE);
    match f {
        Ok(file_reader) => {
            serde_yaml::from_reader(file_reader).expect("Config file is no valid YAML")
        }
        Err(e) => {
            error!("{}, using default Config", e);
            Config::default()
        }
    }
}

pub fn remote_message_macro(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = &input.generics.split_for_impl();
    let s = name.to_string();
    let sources = get_with_source_attr(&input).expect("Expected correct syntax");

    let set_source = match sources.first() {
        Some(source) => {
            let attr = source.clone().unwrap();
            quote! {
                self.#attr.node.network_interface = Some(addr);
            }
        }
        None => quote! {},
    };

    let config: Config = load_config_yaml();
    let serializer = syn::parse_str::<syn::Type>(&config.custom_serializer).unwrap_or_else(|_| {
        panic!(
            "custom_serializer {} could not be found",
            &config.custom_serializer
        )
    });

    let expanded = quote! {
        use log::*;

        impl #impl_generics RemoteMessage for #name #ty_generics #where_clause {
            type Serializer = #serializer;
            const IDENTIFIER: &'static str = #s;

            fn get_serializer(&self) -> Box<Self::Serializer> {
                Box::new(#serializer {})
            }

            fn generate_serializer() -> Box<Self::Serializer> {
                Box::new(#serializer {})
            }

            fn set_source(&mut self, addr: Addr<NetworkInterface>) {
                #set_source
            }
        }

        impl #impl_generics Message for #name #ty_generics #where_clause {
            type Result = ();
        }
    };

    TokenStream::from(expanded)
}

fn get_with_source_attr(ast: &DeriveInput) -> Result<Vec<Option<syn::Type>>> {
    let attr = ast.attrs.iter().find_map(|attr| {
        if attr.path().is_ident(WITH_SOURCE) {
            attr.parse_args().ok()
        } else {
            None
        }
    });

    match attr {
        Some(a) => match a {
            syn::Meta::Path(path) => match path.get_ident() {
                Some(ident) => syn::parse_str::<syn::Type>(&ident.to_string())
                    .map(|ty| vec![Some(ty)])
                    .map_err(|_| syn::Error::new_spanned(ident, "Expect type")),
                None => Err(syn::Error::new_spanned(path, "Expect type")),
            },
            _ => Err(syn::Error::new_spanned(
                a,
                format!("The correct syntax is #[{}(<RemoteAddr>)]", WITH_SOURCE),
            )),
        },
        None => Ok(vec![]),
    }
}