From d052e66bbede71957694aacd97544ec81c77eefd Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Thu, 17 Feb 2022 17:00:21 +0530 Subject: [PATCH] i2c: Trim the device list to avoid errors If the user passes device list with whitespace before or after, like -l " 6:32", then " 6".parse::() fails. Fix this by trimming the argument for whitespace at start or end. Also add a test for the same. Signed-off-by: Viresh Kumar --- i2c/src/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/i2c/src/main.rs b/i2c/src/main.rs index 0775cab..d7ee51a 100644 --- a/i2c/src/main.rs +++ b/i2c/src/main.rs @@ -163,7 +163,7 @@ impl TryFrom for I2cConfiguration { return Err(Error::SocketCountInvalid(0)); } - let devices = AdapterConfig::try_from(args.device_list.as_str())?; + let devices = AdapterConfig::try_from(args.device_list.trim())?; Ok(I2cConfiguration { socket_path: args.socket_path, socket_count: args.socket_count, @@ -324,6 +324,11 @@ mod tests { fn test_parse_successful() { let socket_name = "vi2c.sock"; + // Space before and after the device list + let cmd_args = I2cArgs::from_args(socket_name, " 1:4 ", 1); + I2cConfiguration::try_from(cmd_args).unwrap(); + + // Valid configuration let cmd_args = I2cArgs::from_args(socket_name, "1:4,2:32:21,5:5:23", 5); let config = I2cConfiguration::try_from(cmd_args).unwrap();