Skip to content

Commit c672d8f

Browse files
committed
Make mapping error simple with map_err
1 parent 879ed6e commit c672d8f

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

vm/src/stdlib/builtins.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,16 @@ mod builtins {
262262
));
263263
}
264264

265-
match std::str::from_utf8(source) {
266-
Ok(s) => Ok(Either::A(vm.ctx.new_str(s))),
267-
Err(err) => {
268-
let msg = format!(
269-
"(unicode error) 'utf-8' codec can't decode byte 0x{:x?} in position {}: invalid start byte",
270-
source[err.valid_up_to()],
271-
err.valid_up_to()
272-
);
273-
Err(vm.new_exception_msg(vm.ctx.exceptions.syntax_error.to_owned(), msg))
274-
}
275-
}
265+
let source = std::str::from_utf8(source).map_err(|err| {
266+
let msg = format!(
267+
"(unicode error) 'utf-8' codec can't decode byte 0x{:x?} in position {}: invalid start byte",
268+
source[err.valid_up_to()],
269+
err.valid_up_to()
270+
);
271+
272+
vm.new_exception_msg(vm.ctx.exceptions.syntax_error.to_owned(), msg)
273+
})?;
274+
Ok(Either::A(vm.ctx.new_str(source)))
276275
}
277276
Either::B(code) => Ok(Either::B(code)),
278277
}?;

0 commit comments

Comments
 (0)