Skip to content

Commit 91424b1

Browse files
committed
api: Improve metadata! history errors
By accepting more errors in the macro syntax, and leaving those for `VersionHistory::new()` which has nicer error messages.
1 parent 4772d28 commit 91424b1

10 files changed

Lines changed: 410 additions & 219 deletions

crates/ruma-common/src/api/metadata.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,28 +159,24 @@ macro_rules! metadata {
159159

160160
( @field history: {
161161
$( unstable $(($unstable_feature:literal))? => $unstable_path:literal, )*
162-
$( stable ($stable_feature_only:literal) => $stable_feature_path:literal, )?
163-
$( $( $version:literal $(| stable ($stable_feature:literal))? => $rhs:tt, )+ )?
162+
$( stable ($stable_feature_only:literal) => $stable_feature_path:literal, )*
163+
$( $version:literal $(| stable ($stable_feature:literal))? => $stable_rhs:tt, )*
164164
} ) => {
165165
$crate::metadata! {
166166
@history_impl
167-
[ $( $unstable_path $(= $unstable_feature)? ),* ]
168-
$( stable ($stable_feature_only) => $stable_feature_path, )?
167+
$( unstable $( ($unstable_feature) )? => $unstable_path, )*
168+
$( stable ($stable_feature_only) => $stable_feature_path, )*
169169
// Flip left and right to avoid macro parsing ambiguities
170-
$( $( $rhs = $version $(| stable ($stable_feature))? ),+ )?
170+
$( $stable_rhs = $version $( | stable ($stable_feature) )?, )*
171171
}
172172
};
173173

174174
( @history_impl
175-
[ $( $unstable_path:literal $(= $unstable_feature:literal)? ),* ]
176-
$( stable ($stable_feature_only:literal) => $stable_feature_path:literal, )?
177-
$(
178-
$( $stable_path:literal = $version:literal $(| stable ($stable_feature:literal))? ),+
179-
$(,
180-
deprecated = $deprecated_version:literal
181-
$(, removed = $removed_version:literal )?
182-
)?
183-
)?
175+
$( unstable $(($unstable_feature:literal))? => $unstable_path:literal, )*
176+
$( stable ($stable_feature_only:literal) => $stable_feature_path:literal, )*
177+
$( $stable_path:literal = $version:literal $(| stable ($stable_feature:literal))?, )*
178+
$( deprecated = $deprecated_version:literal, )?
179+
$( removed = $removed_version:literal, )?
184180
) => {
185181
type PathBuilder = $crate::api::path_builder::VersionHistory;
186182
const PATH_BUILDER: $crate::api::path_builder::VersionHistory = $crate::api::path_builder::VersionHistory::new(
@@ -189,14 +185,14 @@ macro_rules! metadata {
189185
$((
190186
$crate::metadata!(@stable_path_selector stable($stable_feature_only)),
191187
$stable_feature_path
192-
),)?
193-
$($((
194-
$crate::metadata!(@stable_path_selector $version $(| stable($stable_feature))?),
188+
),)*
189+
$((
190+
$crate::metadata!(@stable_path_selector $version $( | stable($stable_feature) )?),
195191
$stable_path
196-
)),+)?
192+
),)*
197193
],
198-
$crate::metadata!(@optional_version $($( $deprecated_version )?)?),
199-
$crate::metadata!(@optional_version $($($( $removed_version )?)?)?),
194+
$crate::metadata!(@optional_version $( $deprecated_version )?),
195+
$crate::metadata!(@optional_version $( $removed_version )?),
200196
);
201197
};
202198

crates/ruma-common/tests/it/api/ruma_api.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ fn ui() {
66
t.pass("tests/it/api/ui/move-value.rs");
77
t.pass("tests/it/api/ui/request-only.rs");
88
t.pass("tests/it/api/ui/response-only.rs");
9-
t.compile_fail("tests/it/api/ui/deprecated-without-added.rs");
10-
t.compile_fail("tests/it/api/ui/removed-without-deprecated.rs");
119
t.compile_fail("tests/it/api/ui/serde-flatten-request-body.rs");
1210
t.compile_fail("tests/it/api/ui/serde-flatten-response-body.rs");
1311
t.compile_fail("tests/it/api/ui/invalid-path.rs");
12+
t.compile_fail("tests/it/api/ui/invalid-version-history.rs");
1413
}

