mirror of
https://github.com/fluencelabs/interface-types
synced 2024-12-04 15:20:20 +00:00
feat(interface-types) Simplify code by implementing From<TryFromIntError>
.
This commit is contained in:
parent
5275f3f306
commit
8705de9cc0
@ -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 }
|
||||
}
|
||||
}
|
||||
|
@ -33,18 +33,14 @@ executable_instruction!(
|
||||
)
|
||||
})?;
|
||||
|
||||
let pointer: usize = to_native::<i32>(&inputs[0], instruction)?.try_into().map_err(|_| {
|
||||
InstructionError::new(
|
||||
instruction,
|
||||
InstructionErrorKind::NegativeValue { subject: "pointer" },
|
||||
)
|
||||
})?;
|
||||
let length: usize = to_native::<i32>(&inputs[1], instruction)?.try_into().map_err(|_| {
|
||||
InstructionError::new(
|
||||
instruction,
|
||||
InstructionErrorKind::NegativeValue { subject: "length" },
|
||||
)
|
||||
})?;
|
||||
let pointer: usize = to_native::<i32>(&inputs[0], instruction)?
|
||||
.try_into()
|
||||
.map_err(|e| (e, "pointer").into())
|
||||
.map_err(|k| InstructionError::new(instruction, k))?;
|
||||
let length: usize = to_native::<i32>(&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!(
|
||||
|
Loading…
Reference in New Issue
Block a user