From 5934305f37a765f8c611d9a24d9d5f012845d39b Mon Sep 17 00:00:00 2001 From: Judy Hsiao Date: Mon, 16 Mar 2020 15:58:03 +0800 Subject: 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::() .map_err(|e| argument::Error::InvalidValue { value: v.to_string(), expected: "The value of setting should null or cras", })?; ``` we can have: ``` v.parse::() .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 Tested-by: kokoro Reviewed-by: Chih-Yang Hsia Reviewed-by: Zach Reizner Auto-Submit: Judy Hsiao Commit-Queue: Judy Hsiao --- src/argument.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/argument.rs') 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"), }) } } -- cgit 1.4.1