From a3119dafa7d26e27187533ea652a058161566d59 Mon Sep 17 00:00:00 2001 From: 0xd4d Date: Mon, 28 Dec 2020 18:30:40 +0100 Subject: [PATCH] Use map_or_else() --- src/rust/iced-x86-js/src/block_encoder.rs | 6 ++---- src/rust/iced-x86-js/src/encoder.rs | 20 +++++++++++------ src/rust/iced-x86-js/src/formatter.rs | 26 +++++++++-------------- src/rust/iced-x86-py/src/block_encoder.rs | 6 ++---- src/rust/iced-x86-py/src/formatter.rs | 7 +++--- src/rust/iced-x86-py/src/info.rs | 5 +---- src/rust/iced-x86-py/src/instruction.rs | 10 ++------- src/rust/iced-x86-py/src/op_code_info.rs | 5 +---- 8 files changed, 34 insertions(+), 51 deletions(-) diff --git a/src/rust/iced-x86-js/src/block_encoder.rs b/src/rust/iced-x86-js/src/block_encoder.rs index ddfb74f3f..2bbc7b062 100644 --- a/src/rust/iced-x86-js/src/block_encoder.rs +++ b/src/rust/iced-x86-js/src/block_encoder.rs @@ -102,9 +102,7 @@ impl BlockEncoder { fn encode_core(&mut self, rip: u64) -> Result, JsValue> { let block = InstructionBlock::new(&self.instructions, rip); - match iced_x86_rust::BlockEncoder::encode(self.bitness, block, self.options) { - Ok(result) => Ok(result.code_buffer), - Err(error) => Err(js_sys::Error::new(&format!("{}", error)).into()), - } + iced_x86_rust::BlockEncoder::encode(self.bitness, block, self.options) + .map_or_else(|error| Err(js_sys::Error::new(&format!("{}", error)).into()), |result| Ok(result.code_buffer)) } } diff --git a/src/rust/iced-x86-js/src/encoder.rs b/src/rust/iced-x86-js/src/encoder.rs index 6d8346b90..176912b2a 100644 --- a/src/rust/iced-x86-js/src/encoder.rs +++ b/src/rust/iced-x86-js/src/encoder.rs @@ -187,13 +187,19 @@ impl Encoder { } fn encode_core(&mut self, instruction: &Instruction, rip: u64) -> Result { - match self.0.encode(&instruction.0, rip) { - Ok(size) => Ok(size as u32), - #[cfg(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm"))] - Err(error) => Err(js_sys::Error::new(&format!("{} ({})", error, instruction.0)).into()), - #[cfg(not(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm")))] - Err(error) => Err(js_sys::Error::new(&format!("{}", error)).into()), - } + self.0.encode(&instruction.0, rip).map_or_else( + |error| { + #[cfg(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm"))] + { + Err(js_sys::Error::new(&format!("{} ({})", error, instruction.0)).into()) + } + #[cfg(not(any(feature = "gas", feature = "intel", feature = "masm", feature = "nasm")))] + { + Err(js_sys::Error::new(&format!("{}", error)).into()) + } + }, + |size| Ok(size as u32), + ) } /// Writes a byte to the output buffer diff --git a/src/rust/iced-x86-js/src/formatter.rs b/src/rust/iced-x86-js/src/formatter.rs index 959ded264..37ab9a33e 100644 --- a/src/rust/iced-x86-js/src/formatter.rs +++ b/src/rust/iced-x86-js/src/formatter.rs @@ -180,10 +180,9 @@ impl Formatter { #[cfg(feature = "instr_info")] #[wasm_bindgen(js_name = "opAccess")] pub fn op_access(&mut self, instruction: &Instruction, operand: u32) -> Result, JsValue> { - match self.formatter.op_access(&instruction.0, operand) { - Ok(value) => Ok(value.map(iced_to_op_access)), - Err(error) => Err(js_sys::Error::new(&format!("{}", error)).into()), - } + self.formatter + .op_access(&instruction.0, operand) + .map_or_else(|error| Err(js_sys::Error::new(&format!("{}", error)).into()), |value| Ok(value.map(iced_to_op_access))) } /// Converts a formatter operand index to an instruction operand index. Returns `undefined` if it's an operand added by the formatter @@ -200,10 +199,7 @@ impl Formatter { /// [`operandCount`]: #method.operand_count #[wasm_bindgen(js_name = "getInstructionOperand")] pub fn get_instruction_operand(&mut self, instruction: &Instruction, operand: u32) -> Result, JsValue> { - match self.formatter.get_instruction_operand(&instruction.0, operand) { - Ok(value) => Ok(value), - Err(error) => Err(js_sys::Error::new(&format!("{}", error)).into()), - } + self.formatter.get_instruction_operand(&instruction.0, operand).map_or_else(|error| Err(js_sys::Error::new(&format!("{}", error)).into()), Ok) } /// Converts an instruction operand index to a formatter operand index. Returns `undefined` if the instruction operand isn't used by the formatter @@ -220,10 +216,9 @@ impl Formatter { pub fn get_formatter_operand( &mut self, instruction: &Instruction, #[allow(non_snake_case)] instructionOperand: u32, ) -> Result, JsValue> { - match self.formatter.get_formatter_operand(&instruction.0, instructionOperand) { - Ok(value) => Ok(value), - Err(error) => Err(js_sys::Error::new(&format!("{}", error)).into()), - } + self.formatter + .get_formatter_operand(&instruction.0, instructionOperand) + .map_or_else(|error| Err(js_sys::Error::new(&format!("{}", error)).into()), Ok) } /// Formats an operand. This is a formatter operand and not necessarily a real instruction operand. @@ -242,10 +237,9 @@ impl Formatter { #[wasm_bindgen(js_name = "formatOperand")] pub fn format_operand(&mut self, instruction: &Instruction, operand: u32) -> Result { let mut output = String::new(); - match self.formatter.format_operand(&instruction.0, &mut output, operand) { - Ok(()) => Ok(output), - Err(error) => Err(js_sys::Error::new(&format!("{}", error)).into()), - } + self.formatter + .format_operand(&instruction.0, &mut output, operand) + .map_or_else(|error| Err(js_sys::Error::new(&format!("{}", error)).into()), |_| Ok(output)) } /// Formats an operand separator diff --git a/src/rust/iced-x86-py/src/block_encoder.rs b/src/rust/iced-x86-py/src/block_encoder.rs index 969e72202..058ba3b51 100644 --- a/src/rust/iced-x86-py/src/block_encoder.rs +++ b/src/rust/iced-x86-py/src/block_encoder.rs @@ -123,9 +123,7 @@ impl BlockEncoder { #[text_signature = "($self, rip, /)"] fn encode<'py>(&mut self, py: Python<'py>, rip: u64) -> PyResult<&'py PyBytes> { let block = iced_x86::InstructionBlock::new(&self.instructions, rip); - match iced_x86::BlockEncoder::encode(self.bitness, block, self.options) { - Ok(result) => Ok(PyBytes::new(py, &result.code_buffer)), - Err(error) => Err(to_value_error(error)), - } + iced_x86::BlockEncoder::encode(self.bitness, block, self.options) + .map_or_else(|e| Err(to_value_error(e)), |result| Ok(PyBytes::new(py, &result.code_buffer))) } } diff --git a/src/rust/iced-x86-py/src/formatter.rs b/src/rust/iced-x86-py/src/formatter.rs index 681c5feb9..45f2088f2 100644 --- a/src/rust/iced-x86-py/src/formatter.rs +++ b/src/rust/iced-x86-py/src/formatter.rs @@ -153,10 +153,9 @@ impl Formatter { /// ValueError: If `operand` is invalid #[text_signature = "($self, instruction, operand, /)"] fn op_access(&mut self, instruction: &Instruction, operand: u32) -> PyResult> { - match self.formatter.op_access(&instruction.instr, operand) { - Ok(res) => Ok(res.map(|v| v as u32)), - Err(_) => Err(PyValueError::new_err("Invalid operand")), - } + self.formatter + .op_access(&instruction.instr, operand) + .map_or_else(|_| Err(PyValueError::new_err("Invalid operand")), |res| Ok(res.map(|v| v as u32))) } /// Converts a formatter operand index to an instruction operand index. diff --git a/src/rust/iced-x86-py/src/info.rs b/src/rust/iced-x86-py/src/info.rs index 2aecca89b..c898169d6 100644 --- a/src/rust/iced-x86-py/src/info.rs +++ b/src/rust/iced-x86-py/src/info.rs @@ -298,10 +298,7 @@ impl InstructionInfo { /// ValueError: If `operand` is invalid #[text_signature = "($self, operand, /)"] fn op_access(&self, operand: u32) -> PyResult { - match self.info.try_op_access(operand) { - Ok(op_access) => Ok(op_access as u32), - Err(e) => Err(to_value_error(e)), - } + self.info.try_op_access(operand).map_or_else(|e| Err(to_value_error(e)), |op_access| Ok(op_access as u32)) } } diff --git a/src/rust/iced-x86-py/src/instruction.rs b/src/rust/iced-x86-py/src/instruction.rs index a6d1fa789..f5948234c 100644 --- a/src/rust/iced-x86-py/src/instruction.rs +++ b/src/rust/iced-x86-py/src/instruction.rs @@ -528,10 +528,7 @@ impl Instruction { /// assert instr.op_register(1) == Register.EBX #[text_signature = "($self, operand, /)"] fn op_kind(&self, operand: u32) -> PyResult { - match self.instr.try_op_kind(operand) { - Ok(op_kind) => Ok(op_kind as u32), - Err(e) => Err(to_value_error(e)), - } + self.instr.try_op_kind(operand).map_or_else(|e| Err(to_value_error(e)), |op_kind| Ok(op_kind as u32)) } /// Sets an operand's kind @@ -1066,10 +1063,7 @@ impl Instruction { /// assert instr.op_register(1) == Register.EBX #[text_signature = "($self, operand, /)"] fn op_register(&self, operand: u32) -> PyResult { - match self.instr.try_op_register(operand) { - Ok(register) => Ok(register as u32), - Err(e) => Err(to_value_error(e)), - } + self.instr.try_op_register(operand).map_or_else(|e| Err(to_value_error(e)), |register| Ok(register as u32)) } /// Sets the operand's register value. diff --git a/src/rust/iced-x86-py/src/op_code_info.rs b/src/rust/iced-x86-py/src/op_code_info.rs index 63d4d6147..40e4be4b2 100644 --- a/src/rust/iced-x86-py/src/op_code_info.rs +++ b/src/rust/iced-x86-py/src/op_code_info.rs @@ -776,10 +776,7 @@ impl OpCodeInfo { /// ValueError: If `operand` is invalid #[text_signature = "($self, operand, /)"] fn op_kind(&self, operand: u32) -> PyResult { - match self.info.try_op_kind(operand) { - Ok(op_kind) => Ok(op_kind as u32), - Err(e) => Err(to_value_error(e)), - } + self.info.try_op_kind(operand).map_or_else(|e| Err(to_value_error(e)), |op_kind| Ok(op_kind as u32)) } /// Gets all operand kinds (a list of :class:`OpCodeOperandKind` enum values)