diff options
author | Judy Hsiao <judyhsiao@google.com> | 2020-03-16 15:58:03 +0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-03-17 03:02:42 +0000 |
commit | 5934305f37a765f8c611d9a24d9d5f012845d39b (patch) | |
tree | a4a39d1c815033b57e64879bb3d9816b1b787347 /src/argument.rs | |
parent | 8476d79a3c0976355695dde780786179c3417667 (diff) | |
download | crosvm-5934305f37a765f8c611d9a24d9d5f012845d39b.tar crosvm-5934305f37a765f8c611d9a24d9d5f012845d39b.tar.gz crosvm-5934305f37a765f8c611d9a24d9d5f012845d39b.tar.bz2 crosvm-5934305f37a765f8c611d9a24d9d5f012845d39b.tar.lz crosvm-5934305f37a765f8c611d9a24d9d5f012845d39b.tar.xz crosvm-5934305f37a765f8c611d9a24d9d5f012845d39b.tar.zst crosvm-5934305f37a765f8c611d9a24d9d5f012845d39b.zip |
crosvm: Change expected field to String in InvalidValue
Change the type of argument::Error::InvalidValue::expected from "&'static str" to String. It allows the lower level parse error object to handle the output of the expected value so that the rule of parsing will not be duplicated. For example, instead of: ``` v.parse::<Settings>() .map_err(|e| argument::Error::InvalidValue { value: v.to_string(), expected: "The value of setting should null or cras", })?; ``` we can have: ``` v.parse::<Settings>() .map_err(|e| argument::Error::InvalidValue { value: v.to_string(), expected: e.to_string(), })?; ``` and the expected value can be handled in the Display implementaion of Settings::ParseError. BUG=b:140866281 TEST=cargo build Change-Id: Ieba12a21103945fe0e47c70a190a4e5d985af537 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2103605 Tested-by: Judy Hsiao <judyhsiao@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org> Reviewed-by: Zach Reizner <zachr@chromium.org> Auto-Submit: Judy Hsiao <judyhsiao@chromium.org> Commit-Queue: Judy Hsiao <judyhsiao@chromium.org>
Diffstat (limited to 'src/argument.rs')
-rw-r--r-- | src/argument.rs | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/src/argument.rs b/src/argument.rs index f59d503..9c00ae5 100644 --- a/src/argument.rs +++ b/src/argument.rs @@ -25,7 +25,7 @@ //! let v: u32 = value.unwrap().parse().map_err(|_| { //! Error::InvalidValue { //! value: value.unwrap().to_owned(), -//! expected: "this value for `cpus` needs to be integer", +//! expected: String::from("this value for `cpus` needs to be integer"), //! } //! })?; //! } @@ -56,10 +56,7 @@ pub enum Error { /// The argument was required. ExpectedArgument(String), /// The argument's given value is invalid. - InvalidValue { - value: String, - expected: &'static str, - }, + InvalidValue { value: String, expected: String }, /// The argument was already given and none more are expected. TooManyArguments(String), /// The argument expects a value. @@ -447,7 +444,7 @@ mod tests { "cpus" => { let c: u32 = value.unwrap().parse().map_err(|_| Error::InvalidValue { value: value.unwrap().to_owned(), - expected: "this value for `cpus` needs to be integer", + expected: String::from("this value for `cpus` needs to be integer"), })?; assert_eq!(c, 3); } @@ -521,7 +518,7 @@ mod tests { _ => { return Err(Error::InvalidValue { value: v.to_string(), - expected: "2D or 3D", + expected: String::from("2D or 3D"), }) } } |