mirror of https://github.com/icedland/iced.git
Use map_or_else()
This commit is contained in:
parent
3707090dd0
commit
a3119dafa7
|
@ -102,9 +102,7 @@ impl BlockEncoder {
|
|||
|
||||
fn encode_core(&mut self, rip: u64) -> Result<Vec<u8>, 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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -187,13 +187,19 @@ impl Encoder {
|
|||
}
|
||||
|
||||
fn encode_core(&mut self, instruction: &Instruction, rip: u64) -> Result<u32, JsValue> {
|
||||
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
|
||||
|
|
|
@ -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<Option<OpAccess>, 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<Option<u32>, 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<Option<u32>, 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<String, JsValue> {
|
||||
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
|
||||
|
|
|
@ -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)))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Option<u32>> {
|
||||
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.
|
||||
|
|
|
@ -298,10 +298,7 @@ impl InstructionInfo {
|
|||
/// ValueError: If `operand` is invalid
|
||||
#[text_signature = "($self, operand, /)"]
|
||||
fn op_access(&self, operand: u32) -> PyResult<u32> {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -528,10 +528,7 @@ impl Instruction {
|
|||
/// assert instr.op_register(1) == Register.EBX
|
||||
#[text_signature = "($self, operand, /)"]
|
||||
fn op_kind(&self, operand: u32) -> PyResult<u32> {
|
||||
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<u32> {
|
||||
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.
|
||||
|
|
|
@ -776,10 +776,7 @@ impl OpCodeInfo {
|
|||
/// ValueError: If `operand` is invalid
|
||||
#[text_signature = "($self, operand, /)"]
|
||||
fn op_kind(&self, operand: u32) -> PyResult<u32> {
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue