-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
56 lines (50 loc) · 1.88 KB
/
lib.rs
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
use {
borsh_schema_derive_internal_0_10, borsh_schema_derive_internal_0_9,
proc_macro::TokenStream,
proc_macro2::{Ident, Span},
syn::{parse, Error, ItemEnum, ItemStruct, ItemUnion},
};
#[proc_macro_derive(BorshSchema0_9)]
pub fn borsh_schema_0_9(input: TokenStream) -> TokenStream {
use borsh_schema_derive_internal_0_9::{process_enum, process_struct};
let crate_name = Ident::new("borsh_0_9", Span::call_site());
let res = if let Ok(input) = parse::<ItemStruct>(input.clone()) {
process_struct(&input, crate_name)
} else if let Ok(input) = parse::<ItemEnum>(input.clone()) {
process_enum(&input, crate_name)
} else if parse::<ItemUnion>(input).is_ok() {
Err(Error::new(
Span::call_site(),
"Borsh schema does not support unions yet.",
))
} else {
// Derive macros can only be defined on structs, enums, and unions.
unreachable!()
};
TokenStream::from(match res {
Ok(res) => res,
Err(err) => err.to_compile_error(),
})
}
#[proc_macro_derive(BorshSchema0_10)]
pub fn borsh_schema_0_10(input: TokenStream) -> TokenStream {
use borsh_schema_derive_internal_0_10::{process_enum, process_struct};
let crate_name = Ident::new("borsh_0_10", Span::call_site());
let res = if let Ok(input) = parse::<ItemStruct>(input.clone()) {
process_struct(&input, crate_name)
} else if let Ok(input) = parse::<ItemEnum>(input.clone()) {
process_enum(&input, crate_name)
} else if parse::<ItemUnion>(input).is_ok() {
Err(Error::new(
Span::call_site(),
"Borsh schema does not support unions yet.",
))
} else {
// Derive macros can only be defined on structs, enums, and unions.
unreachable!()
};
TokenStream::from(match res {
Ok(res) => res,
Err(err) => err.to_compile_error(),
})
}