• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Violation of [`ArgMatches`][crate::ArgMatches] assumptions
2 #[derive(Clone, Debug)]
3 #[allow(missing_copy_implementations)] // We might add non-Copy types in the future
4 #[non_exhaustive]
5 pub enum MatchesError {
6     /// Failed to downcast `AnyValue` to the specified type
7     #[non_exhaustive]
8     Downcast {
9         /// Type for value stored in [`ArgMatches`][crate::ArgMatches]
10         actual: super::AnyValueId,
11         /// The target type to downcast to
12         expected: super::AnyValueId,
13     },
14     /// Argument not defined in [`Command`][crate::Command]
15     #[non_exhaustive]
16     UnknownArgument {
17         // Missing `id` but blocked on a public id type which will hopefully come with `unstable-v4`
18     },
19 }
20 
21 impl MatchesError {
22     #[cfg_attr(debug_assertions, track_caller)]
unwrap<T>(id: &str, r: Result<T, MatchesError>) -> T23     pub(crate) fn unwrap<T>(id: &str, r: Result<T, MatchesError>) -> T {
24         let err = match r {
25             Ok(t) => {
26                 return t;
27             }
28             Err(err) => err,
29         };
30         panic!(
31             "Mismatch between definition and access of `{}`. {}",
32             id, err
33         )
34     }
35 }
36 
37 impl std::error::Error for MatchesError {}
38 
39 impl std::fmt::Display for MatchesError {
fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result40     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41         match self {
42             Self::Downcast { actual, expected } => {
43                 writeln!(
44                     f,
45                     "Could not downcast to {:?}, need to downcast to {:?}",
46                     expected, actual
47                 )
48             }
49             Self::UnknownArgument {} => {
50                 writeln!(f, "Unknown argument or group id.  Make sure you are using the argument id and not the short or long flags")
51             }
52         }
53     }
54 }
55 
56 #[test]
check_auto_traits()57 fn check_auto_traits() {
58     static_assertions::assert_impl_all!(
59         MatchesError: Send,
60         Sync,
61         std::panic::RefUnwindSafe,
62         std::panic::UnwindSafe,
63         Unpin
64     );
65 }
66