1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
extern crate proc_macro;

use ctor::ctor;
use darling::FromMeta;
use proc_macro2::TokenStream;
use quote::quote;
use syn::AttributeArgs;

mod attr;
mod call_fn;
mod call_impl;
mod ext;
mod function;
mod ptr_type;
mod return_type;

use attr::invoke::InvokeParams;
use ext::*;

#[proc_macro_attribute]
pub fn marshal(
    params: proc_macro::TokenStream,
    function: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let params = syn::parse_macro_input!(params as AttributeArgs);

    let params = match InvokeParams::from_list(&params) {
        Ok(v) => v,
        Err(err) => return err.write_errors().into(),
    };

    match call_with(params, function.into()) {
        Ok(tokens) => tokens.into(),
        Err(err) => proc_macro::TokenStream::from(
            syn::Error::new(err.span(), err.to_string()).to_compile_error(),
        ),
    }
}

#[ctor]
fn init() {
    pretty_env_logger::init();
}

// fn json_output_path() -> PathBuf {
//     std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("cthulhu.json")
// }

// fn is_exporting() -> bool {
//     let target = std::env::var("CTHULHU_PKG").ok();

//     if let Some(target) = target {
//         let name = match std::env::var("CARGO_PKG_NAME").ok() {
//             Some(v) => v,
//             None => return false,
//         };
//         let version = match std::env::var("CARGO_PKG_VERSION").ok() {
//             Some(v) => v,
//             None => return false,
//         };
//         format!("{}-{}", name, version) == target
//     } else {
//         false
//     }
// }

// pub(crate) struct Context {
//     pkg_name: String,
//     pkg_version: String,
//     cargo_manifest_dir: PathBuf,
// }

// impl Default for Context {
//     fn default() -> Context {
//         Context {
//             pkg_name:
//         }
//         std::env::var("CARGO_PKG_NAME")
//         std::env::var("CARGO_PKG_VERSION")
//         std::env::var("CARGO_MANIFEST_DIR")
//     }
// }

fn call_with(invoke_params: InvokeParams, item: TokenStream) -> Result<TokenStream, syn::Error> {
    // if let Some(value) = invoke_params.send_help.as_ref() {
    //     log::debug!("HELP REQUESTED: {}", value);
    //     return Ok(item);
    // }

    // log::debug!("{:?} {:?} {:?}", std::env::var("CARGO_PKG_NAME"), std::env::var("CARGO_PKG_VERSION"), std::env::var("CARGO_MANIFEST_DIR"));

    let item: syn::Item = syn::parse2(item.clone()).context("error parsing function body")?;
    let result = match item {
        syn::Item::Fn(item) => call_fn::call_with_function(
            invoke_params.return_marshaler,
            invoke_params.callback,
            item,
            None,
        ),
        syn::Item::Impl(item) => call_impl::call_with_impl(invoke_params.prefix, item),
        item => {
            log::error!("{:?}", &item);
            Err(syn::Error::new_spanned(
                &item,
                "Only supported on functions and impls",
            ))
        }
    };

    if result.is_err() {
        log::debug!("macro finished with error");
    } else {
        log::debug!("macro finished successfully");
    }

    result
}

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

pub(crate) fn default_marshaler(ty: &syn::Type) -> Option<syn::Path> {
    DEFAULT_MARSHALERS
        .get(&*quote! { #ty }.to_string())
        .and_then(|x| syn::parse_str(x).ok())
}

pub(crate) fn is_passthrough_type(ty: &syn::Type) -> bool {
    match ty {
        syn::Type::BareFn(bare_fn) => bare_fn.abi.is_some(),
        _ => PASSTHROUGH_TYPES.contains(&&*quote! { #ty }.to_string()),
    }
}