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,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());
}