Make mapping error simple with `map_err`
@@ -262,17 +262,16 @@ mod builtins {
));
}
- match std::str::from_utf8(source) {
- Ok(s) => Ok(Either::A(vm.ctx.new_str(s))),
- Err(err) => {
+ let source = std::str::from_utf8(source).map_err(|err| {
let msg = format!(
"(unicode error) 'utf-8' codec can't decode byte 0x{:x?} in position {}: invalid start byte",
source[err.valid_up_to()],
err.valid_up_to()
);
- Err(vm.new_exception_msg(vm.ctx.exceptions.syntax_error.to_owned(), msg))
- }
- }
+
+ vm.new_exception_msg(vm.ctx.exceptions.syntax_error.to_owned(), msg)
+ })?;
+ Ok(Either::A(vm.ctx.new_str(source)))
}
Either::B(code) => Ok(Either::B(code)),
}?;
