• 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!("Mismatch between definition and access of `{id}`. {err}",)
31     }
32 }
33 
34 impl std::error::Error for MatchesError {}
35 
36 impl std::fmt::Display for MatchesError {
fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result37     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
38         match self {
39             Self::Downcast { actual, expected } => {
40                 writeln!(
41                     f,
42                     "Could not downcast to {expected:?}, need to downcast to {actual:?}"
43                 )
44             }
45             Self::UnknownArgument {} => {
46                 writeln!(f, "Unknown argument or group id.  Make sure you are using the argument id and not the short or long flags")
47             }
48         }
49     }
50 }
51 
52 #[test]
check_auto_traits()53 fn check_auto_traits() {
54     static_assertions::assert_impl_all!(
55         MatchesError: Send,
56         Sync,
57         std::panic::RefUnwindSafe,
58         std::panic::UnwindSafe,
59         Unpin
60     );
61 }
62