-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlib.rs
More file actions
110 lines (95 loc) · 3.31 KB
/
lib.rs
File metadata and controls
110 lines (95 loc) · 3.31 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
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
#![crate_type = "lib"]
#![recursion_limit = "128"]
#![deny(trivial_numeric_casts, warnings)]
#![allow(broken_intra_doc_links)]
#![allow(
clippy::too_many_arguments,
clippy::implicit_hasher,
clippy::module_inception,
clippy::new_without_default
)]
#![allow(unreachable_code)]
extern crate proc_macro;
mod derive_enum;
mod derive_struct;
mod util;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields, Path};
use self::derive_enum::{gen_read_capnp_enum, gen_write_capnp_enum};
use self::derive_struct::{gen_read_capnp_named_struct, gen_write_capnp_named_struct};
use self::util::{assign_defaults_path, extract_defaults, remove_with_attributes};
/// Generate code for conversion between Rust and capnp structs.
#[proc_macro_attribute]
pub fn capnp_conv(
args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
// See: https://github.com/dtolnay/syn/issues/86
// for information about arguments.
// Name of capnp struct:
let capnp_struct = parse_macro_input!(args as Path);
let input = parse_macro_input!(input as DeriveInput);
let defaults = extract_defaults(&input.generics);
let assign_defaults = |path: &mut syn::Path| assign_defaults_path(path, &defaults);
// Name of local struct:
let rust_struct = &input.ident;
let conversion = match input.data {
Data::Struct(ref data) => match data.fields {
Fields::Named(ref fields_named) => {
// Example:
// struct Point {
// x: u32,
// y: u32,
// }
let write_capnp = gen_write_capnp_named_struct(
fields_named,
rust_struct,
&capnp_struct,
&assign_defaults,
);
let read_capnp = gen_read_capnp_named_struct(
fields_named,
rust_struct,
&capnp_struct,
&assign_defaults,
);
quote! {
#[allow(clippy::all)]
#write_capnp
#[allow(clippy::all)]
#read_capnp
}
}
Fields::Unnamed(_) | Fields::Unit => unimplemented!(),
},
Data::Enum(ref data_enum) => {
// Example:
// enum MyEnum {
// Type1(u32),
// Type2,
// Type3(MyStruct),
// }
let write_capnp =
gen_write_capnp_enum(data_enum, rust_struct, &capnp_struct, &assign_defaults);
let read_capnp =
gen_read_capnp_enum(data_enum, rust_struct, &capnp_struct, &assign_defaults);
quote! {
#[allow(clippy::all)]
#write_capnp
#[allow(clippy::all)]
#read_capnp
}
}
Data::Union(_) => unimplemented!(),
};
// Remove all of our `#[capnp_conv(with = ... )]` attributes from the input:
let mut input = input;
remove_with_attributes(&mut input);
let expanded = quote! {
// Original structure
#input
// Generated mutual From conversion code:
#conversion
};
proc_macro::TokenStream::from(expanded)
}