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