forked from ipld/libipld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
76 lines (67 loc) · 2.38 KB
/
Copy pathlib.rs
File metadata and controls
76 lines (67 loc) · 2.38 KB
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
#![deny(warnings)]
use anyhow::anyhow;
use proc_macro2::{Span, TokenStream};
use proc_macro_crate::{crate_name, FoundCrate};
use quote::{quote, ToTokens};
use synstructure::{decl_derive, Structure};
decl_derive!([DagCbor, attributes(ipld)] => dag_cbor_derive);
decl_derive!([DagCborInternal, attributes(ipld)] => dag_cbor_derive_internal);
mod ast;
mod attr;
mod gen;
mod parse;
// Entry point for the DagCbor derive macro
fn dag_cbor_derive(s: Structure) -> TokenStream {
let libipld_core = match use_crate("libipld") {
Ok(ident) => ident,
Err(error) => return error,
};
let libipld_cbor = quote!(#libipld_core::cbor);
let ast = parse::parse(&s);
let encode = gen::gen_encode(&ast, &libipld_core, &libipld_cbor);
let decode = gen::gen_decode(&ast, &libipld_core, &libipld_cbor);
quote! {
#encode
#decode
}
}
// Entry point for the DagCborCrate derive macro
// This variant of the macro may be used within libipld itself
// as it uses the API exposed by the sub-crates within the workspace directly
// instead of the API exposed by the top level libipld crate.
fn dag_cbor_derive_internal(s: Structure) -> TokenStream {
let libipld_core = quote!(libipld_core);
let libipld_cbor = quote!(libipld_cbor);
let ast = parse::parse(&s);
let encode = gen::gen_encode(&ast, &libipld_core, &libipld_cbor);
let decode = gen::gen_decode(&ast, &libipld_core, &libipld_cbor);
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<TokenStream, TokenStream> {
match crate_name(name) {
Ok(FoundCrate::Name(n)) => Ok(syn::Ident::new(&n, Span::call_site()).to_token_stream()),
Ok(FoundCrate::Itself) => Err(syn::Error::new(
Span::call_site(),
anyhow!("unsupported use of dag-cbor-derive macro from within libipld crate"),
)
.to_compile_error()),
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");
}
}