argument: convert to ThisError and sort

BUG=b:197143586
TEST=cargo check

Change-Id: I8d75003c65e647f1cf4962f390823d5c15169a57
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3105434
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Tomasz Jeznach <tjeznach@chromium.org>
This commit is contained in:
Daniel Verkamp 2021-08-18 14:26:42 -07:00 committed by Commit Bot
parent 8eda3ea975
commit 31b6a00062

View file

@ -43,47 +43,39 @@
//! } //! }
//! ``` //! ```
use std::fmt::{self, Display};
use std::result; use std::result;
use remain::sorted;
use thiserror::Error;
/// An error with argument parsing. /// An error with argument parsing.
#[derive(Debug)] #[sorted]
#[derive(Error, Debug)]
pub enum Error { pub enum Error {
/// There was a syntax error with the argument.
Syntax(String),
/// The argument's name is unused.
UnknownArgument(String),
/// The argument was required. /// The argument was required.
#[error("expected argument: {0}")]
ExpectedArgument(String), ExpectedArgument(String),
/// The argument's given value is invalid.
InvalidValue { value: String, expected: String },
/// The argument was already given and none more are expected.
TooManyArguments(String),
/// The argument expects a value. /// The argument expects a value.
#[error("expected parameter value: {0}")]
ExpectedValue(String), ExpectedValue(String),
/// The argument does not expect a value. /// The argument's given value is invalid.
UnexpectedValue(String), #[error("invalid value {value:?}: {expected}")]
InvalidValue { value: String, expected: String },
/// The help information was requested /// The help information was requested
#[error("help was requested")]
PrintHelp, PrintHelp,
} /// There was a syntax error with the argument.
#[error("syntax error: {0}")]
impl Display for Error { Syntax(String),
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { /// The argument was already given and none more are expected.
use self::Error::*; #[error("too many arguments: {0}")]
TooManyArguments(String),
match self { /// The argument does not expect a value.
Syntax(s) => write!(f, "syntax error: {}", s), #[error("unexpected parameter value: {0}")]
UnknownArgument(s) => write!(f, "unknown argument: {}", s), UnexpectedValue(String),
ExpectedArgument(s) => write!(f, "expected argument: {}", s), /// The argument's name is unused.
InvalidValue { value, expected } => { #[error("unknown argument: {0}")]
write!(f, "invalid value {:?}: {}", value, expected) UnknownArgument(String),
}
TooManyArguments(s) => write!(f, "too many arguments: {}", s),
ExpectedValue(s) => write!(f, "expected parameter value: {}", s),
UnexpectedValue(s) => write!(f, "unexpected parameter value: {}", s),
PrintHelp => write!(f, "help was requested"),
}
}
} }
/// Result of a argument parsing. /// Result of a argument parsing.