-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmod.rs
220 lines (183 loc) · 6.96 KB
/
mod.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::ops::Deref;
use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{DataEnum, Error};
use crate::{
attrs::common::ContainerAttributes,
codegen::{
common::{
format_container_from_ident, Container, ContainerInput, ContainerVersion, Item,
VersionedContainer,
},
venum::variant::VersionedVariant,
},
};
pub(crate) mod variant;
/// Stores individual versions of a single enum. Each version tracks variant
/// actions, which describe if the variant was added, renamed or deprecated in
/// that version. Variants which are not versioned, are included in every
/// version of the enum.
#[derive(Debug)]
pub(crate) struct VersionedEnum(VersionedContainer<VersionedVariant>);
impl Deref for VersionedEnum {
type Target = VersionedContainer<VersionedVariant>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Container<DataEnum, VersionedVariant> for VersionedEnum {
fn new(
input: ContainerInput,
data: DataEnum,
attributes: ContainerAttributes,
) -> syn::Result<Self> {
let ContainerInput {
original_attributes,
visibility,
ident,
} = input;
// Convert the raw version attributes into a container version.
let versions: Vec<_> = (&attributes).into();
// Extract the attributes for every variant from the raw token
// stream and also validate that each variant action version uses a
// version declared by the container attribute.
let mut items = Vec::new();
for variant in data.variants {
let mut versioned_field = VersionedVariant::new(variant, &attributes)?;
versioned_field.insert_container_versions(&versions);
items.push(versioned_field);
}
// Check for field ident collisions
for version in &versions {
// Collect the idents of all variants for a single version and then
// ensure that all idents are unique. If they are not, return an
// error.
// TODO (@Techassi): Report which variant(s) use a duplicate ident and
// also hint what can be done to fix it based on the variant action /
// status.
if !items.iter().map(|f| f.get_ident(version)).all_unique() {
return Err(Error::new(
ident.span(),
format!("Enum contains renamed variants which collide with other variants in version {version}", version = version.inner),
));
}
}
let from_ident = format_container_from_ident(&ident);
Ok(Self(VersionedContainer {
skip_from: attributes
.options
.skip
.map_or(false, |s| s.from.is_present()),
original_attributes,
visibility,
from_ident,
versions,
items,
ident,
}))
}
fn generate_tokens(&self) -> TokenStream {
let mut token_stream = TokenStream::new();
let mut versions = self.versions.iter().peekable();
while let Some(version) = versions.next() {
token_stream.extend(self.generate_version(version, versions.peek().copied()));
}
token_stream
}
}
impl VersionedEnum {
fn generate_version(
&self,
version: &ContainerVersion,
next_version: Option<&ContainerVersion>,
) -> TokenStream {
let mut token_stream = TokenStream::new();
let original_attributes = &self.original_attributes;
let visibility = &self.visibility;
let enum_name = &self.ident;
// Generate variants of the enum for `version`.
let variants = self.generate_enum_variants(version);
// TODO (@Techassi): Make the generation of the module optional to
// enable the attribute macro to be applied to a module which
// generates versioned versions of all contained containers.
let version_ident = &version.ident;
let deprecated_note = format!("Version {version} is deprecated", version = version_ident);
let deprecated_attr = version
.deprecated
.then_some(quote! {#[deprecated = #deprecated_note]});
let mut version_specific_docs = TokenStream::new();
for (i, doc) in version.version_specific_docs.iter().enumerate() {
if i == 0 {
// Prepend an empty line to clearly separate the version
// specific docs.
version_specific_docs.extend(quote! {
#[doc = ""]
})
}
version_specific_docs.extend(quote! {
#[doc = #doc]
})
}
// Generate tokens for the module and the contained enum
token_stream.extend(quote! {
#[automatically_derived]
#deprecated_attr
#visibility mod #version_ident {
#(#original_attributes)*
#version_specific_docs
pub enum #enum_name {
#variants
}
}
});
// Generate the From impl between this `version` and the next one.
if !self.skip_from && !version.skip_from {
token_stream.extend(self.generate_from_impl(version, next_version));
}
token_stream
}
fn generate_enum_variants(&self, version: &ContainerVersion) -> TokenStream {
let mut token_stream = TokenStream::new();
for variant in &self.items {
token_stream.extend(variant.generate_for_container(version));
}
token_stream
}
fn generate_from_impl(
&self,
version: &ContainerVersion,
next_version: Option<&ContainerVersion>,
) -> TokenStream {
if let Some(next_version) = next_version {
let next_module_name = &next_version.ident;
let module_name = &version.ident;
let from_ident = &self.from_ident;
let enum_ident = &self.ident;
let mut variants = TokenStream::new();
for item in &self.items {
variants.extend(item.generate_for_from_impl(
module_name,
next_module_name,
version,
next_version,
enum_ident,
))
}
// TODO (@Techassi): Be a little bit more clever about when to include
// the #[allow(deprecated)] attribute.
return quote! {
#[automatically_derived]
#[allow(deprecated)]
impl From<#module_name::#enum_ident> for #next_module_name::#enum_ident {
fn from(#from_ident: #module_name::#enum_ident) -> Self {
match #from_ident {
#variants
}
}
}
};
}
quote! {}
}
}