Skip to content

Commit

Permalink
fix to return error instead of unreachable
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Sep 20, 2024
1 parent bfed5fd commit a7c8401
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions crates/json/src/move_to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,21 @@ fn convert_move_value_to_json_value(val: &MoveValue, depth: usize) -> VMResult<J
}
}

fn bytes_from_move_value(val: &MoveValue) -> Vec<u8> {
fn bytes_from_move_value(val: &MoveValue) -> VMResult<Vec<u8>> {
match val {
MoveValue::Vector(bytes_val) => bytes_val
.iter()
.map(|byte_val| match byte_val {
MoveValue::U8(byte) => *byte,
_ => unreachable!(),
MoveValue::U8(byte) => Ok(*byte),
_ => Err(deserialization_error_with_msg("Expected U8 in vector")),
})
.collect::<Vec<u8>>(),
_ => unreachable!(),
.collect::<VMResult<Vec<u8>>>(),
_ => Err(deserialization_error_with_msg("Expected vector of U8s")),
}
}

fn convert_json_value_to_json_value(val: &MoveValue) -> VMResult<JSONValue> {
let bz = bytes_from_move_value(val);
let bz = bytes_from_move_value(val)?;
serde_json::from_slice(&bz).map_err(deserialization_error_with_msg)
}

Expand All @@ -157,7 +157,7 @@ fn convert_json_object_to_json_value(val: &MoveValue) -> VMResult<JSONValue> {
| MoveStruct::WithVariantFields(_, _, fields),
) => {
let key =
std::str::from_utf8(&bytes_from_move_value(&fields.first().unwrap().1))
std::str::from_utf8(&bytes_from_move_value(&fields.first().unwrap().1)?)
.map_err(deserialization_error_with_msg)?
.to_string();
let val = convert_json_value_to_json_value(&fields.get(1).unwrap().1)?;
Expand Down

0 comments on commit a7c8401

Please sign in to comment.