Skip to content

Commit

Permalink
clean up DebugLocation::from_str
Browse files Browse the repository at this point in the history
  • Loading branch information
anaPerezGhiglia committed Jun 21, 2024
1 parent e0e529b commit f28ada2
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions tooling/debugger/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,23 @@ impl std::str::FromStr for DebugLocation {
type Err = DebugLocationFromStrError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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<DebugLocation> {
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()))
}
}

Expand Down

0 comments on commit f28ada2

Please sign in to comment.