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
use proc_macro2::{Span, TokenStream};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::quote;
use synstructure::{decl_derive, Structure};

decl_derive!([DagCbor, attributes(ipld)] => dag_cbor_derive);

mod ast;
mod attr;
mod gen;
mod parse;

fn dag_cbor_derive(s: Structure) -> TokenStream {
    let libipld = match use_crate("libipld") {
        Ok(ident) => ident,
        Err(error) => return error,
    };
    let ast = parse::parse(&s);
    let encode = gen::gen_encode(&ast, &libipld);
    let decode = gen::gen_decode(&ast, &libipld);
    quote! {
        #encode
        #decode
    }
}

/// Get the name of a crate based on its original name.
///
/// This works even if the crate was renamed in the `Cargo.toml` file. If the crate is not a
/// dependency, it will lead to a compile-time error.
fn use_crate(name: &str) -> Result<syn::Ident, TokenStream> {
    match crate_name(name) {
        Ok(FoundCrate::Name(n)) => Ok(syn::Ident::new(&n, Span::call_site())),
        Ok(FoundCrate::Itself) => Ok(syn::Ident::new("crate", Span::call_site())),
        Err(err) => Err(syn::Error::new(Span::call_site(), err).to_compile_error()),
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test() {
        let t = trybuild::TestCases::new();
        t.pass("examples/basic.rs");
        t.pass("examples/name_attr.rs");
        t.pass("examples/repr_attr.rs");
    }
}