crates/ruma-common/tests/it/api/ui/deprecated-without-added.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

crates/ruma-common/tests/it/api/ui/deprecated-without-added.stderr

Lines changed: 0 additions & 7 deletions
This file was deleted.

crates/ruma-common/tests/it/api/ui/invalid-path.rs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -142,51 +142,6 @@ mod missing_variable_opening_version_history {
142142
pub struct Request;
143143
}
144144

145-
mod no_paths_version_history {
146-
use super::*;
147-
148-
metadata! {
149-
method: GET,
150-
rate_limited: false,
151-
authentication: NoAuthentication,
152-
history: {},
153-
}
154-
155-
pub struct Request;
156-
}
157-
158-
mod variable_count_mismatch_version_history {
159-
use super::*;
160-
161-
metadata! {
162-
method: GET,
163-
rate_limited: false,
164-
authentication: NoAuthentication,
165-
history: {
166-
unstable => "unstable/path/to/endpoint/{variable}/{other}",
167-
1.1 => "path/to/endpoint/{variable}",
168-
},
169-
}
170-
171-
pub struct Request;
172-
}
173-
174-
mod variable_name_mismatch_version_history {
175-
use super::*;
176-
177-
metadata! {
178-
method: GET,
179-
rate_limited: false,
180-
authentication: NoAuthentication,
181-
history: {
182-
unstable => "unstable/path/to/endpoint/{var}",
183-
1.1 => "path/to/endpoint/{variable}",
184-
},
185-
}
186-
187-
pub struct Request;
188-
}
189-
190145
fn main() {
191146
let _ = invalid_char_single_path::Request::PATH_BUILDER;
192147
let _ = invalid_char_version_history::Request::PATH_BUILDER;
@@ -202,8 +157,4 @@ fn main() {
202157

203158
let _ = missing_variable_opening_single_path::Request::PATH_BUILDER;
204159
let _ = missing_variable_opening_version_history::Request::PATH_BUILDER;
205-
206-
let _ = no_paths_version_history::Request::PATH_BUILDER;
207-
let _ = variable_count_mismatch_version_history::Request::PATH_BUILDER;
208-
let _ = variable_name_mismatch_version_history::Request::PATH_BUILDER;
209160
}

crates/ruma-common/tests/it/api/ui/invalid-path.stderr

Lines changed: 20 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ note: inside `path_builder::check_path_is_valid`
2121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
2222

2323
note: erroneous constant encountered
24-
--> tests/it/api/ui/invalid-path.rs:191:13
24+
--> tests/it/api/ui/invalid-path.rs:146:13
2525
|
26-
191 | let _ = invalid_char_single_path::Request::PATH_BUILDER;
26+
146 | let _ = invalid_char_single_path::Request::PATH_BUILDER;
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2828

2929
error[E0080]: evaluation panicked: path contains invalid (non-ascii or whitespace) characters
@@ -50,9 +50,9 @@ note: inside `path_builder::check_path_is_valid`
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
5151

5252
note: erroneous constant encountered
53-
--> tests/it/api/ui/invalid-path.rs:192:13
53+
--> tests/it/api/ui/invalid-path.rs:147:13
5454
|
55-
192 | let _ = invalid_char_version_history::Request::PATH_BUILDER;
55+
147 | let _ = invalid_char_version_history::Request::PATH_BUILDER;
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5757

5858
error[E0080]: evaluation panicked: path contains invalid (non-ascii or whitespace) characters
@@ -78,9 +78,9 @@ note: inside `path_builder::check_path_is_valid`
7878
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
7979

8080
note: erroneous constant encountered
81-
--> tests/it/api/ui/invalid-path.rs:194:13
81+
--> tests/it/api/ui/invalid-path.rs:149:13
8282
|
83-
194 | let _ = whitespace_single_path::Request::PATH_BUILDER;
83+
149 | let _ = whitespace_single_path::Request::PATH_BUILDER;
8484
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8585

8686
error[E0080]: evaluation panicked: path contains invalid (non-ascii or whitespace) characters
@@ -107,9 +107,9 @@ note: inside `path_builder::check_path_is_valid`
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
108108

109109
note: erroneous constant encountered
110-
--> tests/it/api/ui/invalid-path.rs:195:13
110+
--> tests/it/api/ui/invalid-path.rs:150:13
111111
|
112-
195 | let _ = whitespace_version_history::Request::PATH_BUILDER;
112+
150 | let _ = whitespace_version_history::Request::PATH_BUILDER;
113113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114114

115115
error[E0080]: evaluation panicked: endpoint paths syntax has changed and segment variables must be wrapped by `{}`
@@ -135,9 +135,9 @@ note: inside `extract_endpoint_path_segment_variable`
135135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
136136

137137
note: erroneous constant encountered
138-
--> tests/it/api/ui/invalid-path.rs:197:13
138+
--> tests/it/api/ui/invalid-path.rs:152:13
139139
|
140-
197 | let _ = old_variable_syntax_single_path::Request::PATH_BUILDER;
140+
152 | let _ = old_variable_syntax_single_path::Request::PATH_BUILDER;
141141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
142142

143143
error[E0080]: evaluation panicked: endpoint paths syntax has changed and segment variables must be wrapped by `{}`
@@ -169,9 +169,9 @@ note: inside `extract_endpoint_path_segment_variable`
169169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
170170

171171
note: erroneous constant encountered
172-
--> tests/it/api/ui/invalid-path.rs:198:13
172+
--> tests/it/api/ui/invalid-path.rs:153:13
173173
|
174-
198 | let _ = old_variable_syntax_version_history::Request::PATH_BUILDER;
174+
153 | let _ = old_variable_syntax_version_history::Request::PATH_BUILDER;
175175
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
176176

177177
error[E0080]: evaluation panicked: endpoint path segment variable braces mismatch: missing ending `}`
@@ -200,9 +200,9 @@ note: inside `extract_endpoint_path_segment_variable`
200200
= note: this error originates in the macro `$crate::metadata` which comes from the expansion of the macro `metadata` (in Nightly builds, run with -Z macro-backtrace for more info)
201201

202202
note: erroneous constant encountered
203-
--> tests/it/api/ui/invalid-path.rs:200:13
203+
--> tests/it/api/ui/invalid-path.rs:155:13
204204
|
205-
200 | let _ = missing_variable_closing_single_path::Request::PATH_BUILDER;
205+
155 | let _ = missing_variable_closing_single_path::Request::PATH_BUILDER;
206206
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
207207

208208
error[E0080]: evaluation panicked: endpoint path segment variable braces mismatch: missing ending `}`
@@ -237,9 +237,9 @@ note: inside `extract_endpoint_path_segment_variable`
237237
= note: this error originates in the macro `$crate::metadata` which comes from the expansion of the macro `metadata` (in Nightly builds, run with -Z macro-backtrace for more info)
238238

239239
note: erroneous constant encountered
240-
--> tests/it/api/ui/invalid-path.rs:201:13
240+
--> tests/it/api/ui/invalid-path.rs:156:13
241241
|
242-
201 | let _ = missing_variable_closing_version_history::Request::PATH_BUILDER;
242+
156 | let _ = missing_variable_closing_version_history::Request::PATH_BUILDER;
243243
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
244244

245245
error[E0080]: evaluation panicked: endpoint path segment variable braces mismatch: missing starting `{`
@@ -265,9 +265,9 @@ note: inside `extract_endpoint_path_segment_variable`
265265
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
266266

267267
note: erroneous constant encountered
268-
--> tests/it/api/ui/invalid-path.rs:203:13
268+
--> tests/it/api/ui/invalid-path.rs:158:13
269269
|
270-
203 | let _ = missing_variable_opening_single_path::Request::PATH_BUILDER;
270+
158 | let _ = missing_variable_opening_single_path::Request::PATH_BUILDER;
271271
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
272272

273273
error[E0080]: evaluation panicked: endpoint path segment variable braces mismatch: missing starting `{`
@@ -299,88 +299,7 @@ note: inside `extract_endpoint_path_segment_variable`
299299
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
300300

301301
note: erroneous constant encountered
302-
--> tests/it/api/ui/invalid-path.rs:204:13
302+
--> tests/it/api/ui/invalid-path.rs:159:13
303303
|
304-
204 | let _ = missing_variable_opening_version_history::Request::PATH_BUILDER;
304+
159 | let _ = missing_variable_opening_version_history::Request::PATH_BUILDER;
305305
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
306-
307-
error[E0080]: evaluation panicked: no endpoint paths supplied
308-
--> tests/it/api/ui/invalid-path.rs:148:5
309-
|
310-
148 | / metadata! {
311-
149 | | method: GET,
312-
150 | | rate_limited: false,
313-
151 | | authentication: NoAuthentication,
314-
152 | | history: {},
315-
153 | | }
316-
| |_____^ evaluation of `<no_paths_version_history::Request as ruma_common::api::Metadata>::PATH_BUILDER` failed inside this call
317-
|
318-
note: inside `VersionHistory::new`
319-
--> src/api/path_builder.rs
320-
|
321-
| panic!("no endpoint paths supplied")
322-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
323-
324-
note: erroneous constant encountered
325-
--> tests/it/api/ui/invalid-path.rs:206:13
326-
|
327-
206 | let _ = no_paths_version_history::Request::PATH_BUILDER;
328-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
329-
330-
error[E0080]: evaluation panicked: counts of endpoint path segment variables do not match
331-
--> tests/it/api/ui/invalid-path.rs:161:5
332-
|
333-
161 | / metadata! {
334-
162 | | method: GET,
335-
163 | | rate_limited: false,
336-
164 | | authentication: NoAuthentication,
337-
... |
338-
168 | | },
339-
169 | | }
340-
| |_____^ evaluation of `<variable_count_mismatch_version_history::Request as ruma_common::api::Metadata>::PATH_BUILDER` failed inside this call
341-
|
342-
note: inside `VersionHistory::new`
343-
--> src/api/path_builder.rs
344-
|
345-
| check_path_args_equal(ref_path, version_path.1);
346-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
347-
note: inside `VersionHistory::new::check_path_args_equal`
348-
--> src/api/path_builder.rs
349-
|
350-
| panic!("counts of endpoint path segment variables do not match");
351-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
352-
353-
note: erroneous constant encountered
354-
--> tests/it/api/ui/invalid-path.rs:207:13
355-
|
356-
207 | let _ = variable_count_mismatch_version_history::Request::PATH_BUILDER;
357-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
358-
359-
error[E0080]: evaluation panicked: names of endpoint path segment variables do not match
360-
--> tests/it/api/ui/invalid-path.rs:177:5
361-
|
362-
177 | / metadata! {
363-
178 | | method: GET,
364-
179 | | rate_limited: false,
365-
180 | | authentication: NoAuthentication,
366-
... |
367-
184 | | },
368-
185 | | }
369-
| |_____^ evaluation of `<variable_name_mismatch_version_history::Request as ruma_common::api::Metadata>::PATH_BUILDER` failed inside this call
370-
|
371-
note: inside `VersionHistory::new`
372-
--> src/api/path_builder.rs
373-
|
374-
| check_path_args_equal(ref_path, version_path.1);
375-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
376-
note: inside `VersionHistory::new::check_path_args_equal`
377-
--> src/api/path_builder.rs
378-
|
379-
| ... panic!("names of endpoint path segment variables do not match");
380-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
381-
382-
note: erroneous constant encountered
383-
--> tests/it/api/ui/invalid-path.rs:208:13
384-
|
385-
208 | let _ = variable_name_mismatch_version_history::Request::PATH_BUILDER;
386-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)