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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2024 Oxide Computer Company
//! Code to handle metadata associated with an endpoint.
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned, ToTokens};
use serde::Deserialize;
use syn::{spanned::Spanned, Error};
use crate::{
doc::ExtractedDoc,
error_store::ErrorSink,
util::{get_crate, is_wildcard_path, MacroKind, ValidContentType},
};
#[allow(non_snake_case)]
#[derive(Deserialize, Debug)]
pub(crate) enum MethodType {
DELETE,
GET,
HEAD,
PATCH,
POST,
PUT,
OPTIONS,
}
impl MethodType {
pub(crate) fn as_str(&self) -> &'static str {
match self {
MethodType::DELETE => "DELETE",
MethodType::GET => "GET",
MethodType::HEAD => "HEAD",
MethodType::PATCH => "PATCH",
MethodType::POST => "POST",
MethodType::PUT => "PUT",
MethodType::OPTIONS => "OPTIONS",
}
}
}
#[derive(Deserialize, Debug)]
pub(crate) struct EndpointMetadata {
pub(crate) method: MethodType,
pub(crate) path: String,
#[serde(default)]
pub(crate) tags: Vec<String>,
#[serde(default)]
pub(crate) unpublished: bool,
#[serde(default)]
pub(crate) deprecated: bool,
pub(crate) content_type: Option<String>,
pub(crate) _dropshot_crate: Option<String>,
}
impl EndpointMetadata {
/// Returns the dropshot crate value as a TokenStream.
pub(crate) fn dropshot_crate(&self) -> TokenStream {
get_crate(self._dropshot_crate.as_deref())
}
/// Validates metadata, returning an `EndpointMetadata` if valid.
///
/// Note: the only reason we pass in attr here is to provide a span for
/// error reporting. As of Rust 1.76, just passing in `attr.span()` produces
/// incorrect span info in error messages.
pub(crate) fn validate(
self,
name_str: &str,
attr: &dyn ToTokens,
kind: MacroKind,
errors: &ErrorSink<'_, Error>,
) -> Option<ValidatedEndpointMetadata> {
let errors = errors.new();
let EndpointMetadata {
method,
path,
tags,
unpublished,
deprecated,
content_type,
_dropshot_crate,
} = self;
if kind == MacroKind::Trait && _dropshot_crate.is_some() {
errors.push(Error::new_spanned(
attr,
format!(
"endpoint `{name_str}` must not specify `_dropshot_crate`\n\
note: specify this as an argument to `#[api_description]` \
instead",
),
));
}
if path.contains(":.*}") && !self.unpublished {
errors.push(Error::new_spanned(
attr,
format!(
"endpoint `{name_str}` has paths that contain \
a wildcard match, but is not marked 'unpublished = true'",
),
));
}
// The content type must be one of the allowed values.
let content_type = match content_type {
Some(content_type) => match content_type.parse() {
Ok(content_type) => Some(content_type),
Err(_) => {
errors.push(Error::new_spanned(
attr,
format!(
"endpoint `{name_str}` has an invalid \
content type\n\
note: supported content types are: {}",
ValidContentType::to_supported_string()
),
));
None
}
},
None => Some(ValidContentType::ApplicationJson),
};
// errors.has_errors() must be checked first, because it's possible for
// content_type to be Some, but other errors to have occurred.
if errors.has_errors() {
None
} else if let Some(content_type) = content_type {
Some(ValidatedEndpointMetadata {
method,
path,
tags,
unpublished,
deprecated,
content_type,
})
} else {
unreachable!("no validation errors, but content_type is None")
}
}
}
/// A validated form of endpoint metadata.
pub(crate) struct ValidatedEndpointMetadata {
method: MethodType,
path: String,
tags: Vec<String>,
unpublished: bool,
deprecated: bool,
content_type: ValidContentType,
}
impl ValidatedEndpointMetadata {
pub(crate) fn to_api_endpoint_fn(
&self,
dropshot: &TokenStream,
endpoint_name: &str,
kind: &ApiEndpointKind<'_>,
doc: &ExtractedDoc,
) -> TokenStream {
let path = &self.path;
let content_type = self.content_type;
let method_ident = format_ident!("{}", self.method.as_str());
let summary = doc.summary.as_ref().map(|summary| {
quote! { .summary(#summary) }
});
let description = doc.description.as_ref().map(|description| {
quote! { .description(#description) }
});
let tags = self
.tags
.iter()
.map(|tag| {
quote! { .tag(#tag) }
})
.collect::<Vec<_>>();
let visible = self.unpublished.then(|| {
quote! { .visible(false) }
});
let deprecated = self.deprecated.then(|| {
quote! { .deprecated(true) }
});
let fn_call = match kind {
ApiEndpointKind::Regular(endpoint_fn) => {
quote_spanned! {endpoint_fn.span()=>
#dropshot::ApiEndpoint::new(
#endpoint_name.to_string(),
#endpoint_fn,
#dropshot::Method::#method_ident,
#content_type,
#path,
)
}
}
ApiEndpointKind::Stub { attr, extractor_types, ret_ty } => {
// We need to point at the closest possible span to the actual
// error, but we can't point at something nice like the
// function name. That's because if we do, rust-analyzer will
// produce a lot of irrelevant results when ctrl-clicking on
// the function name.
//
// So we point at the `#`, which seems out-of-the-way enough
// for successful generation while being close by for errors.
// Seems pretty unobjectionable.
quote_spanned! {attr.pound_token.span()=>
#dropshot::ApiEndpoint::new_for_types::<(#(#extractor_types,)*), #ret_ty>(
#endpoint_name.to_string(),
#dropshot::Method::#method_ident,
#content_type,
#path,
)
}
}
};
quote! {
#fn_call
#summary
#description
#(#tags)*
#visible
#deprecated
}
}
}
#[allow(non_snake_case)]
#[derive(Deserialize, Debug)]
pub(crate) enum ChannelProtocol {
WEBSOCKETS,
}
#[derive(Deserialize, Debug)]
pub(crate) struct ChannelMetadata {
pub(crate) protocol: ChannelProtocol,
pub(crate) path: String,
#[serde(default)]
pub(crate) tags: Vec<String>,
#[serde(default)]
pub(crate) unpublished: bool,
#[serde(default)]
pub(crate) deprecated: bool,
pub(crate) _dropshot_crate: Option<String>,
}
impl ChannelMetadata {
/// Returns the dropshot crate value as a TokenStream.
pub(crate) fn dropshot_crate(&self) -> TokenStream {
get_crate(self._dropshot_crate.as_deref())
}
/// Validates metadata, returning a `ValidatedChannelMetadata` is valid.
///
/// Note: the only reason we pass in attr here is to provide a span for
/// error reporting. As of Rust 1.76, just passing in `attr.span()` produces
/// incorrect span info in error messages.
pub(crate) fn validate(
self,
name_str: &str,
attr: &dyn ToTokens,
kind: MacroKind,
errors: &ErrorSink<'_, Error>,
) -> Option<ValidatedChannelMetadata> {
let errors = errors.new();
let ChannelMetadata {
protocol: ChannelProtocol::WEBSOCKETS,
path,
tags,
unpublished,
deprecated,
_dropshot_crate,
} = self;
if kind == MacroKind::Trait && _dropshot_crate.is_some() {
errors.push(Error::new_spanned(
attr,
format!(
"channel `{name_str}` must not specify `_dropshot_crate`\n\
note: specify this as an argument to `#[api_description]` \
instead",
),
));
}
// Wildcard paths are not allowed.
if is_wildcard_path(&path) {
errors.push(Error::new_spanned(
attr,
format!(
"channel `{name_str}` has a wildcard path, which is not allowed",
)
));
}
if errors.has_errors() {
None
} else {
// Validating channel metadata also validates the corresponding
// endpoint metadata.
let inner = ValidatedEndpointMetadata {
method: MethodType::GET,
path,
tags,
unpublished,
deprecated,
content_type: ValidContentType::ApplicationJson,
};
Some(ValidatedChannelMetadata { inner })
}
}
}
pub(crate) struct ValidatedChannelMetadata {
// Currently just a wrapper around endpoint metadata, but provided as a
// separate type to be less surprising, and in case more custom
// functionality is desired.
inner: ValidatedEndpointMetadata,
}
impl ValidatedChannelMetadata {
pub(crate) fn to_api_endpoint_fn(
&self,
dropshot: &TokenStream,
endpoint_name: &str,
kind: &ApiEndpointKind<'_>,
doc: &ExtractedDoc,
) -> TokenStream {
// Just forward to the inner endpoint -- any differences are captured in
// `kind`.
self.inner.to_api_endpoint_fn(dropshot, endpoint_name, kind, doc)
}
}
/// The kind of API endpoint call to generate.
#[derive(Clone)]
pub(crate) enum ApiEndpointKind<'ast> {
/// A regular API endpoint. The argument is the function identifier or path.
Regular(&'ast dyn ToTokens),
/// A stub API endpoint. This is used for #[api_description].
Stub {
/// The attribute, used for span information.
attr: &'ast syn::Attribute,
/// The extractor types in use.
extractor_types: Vec<&'ast syn::Type>,
/// The return type.
ret_ty: &'ast syn::Type,
},
}