From 351cef7b6a698f21af700ececf716b9d849e0049 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Mon, 15 Nov 2021 10:59:18 +0000 Subject: [PATCH] agent: Remove unwrap from verify_cid() Improved the `verify_cid()` function that validates container ID's by removing the need for an `unwrap()`. Signed-off-by: James O. D. Hunt --- src/agent/src/rpc.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index 4cf58830ea..bcabecddf6 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -111,11 +111,18 @@ pub struct AgentService { // ^[a-zA-Z0-9][a-zA-Z0-9_.-]+$ // fn verify_cid(id: &str) -> Result<()> { - let valid = id.len() > 1 - && id.chars().next().unwrap().is_alphanumeric() - && id - .chars() - .all(|c| (c.is_alphanumeric() || ['.', '-', '_'].contains(&c))); + let mut chars = id.chars(); + + let valid = match chars.next() { + Some(first) + if first.is_alphanumeric() + && id.len() > 1 + && chars.all(|c| c.is_alphanumeric() || ['.', '-', '_'].contains(&c)) => + { + true + } + _ => false, + }; match valid { true => Ok(()),