From f22904bac4bd157f9064aa9840fb02b6020883b2 Mon Sep 17 00:00:00 2001 From: Ana Perez Ghiglia Date: Mon, 17 Jun 2024 16:31:53 -0600 Subject: [PATCH] clean up DebugLocation::from_str --- tooling/debugger/src/context.rs | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/tooling/debugger/src/context.rs b/tooling/debugger/src/context.rs index cf650158ee2..27fe92924dc 100644 --- a/tooling/debugger/src/context.rs +++ b/tooling/debugger/src/context.rs @@ -49,28 +49,23 @@ impl std::str::FromStr for DebugLocation { type Err = DebugLocationFromStrError; fn from_str(s: &str) -> Result { let parts: Vec<_> = s.split(':').collect(); + let error = Err(DebugLocationFromStrError::InvalidDebugLocationString(s.to_string())); - if parts.is_empty() || parts.len() > 2 { - return Err(DebugLocationFromStrError::InvalidDebugLocationString(s.to_string())); - } - - fn parse_components(parts: &Vec<&str>) -> Option { - match parts.len() { - 1 => { - let opcode_location = OpcodeLocation::from_str(parts[0]).ok()?; - Some(DebugLocation { circuit_id: 0, opcode_location }) - } - 2 => { - let circuit_id = parts[0].parse().ok()?; - let opcode_location = OpcodeLocation::from_str(parts[1]).ok()?; - Some(DebugLocation { circuit_id, opcode_location }) + match parts.len() { + 1 => OpcodeLocation::from_str(parts[0]).map_or(error, |opcode_location| { + Ok(DebugLocation { circuit_id: 0, opcode_location }) + }), + 2 => { + let first_part = parts[0].parse().ok(); + let second_part = OpcodeLocation::from_str(parts[1]).ok(); + if let (Some(circuit_id), Some(opcode_location)) = (first_part, second_part) { + Ok(DebugLocation { circuit_id, opcode_location }) + } else { + error } - _ => unreachable!("`DebugLocation` has too many components"), } + _ => error, } - - parse_components(&parts) - .ok_or(DebugLocationFromStrError::InvalidDebugLocationString(s.to_string())) } }