Use type alias

This commit is contained in:
Stephen Chung
2021-12-25 23:49:14 +08:00
parent 43363e0660
commit 01c35808cb
42 changed files with 538 additions and 702 deletions

View File

@@ -1,6 +1,6 @@
//! Module containing error definitions for the evaluation process.
use crate::{Dynamic, ImmutableString, ParseErrorType, Position, INT};
use crate::{Dynamic, ImmutableString, ParseErrorType, Position, INT, RhaiError};
#[cfg(feature = "no_std")]
use core_error::Error;
#[cfg(not(feature = "no_std"))]
@@ -36,12 +36,12 @@ pub enum EvalAltResult {
ErrorFunctionNotFound(String, Position),
/// An error has occurred inside a called function.
/// Wrapped values are the function name, function source, and the interior error.
ErrorInFunctionCall(String, String, Box<EvalAltResult>, Position),
ErrorInFunctionCall(String, String, RhaiError, Position),
/// Usage of an unknown [module][crate::Module]. Wrapped value is the [module][crate::Module] name.
ErrorModuleNotFound(String, Position),
/// An error has occurred while loading a [module][crate::Module].
/// Wrapped value are the [module][crate::Module] name and the interior error.
ErrorInModule(String, Box<EvalAltResult>, Position),
ErrorInModule(String, RhaiError, Position),
/// Access to `this` that is not bound.
ErrorUnboundThis(Position),
/// Data is not of the required type.
@@ -222,7 +222,7 @@ impl<T: AsRef<str>> From<T> for EvalAltResult {
}
}
impl<T: AsRef<str>> From<T> for Box<EvalAltResult> {
impl<T: AsRef<str>> From<T> for RhaiError {
#[inline(never)]
fn from(err: T) -> Self {
EvalAltResult::ErrorRuntime(err.as_ref().to_string().into(), Position::NONE).into()

View File

@@ -4,7 +4,7 @@ use crate::tokenizer::is_valid_identifier;
use crate::types::dynamic::Variant;
use crate::{
Dynamic, Engine, EvalAltResult, FuncArgs, Identifier, Module, NativeCallContext, Position,
RhaiResult, StaticVec, AST,
RhaiError, RhaiResult, RhaiResultOf, StaticVec, AST,
};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@@ -32,7 +32,7 @@ impl fmt::Debug for FnPtr {
impl FnPtr {
/// Create a new function pointer.
#[inline(always)]
pub fn new(name: impl Into<Identifier>) -> Result<Self, Box<EvalAltResult>> {
pub fn new(name: impl Into<Identifier>) -> RhaiResultOf<Self> {
name.into().try_into()
}
/// Create a new function pointer without checking its parameters.
@@ -135,7 +135,7 @@ impl FnPtr {
engine: &Engine,
ast: &AST,
args: impl FuncArgs,
) -> Result<T, Box<EvalAltResult>> {
) -> RhaiResultOf<T> {
let _ast = ast;
let mut arg_values = crate::StaticVec::new_const();
args.parse(&mut arg_values);
@@ -176,7 +176,7 @@ impl FnPtr {
&self,
context: &NativeCallContext,
args: impl FuncArgs,
) -> Result<T, Box<EvalAltResult>> {
) -> RhaiResultOf<T> {
let mut arg_values = crate::StaticVec::new_const();
args.parse(&mut arg_values);
@@ -247,10 +247,10 @@ impl fmt::Display for FnPtr {
}
impl TryFrom<Identifier> for FnPtr {
type Error = Box<EvalAltResult>;
type Error = RhaiError;
#[inline]
fn try_from(value: Identifier) -> Result<Self, Self::Error> {
fn try_from(value: Identifier) -> RhaiResultOf<Self> {
if is_valid_identifier(value.chars()) {
Ok(Self(value, StaticVec::new_const()))
} else {
@@ -260,40 +260,40 @@ impl TryFrom<Identifier> for FnPtr {
}
impl TryFrom<crate::ImmutableString> for FnPtr {
type Error = Box<EvalAltResult>;
type Error = RhaiError;
#[inline(always)]
fn try_from(value: crate::ImmutableString) -> Result<Self, Self::Error> {
fn try_from(value: crate::ImmutableString) -> RhaiResultOf<Self> {
let s: Identifier = value.into();
Self::try_from(s)
}
}
impl TryFrom<String> for FnPtr {
type Error = Box<EvalAltResult>;
type Error = RhaiError;
#[inline(always)]
fn try_from(value: String) -> Result<Self, Self::Error> {
fn try_from(value: String) -> RhaiResultOf<Self> {
let s: Identifier = value.into();
Self::try_from(s)
}
}
impl TryFrom<Box<str>> for FnPtr {
type Error = Box<EvalAltResult>;
type Error = RhaiError;
#[inline(always)]
fn try_from(value: Box<str>) -> Result<Self, Self::Error> {
fn try_from(value: Box<str>) -> RhaiResultOf<Self> {
let s: Identifier = value.into();
Self::try_from(s)
}
}
impl TryFrom<&str> for FnPtr {
type Error = Box<EvalAltResult>;
type Error = RhaiError;
#[inline(always)]
fn try_from(value: &str) -> Result<Self, Self::Error> {
fn try_from(value: &str) -> RhaiResultOf<Self> {
let s: Identifier = value.into();
Self::try_from(s)
}

View File

@@ -1,7 +1,7 @@
//! Module containing error definitions for the parsing process.
use crate::tokenizer::is_valid_identifier;
use crate::{EvalAltResult, Position};
use crate::{EvalAltResult, Position, RhaiError};
#[cfg(feature = "no_std")]
use core_error::Error;
#[cfg(not(feature = "no_std"))]
@@ -310,7 +310,7 @@ impl fmt::Display for ParseError {
}
}
impl From<ParseErrorType> for Box<EvalAltResult> {
impl From<ParseErrorType> for RhaiError {
#[inline(always)]
fn from(err: ParseErrorType) -> Self {
Box::new(err.into())
@@ -324,7 +324,7 @@ impl From<ParseErrorType> for EvalAltResult {
}
}
impl From<ParseError> for Box<EvalAltResult> {
impl From<ParseError> for RhaiError {
#[inline(always)]
fn from(err: ParseError) -> Self {
Box::new(err.into())