From 8705de9cc0956d42797cf1bcaa6de421bac5b3a8 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 26 Mar 2020 10:49:49 +0100 Subject: [PATCH] feat(interface-types) Simplify code by implementing `From`. --- src/errors.rs | 11 +++++++++-- src/interpreter/instructions/strings.rs | 24 ++++++++++-------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index db6d7bd..bf9612a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -5,6 +5,7 @@ use crate::{ast::InterfaceType, interpreter::Instruction}; use std::{ error::Error, fmt::{self, Display, Formatter}, + num::TryFromIntError, result::Result, string::{self, ToString}, }; @@ -150,7 +151,7 @@ pub enum InstructionErrorKind { /// The string contains invalid UTF-8 encoding. String(string::FromUtf8Error), - /// A negative value isn't allowed (like a negative pointer value). + /// Out of range integral type conversion attempted. NegativeValue { /// The variable name that triggered the error. subject: &'static str, @@ -236,9 +237,15 @@ impl Display for InstructionErrorKind { Self::NegativeValue { subject } => write!( formatter, - "read the value of `{}` which must be positive", + "attempted to convert `{}` but it appears to be a negative value", subject ), } } } + +impl From<(TryFromIntError, &'static str)> for InstructionErrorKind { + fn from((_, subject): (TryFromIntError, &'static str)) -> Self { + InstructionErrorKind::NegativeValue { subject } + } +} diff --git a/src/interpreter/instructions/strings.rs b/src/interpreter/instructions/strings.rs index 8a40ed2..66afa8d 100644 --- a/src/interpreter/instructions/strings.rs +++ b/src/interpreter/instructions/strings.rs @@ -33,18 +33,14 @@ executable_instruction!( ) })?; - let pointer: usize = to_native::(&inputs[0], instruction)?.try_into().map_err(|_| { - InstructionError::new( - instruction, - InstructionErrorKind::NegativeValue { subject: "pointer" }, - ) - })?; - let length: usize = to_native::(&inputs[1], instruction)?.try_into().map_err(|_| { - InstructionError::new( - instruction, - InstructionErrorKind::NegativeValue { subject: "length" }, - ) - })?; + let pointer: usize = to_native::(&inputs[0], instruction)? + .try_into() + .map_err(|e| (e, "pointer").into()) + .map_err(|k| InstructionError::new(instruction, k))?; + let length: usize = to_native::(&inputs[1], instruction)? + .try_into() + .map_err(|e| (e, "length").into()) + .map_err(|k| InstructionError::new(instruction, k))?; let memory_view = memory.view(); if length == 0 { @@ -237,7 +233,7 @@ mod tests { memory: Memory::new("Hello!".as_bytes().iter().map(|u| Cell::new(*u)).collect()), ..Default::default() }, - error: r#"`string.lift_memory` read the value of `pointer` which must be positive"#, + error: r#"`string.lift_memory` attempted to convert `pointer` but it appears to be a negative value"#, ); test_executable_instruction!( @@ -255,7 +251,7 @@ mod tests { memory: Memory::new("Hello!".as_bytes().iter().map(|u| Cell::new(*u)).collect()), ..Default::default() }, - error: r#"`string.lift_memory` read the value of `length` which must be positive"#, + error: r#"`string.lift_memory` attempted to convert `length` but it appears to be a negative value"#, ); test_executable_instruction!(