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

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

mod gen;

fn dag_cbor_derive(s: Structure) -> TokenStream {
    let encode = gen::encode(&s);
    let try_read_cbor = gen::decode(&s);
    s.gen_impl(quote! {
        use libipld::cbor::{DagCborCodec, Result};
        use libipld::error::Error;
        use libipld::cbor::encode::write_u64;
        use libipld::cbor::error::LengthOutOfRange;
        use libipld::cbor::decode::{read, read_u8, read_key, TryReadCbor};
        use libipld::codec::{Encode, Decode};
        use libipld::error::{TypeError, TypeErrorType};
        use std::io::{Read, Write};

        gen impl Encode<DagCborCodec> for @Self {
            #encode
        }

        gen impl TryReadCbor for @Self {
            #try_read_cbor
        }

        gen impl Decode<DagCborCodec> for @Self {
            fn decode<R: Read>(c: DagCborCodec, r: &mut R) -> Result<Self> {
                read(r)
            }
        }
    })
}

#[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");
    }
}