GeezerVison is showing github.com — fix(derive): reject optional positional values · jdx/usage@2b1fb98
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: derive/src/model.rs +21-1Lines changed: 21 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1391,7 +1391,14 @@ impl Field { |
1391 | 1391 | return Err(syn::Error::new( |
1392 | 1392 | span, |
1393 | 1393 | "a field is either a flag or a positional argument, not both: it has \ |
1394 | | - `long` or `short` as well as `arg`", |
| 1394 | + `long` or `short` as well as `arg`", |
| 1395 | + )); |
| 1396 | + } |
| 1397 | + if value_optional && !is_flag { |
| 1398 | + return Err(syn::Error::new( |
| 1399 | + span, |
| 1400 | + "`value_optional` describes a flag whose value may be left off, so the field \ |
| 1401 | + needs a `long` or a `short`", |
1395 | 1402 | )); |
1396 | 1403 | } |
1397 | 1404 | if !is_flag && count { |
@@ -2570,6 +2577,19 @@ mod tests { |
2570 | 2577 | ); |
2571 | 2578 | } |
2572 | 2579 |
|
| 2580 | + #[test] |
| 2581 | + fn value_optional_is_refused_on_a_positional() { |
| 2582 | + let err = rejection( |
| 2583 | + r#" |
| 2584 | + struct Ex { |
| 2585 | + #[usage(arg, value_optional)] |
| 2586 | + out: Option<String>, |
| 2587 | + } |
| 2588 | + "#, |
| 2589 | + ); |
| 2590 | + assert!(err.contains("needs a `long` or a `short`"), "{err}"); |
| 2591 | + } |
| 2592 | + |
2573 | 2593 | #[test] |
2574 | 2594 | fn a_selector_naming_nothing_is_a_compile_error() { |
2575 | 2595 | let err = rejection( |
|