Skip to main content

clap_builder/error/
context.rs

1/// Semantics for a piece of error information
2#[derive(#[automatically_derived]
impl ::core::marker::Copy for ContextKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ContextKind {
    #[inline]
    fn clone(&self) -> ContextKind { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ContextKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                ContextKind::InvalidSubcommand => "InvalidSubcommand",
                ContextKind::InvalidArg => "InvalidArg",
                ContextKind::PriorArg => "PriorArg",
                ContextKind::ValidSubcommand => "ValidSubcommand",
                ContextKind::ValidValue => "ValidValue",
                ContextKind::InvalidValue => "InvalidValue",
                ContextKind::ActualNumValues => "ActualNumValues",
                ContextKind::ExpectedNumValues => "ExpectedNumValues",
                ContextKind::MinValues => "MinValues",
                ContextKind::SuggestedCommand => "SuggestedCommand",
                ContextKind::SuggestedSubcommand => "SuggestedSubcommand",
                ContextKind::SuggestedArg => "SuggestedArg",
                ContextKind::SuggestedValue => "SuggestedValue",
                ContextKind::TrailingArg => "TrailingArg",
                ContextKind::Suggested => "Suggested",
                ContextKind::Usage => "Usage",
                ContextKind::Custom => "Custom",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for ContextKind {
    #[inline]
    fn eq(&self, other: &ContextKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ContextKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for ContextKind {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash)]
3#[non_exhaustive]
4#[cfg(feature = "error-context")]
5pub enum ContextKind {
6    /// The cause of the error
7    InvalidSubcommand,
8    /// The cause of the error
9    InvalidArg,
10    /// Existing arguments
11    PriorArg,
12    /// Accepted subcommands
13    ValidSubcommand,
14    /// Accepted values
15    ValidValue,
16    /// Rejected values
17    InvalidValue,
18    /// Number of values present
19    ActualNumValues,
20    /// Number of allowed values
21    ExpectedNumValues,
22    /// Minimum number of allowed values
23    MinValues,
24    /// Potential fix for the user
25    SuggestedCommand,
26    /// Potential fix for the user
27    SuggestedSubcommand,
28    /// Potential fix for the user
29    SuggestedArg,
30    /// Potential fix for the user
31    SuggestedValue,
32    /// Trailing argument
33    TrailingArg,
34    /// Potential fix for the user
35    Suggested,
36    /// A usage string
37    Usage,
38    /// An opaque message to the user
39    Custom,
40}
41
42impl ContextKind {
43    /// End-user description of the error case, where relevant
44    pub fn as_str(self) -> Option<&'static str> {
45        match self {
46            Self::InvalidSubcommand => Some("Invalid Subcommand"),
47            Self::InvalidArg => Some("Invalid Argument"),
48            Self::PriorArg => Some("Prior Argument"),
49            Self::ValidSubcommand => Some("Valid Subcommand"),
50            Self::ValidValue => Some("Valid Value"),
51            Self::InvalidValue => Some("Invalid Value"),
52            Self::ActualNumValues => Some("Actual Number of Values"),
53            Self::ExpectedNumValues => Some("Expected Number of Values"),
54            Self::MinValues => Some("Minimum Number of Values"),
55            Self::SuggestedCommand => Some("Suggested Command"),
56            Self::SuggestedSubcommand => Some("Suggested Subcommand"),
57            Self::SuggestedArg => Some("Suggested Argument"),
58            Self::SuggestedValue => Some("Suggested Value"),
59            Self::TrailingArg => Some("Trailing Argument"),
60            Self::Suggested => Some("Suggested"),
61            Self::Usage => None,
62            Self::Custom => None,
63        }
64    }
65}
66
67impl std::fmt::Display for ContextKind {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        self.as_str().unwrap_or_default().fmt(f)
70    }
71}
72
73/// A piece of error information
74#[derive(#[automatically_derived]
impl ::core::clone::Clone for ContextValue {
    #[inline]
    fn clone(&self) -> ContextValue {
        match self {
            ContextValue::None => ContextValue::None,
            ContextValue::Bool(__self_0) =>
                ContextValue::Bool(::core::clone::Clone::clone(__self_0)),
            ContextValue::String(__self_0) =>
                ContextValue::String(::core::clone::Clone::clone(__self_0)),
            ContextValue::Strings(__self_0) =>
                ContextValue::Strings(::core::clone::Clone::clone(__self_0)),
            ContextValue::StyledStr(__self_0) =>
                ContextValue::StyledStr(::core::clone::Clone::clone(__self_0)),
            ContextValue::StyledStrs(__self_0) =>
                ContextValue::StyledStrs(::core::clone::Clone::clone(__self_0)),
            ContextValue::Number(__self_0) =>
                ContextValue::Number(::core::clone::Clone::clone(__self_0)),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ContextValue {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ContextValue::None =>
                ::core::fmt::Formatter::write_str(f, "None"),
            ContextValue::Bool(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Bool",
                    &__self_0),
            ContextValue::String(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "String",
                    &__self_0),
            ContextValue::Strings(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Strings", &__self_0),
            ContextValue::StyledStr(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "StyledStr", &__self_0),
            ContextValue::StyledStrs(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "StyledStrs", &__self_0),
            ContextValue::Number(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Number",
                    &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for ContextValue {
    #[inline]
    fn eq(&self, other: &ContextValue) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (ContextValue::Bool(__self_0), ContextValue::Bool(__arg1_0))
                    => __self_0 == __arg1_0,
                (ContextValue::String(__self_0),
                    ContextValue::String(__arg1_0)) => __self_0 == __arg1_0,
                (ContextValue::Strings(__self_0),
                    ContextValue::Strings(__arg1_0)) => __self_0 == __arg1_0,
                (ContextValue::StyledStr(__self_0),
                    ContextValue::StyledStr(__arg1_0)) => __self_0 == __arg1_0,
                (ContextValue::StyledStrs(__self_0),
                    ContextValue::StyledStrs(__arg1_0)) => __self_0 == __arg1_0,
                (ContextValue::Number(__self_0),
                    ContextValue::Number(__arg1_0)) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ContextValue {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<bool>;
        let _: ::core::cmp::AssertParamIsEq<String>;
        let _: ::core::cmp::AssertParamIsEq<Vec<String>>;
        let _: ::core::cmp::AssertParamIsEq<crate::builder::StyledStr>;
        let _: ::core::cmp::AssertParamIsEq<Vec<crate::builder::StyledStr>>;
        let _: ::core::cmp::AssertParamIsEq<isize>;
    }
}Eq)]
75#[non_exhaustive]
76#[cfg(feature = "error-context")]
77pub enum ContextValue {
78    /// [`ContextKind`] is self-sufficient, no additional information needed
79    None,
80    /// A single value
81    Bool(bool),
82    /// A single value
83    String(String),
84    /// Many values
85    Strings(Vec<String>),
86    /// A single value
87    StyledStr(crate::builder::StyledStr),
88    /// many value
89    StyledStrs(Vec<crate::builder::StyledStr>),
90    /// A single value
91    Number(isize),
92}
93
94impl std::fmt::Display for ContextValue {
95    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96        match self {
97            Self::None => "".fmt(f),
98            Self::Bool(v) => v.fmt(f),
99            Self::String(v) => v.fmt(f),
100            Self::Strings(v) => v.join(", ").fmt(f),
101            Self::StyledStr(v) => v.fmt(f),
102            Self::StyledStrs(v) => {
103                for (i, v) in v.iter().enumerate() {
104                    if i != 0 {
105                        ", ".fmt(f)?;
106                    }
107                    v.fmt(f)?;
108                }
109                Ok(())
110            }
111            Self::Number(v) => v.fmt(f),
112        }
113    }
114}