Use type alias for error.

This commit is contained in:
Stephen Chung
2021-12-27 12:27:31 +08:00
parent e7ca3f41dd
commit 05d4c81e7a
29 changed files with 293 additions and 378 deletions

View File

@@ -1,7 +1,7 @@
#![allow(non_snake_case)]
use crate::plugin::*;
use crate::{def_package, EvalAltResult, Position, RhaiError, RhaiResultOf, INT};
use crate::{def_package, Position, RhaiError, RhaiResultOf, ERR, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -11,7 +11,7 @@ use num_traits::Float;
#[inline(never)]
pub fn make_err(msg: impl Into<String>) -> RhaiError {
EvalAltResult::ErrorArithmetic(msg.into(), Position::NONE).into()
ERR::ErrorArithmetic(msg.into(), Position::NONE).into()
}
macro_rules! gen_arithmetic_functions {

View File

@@ -4,8 +4,8 @@
use crate::engine::OP_EQUALS;
use crate::plugin::*;
use crate::{
def_package, Array, Dynamic, EvalAltResult, ExclusiveRange, FnPtr, InclusiveRange,
NativeCallContext, Position, RhaiResultOf, INT,
def_package, Array, Dynamic, ExclusiveRange, FnPtr, InclusiveRange, NativeCallContext,
Position, RhaiResultOf, ERR, INT,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -90,11 +90,7 @@ pub mod array_functions {
// Check if array will be over max size limit
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_array_size() > 0 && (len as usize) > _ctx.engine().max_array_size() {
return Err(EvalAltResult::ErrorDataTooLarge(
"Size of array".to_string(),
Position::NONE,
)
.into());
return Err(ERR::ErrorDataTooLarge("Size of array".to_string(), Position::NONE).into());
}
if len as usize > array.len() {
@@ -174,7 +170,7 @@ pub mod array_functions {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
array.extend(replace);
return;
@@ -213,7 +209,7 @@ pub mod array_functions {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return Array::new();
} else {
@@ -275,7 +271,7 @@ pub mod array_functions {
mapper
.call_raw(&ctx, None, [item.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(mapper.fn_name()) =>
{
mapper.call_raw(&ctx, None, [item.clone(), (i as INT).into()])
@@ -283,7 +279,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"map".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -316,7 +312,7 @@ pub mod array_functions {
if filter
.call_raw(&ctx, None, [item.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(filter.fn_name()) =>
{
filter.call_raw(&ctx, None, [item.clone(), (i as INT).into()])
@@ -324,7 +320,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"filter".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -362,9 +358,7 @@ pub mod array_functions {
if ctx
.call_fn_raw(OP_EQUALS, true, false, &mut [item, &mut value.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
if fn_sig.starts_with(OP_EQUALS) =>
{
ERR::ErrorFunctionNotFound(ref fn_sig, _) if fn_sig.starts_with(OP_EQUALS) => {
if item.type_id() == value.type_id() {
// No default when comparing same type
Err(err)
@@ -410,7 +404,7 @@ pub mod array_functions {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return Ok(-1);
} else {
@@ -421,9 +415,7 @@ pub mod array_functions {
if ctx
.call_fn_raw(OP_EQUALS, true, false, &mut [item, &mut value.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
if fn_sig.starts_with(OP_EQUALS) =>
{
ERR::ErrorFunctionNotFound(ref fn_sig, _) if fn_sig.starts_with(OP_EQUALS) => {
if item.type_id() == value.type_id() {
// No default when comparing same type
Err(err)
@@ -477,7 +469,7 @@ pub mod array_functions {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return Ok(-1);
} else {
@@ -488,7 +480,7 @@ pub mod array_functions {
if filter
.call_raw(&ctx, None, [item.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(filter.fn_name()) =>
{
filter.call_raw(&ctx, None, [item.clone(), (i as INT).into()])
@@ -496,7 +488,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"index_of".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -531,7 +523,7 @@ pub mod array_functions {
if filter
.call_raw(&ctx, None, [item.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(filter.fn_name()) =>
{
filter.call_raw(&ctx, None, [item.clone(), (i as INT).into()])
@@ -539,7 +531,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"some".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -573,7 +565,7 @@ pub mod array_functions {
if !filter
.call_raw(&ctx, None, [item.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(filter.fn_name()) =>
{
filter.call_raw(&ctx, None, [item.clone(), (i as INT).into()])
@@ -581,7 +573,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"all".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -668,7 +660,7 @@ pub mod array_functions {
result = reducer
.call_raw(&ctx, None, [result.clone(), item.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(reducer.fn_name()) =>
{
reducer.call_raw(&ctx, None, [result, item, (i as INT).into()])
@@ -676,7 +668,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"reduce".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -728,7 +720,7 @@ pub mod array_functions {
result = reducer
.call_raw(&ctx, None, [result.clone(), item.clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(reducer.fn_name()) =>
{
reducer.call_raw(&ctx, None, [result, item, ((len - 1 - i) as INT).into()])
@@ -736,7 +728,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"reduce_rev".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -795,7 +787,7 @@ pub mod array_functions {
let type_id = array[0].type_id();
if array.iter().any(|a| a.type_id() != type_id) {
return Err(EvalAltResult::ErrorFunctionNotFound(
return Err(ERR::ErrorFunctionNotFound(
"sort() cannot be called with elements of different types".into(),
Position::NONE,
)
@@ -873,7 +865,7 @@ pub mod array_functions {
if filter
.call_raw(&ctx, None, [array[x].clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(filter.fn_name()) =>
{
filter.call_raw(&ctx, None, [array[x].clone(), (i as INT).into()])
@@ -881,7 +873,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"drain".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -931,7 +923,7 @@ pub mod array_functions {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return Array::new();
} else {
@@ -963,7 +955,7 @@ pub mod array_functions {
if !filter
.call_raw(&ctx, None, [array[x].clone()])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(fn_sig, _)
ERR::ErrorFunctionNotFound(fn_sig, _)
if fn_sig.starts_with(filter.fn_name()) =>
{
filter.call_raw(&ctx, None, [array[x].clone(), (i as INT).into()])
@@ -971,7 +963,7 @@ pub mod array_functions {
_ => Err(err),
})
.map_err(|err| {
Box::new(EvalAltResult::ErrorInFunctionCall(
Box::new(ERR::ErrorInFunctionCall(
"retain".to_string(),
ctx.source().unwrap_or("").to_string(),
err,
@@ -1021,7 +1013,7 @@ pub mod array_functions {
let arr_len = array.len();
start
.checked_abs()
.map_or(0, |n| arr_len - (n as usize).min(arr_len))
.map_or(0, |n| arr_len - usize::min(n as usize, arr_len))
} else if start as usize >= array.len() {
return mem::take(array);
} else {
@@ -1056,9 +1048,7 @@ pub mod array_functions {
if !ctx
.call_fn_raw(OP_EQUALS, true, false, &mut [a1, a2])
.or_else(|err| match *err {
EvalAltResult::ErrorFunctionNotFound(ref fn_sig, _)
if fn_sig.starts_with(OP_EQUALS) =>
{
ERR::ErrorFunctionNotFound(ref fn_sig, _) if fn_sig.starts_with(OP_EQUALS) => {
if a1.type_id() == a2.type_id() {
// No default when comparing same type
Err(err)

View File

@@ -3,8 +3,8 @@
use crate::plugin::*;
use crate::{
def_package, Blob, Dynamic, EvalAltResult, ExclusiveRange, InclusiveRange, NativeCallContext,
Position, RhaiResultOf, INT,
def_package, Blob, Dynamic, ExclusiveRange, InclusiveRange, NativeCallContext, Position,
RhaiResultOf, ERR, INT,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -46,11 +46,7 @@ pub mod blob_functions {
// Check if blob will be over max size limit
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_array_size() > 0 && len > _ctx.engine().max_array_size() {
return Err(EvalAltResult::ErrorDataTooLarge(
"Size of BLOB".to_string(),
Position::NONE,
)
.into());
return Err(ERR::ErrorDataTooLarge("Size of BLOB".to_string(), Position::NONE).into());
}
let mut blob = Blob::new();
@@ -121,11 +117,7 @@ pub mod blob_functions {
// Check if blob will be over max size limit
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_array_size() > 0 && (len as usize) > _ctx.engine().max_array_size() {
return Err(EvalAltResult::ErrorDataTooLarge(
"Size of BLOB".to_string(),
Position::NONE,
)
.into());
return Err(ERR::ErrorDataTooLarge("Size of BLOB".to_string(), Position::NONE).into());
}
if len as usize > blob.len() {
@@ -205,7 +197,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
blob.extend(replace);
return;
@@ -244,7 +236,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return Blob::new();
} else {
@@ -308,7 +300,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return Blob::new();
} else {
@@ -344,7 +336,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return mem::take(blob);
} else {
@@ -373,7 +365,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return 0;
} else {
@@ -442,7 +434,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return;
} else {
@@ -511,7 +503,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return 0.0;
} else {
@@ -587,7 +579,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return;
} else {
@@ -659,7 +651,7 @@ pub mod blob_functions {
let start = if start < 0 {
start
.checked_abs()
.map_or(0, |n| blob_len - (n as usize).min(blob_len))
.map_or(0, |n| blob_len - usize::min(n as usize, blob_len))
} else if start as usize >= blob_len {
return;
} else {

View File

@@ -1,6 +1,6 @@
use crate::plugin::*;
use crate::types::dynamic::Variant;
use crate::{def_package, EvalAltResult, ExclusiveRange, InclusiveRange, RhaiResultOf, INT};
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, ERR, INT};
use std::iter::{ExactSizeIterator, FusedIterator};
use std::ops::{Range, RangeInclusive};
#[cfg(feature = "no_std")]
@@ -26,10 +26,10 @@ where
#[cfg(not(feature = "unchecked"))]
if let Some(r) = from.checked_add(&step) {
if r == from {
return Err(EvalAltResult::ErrorInFunctionCall(
return Err(ERR::ErrorInFunctionCall(
"range".to_string(),
String::new(),
EvalAltResult::ErrorArithmetic(
ERR::ErrorArithmetic(
"step value cannot be zero".to_string(),
crate::Position::NONE,
)
@@ -123,27 +123,18 @@ impl BitRange {
#[cfg(not(feature = "unchecked"))]
if offset >= BITS {
return Err(
EvalAltResult::ErrorBitFieldBounds(BITS, from, crate::Position::NONE).into(),
);
return Err(ERR::ErrorBitFieldBounds(BITS, from, crate::Position::NONE).into());
}
offset
} else {
#[cfg(not(feature = "unchecked"))]
if let Some(abs_from) = from.checked_abs() {
if (abs_from as usize) > BITS {
return Err(EvalAltResult::ErrorBitFieldBounds(
BITS,
from,
crate::Position::NONE,
)
.into());
return Err(ERR::ErrorBitFieldBounds(BITS, from, crate::Position::NONE).into());
}
BITS - (abs_from as usize)
} else {
return Err(
EvalAltResult::ErrorBitFieldBounds(BITS, from, crate::Position::NONE).into(),
);
return Err(ERR::ErrorBitFieldBounds(BITS, from, crate::Position::NONE).into());
}
#[cfg(feature = "unchecked")]
@@ -337,8 +328,8 @@ def_package! {
pub fn new(from: FLOAT, to: FLOAT, step: FLOAT) -> RhaiResultOf<Self> {
#[cfg(not(feature = "unchecked"))]
if step == 0.0 {
return Err(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE).into(),
return Err(ERR::ErrorInFunctionCall("range".to_string(), "".to_string(),
ERR::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE).into(),
crate::Position::NONE,
).into());
}
@@ -399,8 +390,8 @@ def_package! {
pub fn new(from: Decimal, to: Decimal, step: Decimal) -> RhaiResultOf<Self> {
#[cfg(not(feature = "unchecked"))]
if step.is_zero() {
return Err(EvalAltResult::ErrorInFunctionCall("range".to_string(), "".to_string(),
EvalAltResult::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE).into(),
return Err(ERR::ErrorInFunctionCall("range".to_string(), "".to_string(),
ERR::ErrorArithmetic("step value cannot be zero".to_string(), crate::Position::NONE).into(),
crate::Position::NONE,
).into());
}

View File

@@ -1,7 +1,7 @@
use crate::def_package;
use crate::plugin::*;
use crate::types::dynamic::Tag;
use crate::{Dynamic, EvalAltResult, RhaiResultOf, INT};
use crate::{Dynamic, RhaiResultOf, ERR, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -23,7 +23,7 @@ mod core_functions {
#[rhai_fn(name = "set_tag", set = "tag", return_raw)]
pub fn set_tag(value: &mut Dynamic, tag: INT) -> RhaiResultOf<()> {
if tag < Tag::MIN as INT {
Err(EvalAltResult::ErrorArithmetic(
Err(ERR::ErrorArithmetic(
format!(
"{} is too small to fit into a tag (must be between {} and {})",
tag,
@@ -34,7 +34,7 @@ mod core_functions {
)
.into())
} else if tag > Tag::MAX as INT {
Err(EvalAltResult::ErrorArithmetic(
Err(ERR::ErrorArithmetic(
format!(
"{} is too large to fit into a tag (must be between {} and {})",
tag,

View File

@@ -1,7 +1,7 @@
#![allow(non_snake_case)]
use crate::plugin::*;
use crate::{def_package, EvalAltResult, ExclusiveRange, InclusiveRange, RhaiResultOf, INT};
use crate::{def_package, ExclusiveRange, InclusiveRange, RhaiResultOf, ERR, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -207,7 +207,7 @@ mod bit_field_functions {
let offset = index as usize;
if offset >= BITS {
Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
} else {
Ok((value & (1 << offset)) != 0)
}
@@ -216,12 +216,12 @@ mod bit_field_functions {
// Count from end if negative
if offset > BITS {
Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
} else {
Ok((value & (1 << (BITS - offset))) != 0)
}
} else {
Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
}
}
#[rhai_fn(return_raw)]
@@ -230,7 +230,7 @@ mod bit_field_functions {
let offset = index as usize;
if offset >= BITS {
Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
} else {
let mask = 1 << offset;
if new_value {
@@ -245,7 +245,7 @@ mod bit_field_functions {
// Count from end if negative
if offset > BITS {
Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
} else {
let mask = 1 << offset;
if new_value {
@@ -256,7 +256,7 @@ mod bit_field_functions {
Ok(())
}
} else {
Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into())
}
}
#[rhai_fn(name = "get_bits", return_raw)]
@@ -281,7 +281,7 @@ mod bit_field_functions {
let offset = index as usize;
if offset >= BITS {
return Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
}
offset
@@ -290,11 +290,11 @@ mod bit_field_functions {
// Count from end if negative
if offset > BITS {
return Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
}
BITS - offset
} else {
return Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
};
let bits = if offset + bits as usize > BITS {
@@ -343,7 +343,7 @@ mod bit_field_functions {
let offset = index as usize;
if offset >= BITS {
return Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
}
offset
@@ -352,11 +352,11 @@ mod bit_field_functions {
// Count from end if negative
if offset > BITS {
return Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
}
BITS - offset
} else {
return Err(EvalAltResult::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
return Err(ERR::ErrorBitFieldBounds(BITS, index, Position::NONE).into());
};
let bits = if offset + bits as usize > BITS {

View File

@@ -1,7 +1,7 @@
#![allow(non_snake_case)]
use crate::plugin::*;
use crate::{def_package, Position, RhaiResultOf, INT, INT_BASE};
use crate::{def_package, Position, RhaiResultOf, ERR, INT, INT_BASE};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -116,7 +116,7 @@ mod int_functions {
#[rhai_fn(name = "parse_int", return_raw)]
pub fn parse_int_radix(string: &str, radix: INT) -> RhaiResultOf<INT> {
if !(2..=36).contains(&radix) {
return Err(EvalAltResult::ErrorArithmetic(
return Err(ERR::ErrorArithmetic(
format!("Invalid radix: '{}'", radix),
Position::NONE,
)
@@ -126,7 +126,7 @@ mod int_functions {
INT_BASE::from_str_radix(string.trim(), radix as u32)
.map(|v| v as INT)
.map_err(|err| {
EvalAltResult::ErrorArithmetic(
ERR::ErrorArithmetic(
format!("Error parsing integer number '{}': {}", string, err),
Position::NONE,
)
@@ -265,11 +265,10 @@ mod float_functions {
#[rhai_fn(name = "to_int", return_raw)]
pub fn f32_to_int(x: f32) -> RhaiResultOf<INT> {
if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f32) {
Err(EvalAltResult::ErrorArithmetic(
format!("Integer overflow: to_int({})", x),
Position::NONE,
Err(
ERR::ErrorArithmetic(format!("Integer overflow: to_int({})", x), Position::NONE)
.into(),
)
.into())
} else {
Ok(x.trunc() as INT)
}
@@ -277,11 +276,10 @@ mod float_functions {
#[rhai_fn(name = "to_int", return_raw)]
pub fn f64_to_int(x: f64) -> RhaiResultOf<INT> {
if cfg!(not(feature = "unchecked")) && x > (MAX_INT as f64) {
Err(EvalAltResult::ErrorArithmetic(
format!("Integer overflow: to_int({})", x),
Position::NONE,
Err(
ERR::ErrorArithmetic(format!("Integer overflow: to_int({})", x), Position::NONE)
.into(),
)
.into())
} else {
Ok(x.trunc() as INT)
}
@@ -289,7 +287,7 @@ mod float_functions {
#[rhai_fn(return_raw)]
pub fn parse_float(string: &str) -> RhaiResultOf<FLOAT> {
string.trim().parse::<FLOAT>().map_err(|err| {
EvalAltResult::ErrorArithmetic(
ERR::ErrorArithmetic(
format!("Error parsing floating-point number '{}': {}", string, err),
Position::NONE,
)
@@ -475,7 +473,7 @@ mod decimal_functions {
Decimal::from_str(string)
.or_else(|_| Decimal::from_scientific(string))
.map_err(|err| {
EvalAltResult::ErrorArithmetic(
ERR::ErrorArithmetic(
format!("Error parsing decimal number '{}': {}", string, err),
Position::NONE,
)
@@ -487,7 +485,7 @@ mod decimal_functions {
#[rhai_fn(name = "to_decimal", return_raw)]
pub fn f32_to_decimal(x: f32) -> RhaiResultOf<Decimal> {
Decimal::try_from(x).map_err(|_| {
EvalAltResult::ErrorArithmetic(
ERR::ErrorArithmetic(
format!("Cannot convert to Decimal: to_decimal({})", x),
Position::NONE,
)
@@ -498,7 +496,7 @@ mod decimal_functions {
#[rhai_fn(name = "to_decimal", return_raw)]
pub fn f64_to_decimal(x: f64) -> RhaiResultOf<Decimal> {
Decimal::try_from(x).map_err(|_| {
EvalAltResult::ErrorArithmetic(
ERR::ErrorArithmetic(
format!("Cannot convert to Decimal: to_decimal({})", x),
Position::NONE,
)
@@ -509,7 +507,7 @@ mod decimal_functions {
#[rhai_fn(return_raw)]
pub fn to_float(x: Decimal) -> RhaiResultOf<FLOAT> {
FLOAT::try_from(x).map_err(|_| {
EvalAltResult::ErrorArithmetic(
ERR::ErrorArithmetic(
format!("Cannot convert to floating-point: to_float({})", x),
Position::NONE,
)

View File

@@ -509,7 +509,7 @@ mod string_functions {
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
return Err(crate::EvalAltResult::ErrorDataTooLarge(
return Err(crate::ERR::ErrorDataTooLarge(
"Length of string".to_string(),
crate::Position::NONE,
)
@@ -528,7 +528,7 @@ mod string_functions {
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0 && string.len() > _ctx.engine().max_string_size()
{
return Err(crate::EvalAltResult::ErrorDataTooLarge(
return Err(crate::ERR::ErrorDataTooLarge(
"Length of string".to_string(),
crate::Position::NONE,
)
@@ -553,7 +553,7 @@ mod string_functions {
// Check if string will be over max size limit
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0 && len as usize > _ctx.engine().max_string_size() {
return Err(crate::EvalAltResult::ErrorDataTooLarge(
return Err(crate::ERR::ErrorDataTooLarge(
"Length of string".to_string(),
crate::Position::NONE,
)
@@ -579,7 +579,7 @@ mod string_functions {
#[cfg(not(feature = "unchecked"))]
if _ctx.engine().max_string_size() > 0 && string.len() > _ctx.engine().max_string_size()
{
return Err(crate::EvalAltResult::ErrorDataTooLarge(
return Err(crate::ERR::ErrorDataTooLarge(
"Length of string".to_string(),
crate::Position::NONE,
)