Skip to content

Commit

Permalink
refactor(eval): define EvalResult<T> as alias of `Result<T, Box<Eva…
Browse files Browse the repository at this point in the history
…lError>>`
  • Loading branch information
eduardosm committed Nov 3, 2024
1 parent 5556c62 commit 2f22fdf
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 185 deletions.
10 changes: 5 additions & 5 deletions rsjsonnet-lang/src/program/eval/call.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::super::{
ir, BuiltInFunc, FuncData, FuncKind, FuncParams, ThunkData, ThunkEnv, ThunkEnvData,
};
use super::{EvalError, EvalErrorKind, Evaluator, State, TraceItem};
use super::{EvalErrorKind, EvalResult, Evaluator, State, TraceItem};
use crate::gc::{Gc, GcView};
use crate::interner::InternedStr;
use crate::span::SpanId;
Expand All @@ -27,7 +27,7 @@ impl<'p> Evaluator<'_, 'p> {
call_env: GcView<ThunkEnv<'p>>,
func_env: Option<Gc<ThunkEnv<'p>>>,
call_span: SpanId,
) -> Result<Box<[Gc<ThunkData<'p>>]>, Box<EvalError>> {
) -> EvalResult<Box<[Gc<ThunkData<'p>>]>> {
self.check_call_args_generic(
params,
positional_args,
Expand All @@ -53,7 +53,7 @@ impl<'p> Evaluator<'_, 'p> {
positional_args: &[GcView<ThunkData<'p>>],
named_args: &[(InternedStr<'p>, GcView<ThunkData<'p>>)],
func_env: Option<Gc<ThunkEnv<'p>>>,
) -> Result<Box<[Gc<ThunkData<'p>>]>, Box<EvalError>> {
) -> EvalResult<Box<[Gc<ThunkData<'p>>]>> {
self.check_call_args_generic(
params,
positional_args,
Expand All @@ -79,7 +79,7 @@ impl<'p> Evaluator<'_, 'p> {
named_arg_thunk: impl Fn(&Self, &NamedArg) -> Gc<ThunkData<'p>>,
func_env: Option<Gc<ThunkEnv<'p>>>,
call_span: Option<SpanId>,
) -> Result<Box<[Gc<ThunkData<'p>>]>, Box<EvalError>> {
) -> EvalResult<Box<[Gc<ThunkData<'p>>]>> {
if positional_args.len() > params.order.len() {
return Err(self.report_error(EvalErrorKind::TooManyCallArgs {
span: call_span,
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<'p> Evaluator<'_, 'p> {
positional_args: &[GcView<ThunkData<'p>>],
named_args: &[(InternedStr<'p>, GcView<ThunkData<'p>>)],
call_span: Option<SpanId>,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let (func_name, func_env) = self.get_func_info(func);
let args_thunks =
self.check_call_thunk_args(&func.params, positional_args, named_args, func_env)?;
Expand Down
12 changes: 6 additions & 6 deletions rsjsonnet-lang/src/program/eval/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cell::{Cell, OnceCell};
use super::super::{
ir, FuncData, FuncKind, ImportError, ObjectCore, ObjectData, ThunkEnv, ThunkEnvData, ValueData,
};
use super::{EvalError, EvalErrorKind, EvalErrorValueType, Evaluator, State, TraceItem};
use super::{EvalErrorKind, EvalErrorValueType, EvalResult, Evaluator, State, TraceItem};
use crate::gc::{Gc, GcView};
use crate::interner::InternedStr;
use crate::span::SpanId;
Expand All @@ -14,7 +14,7 @@ impl<'p> Evaluator<'_, 'p> {
&mut self,
expr: &'p ir::Expr<'p>,
env: GcView<ThunkEnv<'p>>,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
match *expr {
ir::Expr::Null => {
self.value_stack.push(ValueData::Null);
Expand Down Expand Up @@ -513,7 +513,7 @@ impl<'p> Evaluator<'_, 'p> {
object: &GcView<ObjectData<'p>>,
field_name: InternedStr<'p>,
expr_span: SpanId,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
if let Some(field_thunk) = self.program.find_object_field_thunk(object, 0, field_name) {
if object.asserts_checked.get() {
self.want_thunk_direct(field_thunk, || TraceItem::ObjectField {
Expand Down Expand Up @@ -543,7 +543,7 @@ impl<'p> Evaluator<'_, 'p> {
super_span: SpanId,
field_name: InternedStr<'p>,
expr_span: SpanId,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let (object, core_i) = env.get_object();
let object = object.view();
if core_i == object.super_cores.len() {
Expand Down Expand Up @@ -576,7 +576,7 @@ impl<'p> Evaluator<'_, 'p> {
step: Option<f64>,
is_func: bool,
span: Option<SpanId>,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let start = if let Some(start) = start {
if !start.is_finite() || start.trunc() != start || start < 0.0 {
return Err(self.report_error(EvalErrorKind::Other {
Expand Down Expand Up @@ -650,7 +650,7 @@ impl<'p> Evaluator<'_, 'p> {
&mut self,
span: Option<SpanId>,
op: ast::BinaryOp,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let rhs = self.value_stack.pop().unwrap();
let lhs = self.value_stack.pop().unwrap();
match (op, lhs, rhs) {
Expand Down
33 changes: 12 additions & 21 deletions rsjsonnet-lang/src/program/eval/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::rc::Rc;

use super::super::{ArrayData, ObjectData, ThunkData, ValueData};
use super::manifest::ManifestJsonFormat;
use super::{EvalError, EvalErrorKind, EvalErrorValueType, Evaluator, State};
use super::{EvalError, EvalErrorKind, EvalErrorValueType, EvalResult, Evaluator, State};
use crate::float;
use crate::gc::GcView;

Expand Down Expand Up @@ -81,7 +81,7 @@ enum WidthTmp<'p> {
}

impl<'p> Evaluator<'_, 'p> {
pub(super) fn do_std_format(&mut self) -> Result<(), Box<EvalError>> {
pub(super) fn do_std_format(&mut self) -> EvalResult<()> {
let vals = self.value_stack.pop().unwrap();
let fmt = self.value_stack.pop().unwrap();

Expand All @@ -106,10 +106,7 @@ impl<'p> Evaluator<'_, 'p> {
Ok(())
}

pub(super) fn parse_format_codes(
&mut self,
fmt: &str,
) -> Result<Vec<FormatPart>, Box<EvalError>> {
pub(super) fn parse_format_codes(&mut self, fmt: &str) -> EvalResult<Vec<FormatPart>> {
let mut parts = Vec::new();
let mut rem = fmt;
while !rem.is_empty() {
Expand Down Expand Up @@ -139,7 +136,7 @@ impl<'p> Evaluator<'_, 'p> {
Ok(parts)
}

fn parse_format_mkey(&mut self, rem: &mut &str) -> Result<Option<String>, Box<EvalError>> {
fn parse_format_mkey(&mut self, rem: &mut &str) -> EvalResult<Option<String>> {
if let Some(rem_cont) = rem.strip_prefix('(') {
let Some(end) = rem_cont.find(')') else {
return Err(self.report_error(EvalErrorKind::Other {
Expand Down Expand Up @@ -185,10 +182,7 @@ impl<'p> Evaluator<'_, 'p> {
Ok(cflags)
}

fn parse_format_field_width(
&mut self,
rem: &mut &str,
) -> Result<Option<FieldWidth>, Box<EvalError>> {
fn parse_format_field_width(&mut self, rem: &mut &str) -> EvalResult<Option<FieldWidth>> {
if let Some(rem_cont) = rem.strip_prefix('*') {
*rem = rem_cont;
Ok(Some(FieldWidth::External))
Expand All @@ -213,7 +207,7 @@ impl<'p> Evaluator<'_, 'p> {
}
}

fn parse_format_prec(&mut self, rem: &mut &str) -> Result<Option<FieldWidth>, Box<EvalError>> {
fn parse_format_prec(&mut self, rem: &mut &str) -> EvalResult<Option<FieldWidth>> {
if let Some(rem_cont) = rem.strip_prefix('.') {
if let Some(rem_cont) = rem_cont.strip_prefix('*') {
*rem = rem_cont;
Expand Down Expand Up @@ -252,10 +246,7 @@ impl<'p> Evaluator<'_, 'p> {
}
}

fn parse_format_length_modifier(
&mut self,
rem: &mut &str,
) -> Result<Option<LenMod>, Box<EvalError>> {
fn parse_format_length_modifier(&mut self, rem: &mut &str) -> EvalResult<Option<LenMod>> {
if let Some(rem_cont) = rem.strip_prefix('h') {
*rem = rem_cont;
Ok(Some(LenMod::H))
Expand All @@ -270,7 +261,7 @@ impl<'p> Evaluator<'_, 'p> {
}
}

fn parse_format_conv_type(&mut self, rem: &mut &str) -> Result<ConvType, Box<EvalError>> {
fn parse_format_conv_type(&mut self, rem: &mut &str) -> EvalResult<ConvType> {
let conv_type = match rem.chars().next() {
None => {
return Err(self.report_error(EvalErrorKind::Other {
Expand Down Expand Up @@ -335,7 +326,7 @@ impl<'p> Evaluator<'_, 'p> {
array: GcView<ArrayData<'p>>,
part_i: usize,
mut array_i: usize,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
if part_i >= parts.len() {
if array_i < array.len() {
return Err(self.report_error(EvalErrorKind::Other {
Expand Down Expand Up @@ -441,7 +432,7 @@ impl<'p> Evaluator<'_, 'p> {
array: GcView<ArrayData<'p>>,
part_i: usize,
mut array_i: usize,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let FormatPart::Code(ref code) = parts[part_i] else {
unreachable!();
};
Expand Down Expand Up @@ -571,7 +562,7 @@ impl<'p> Evaluator<'_, 'p> {
parts: Rc<Vec<FormatPart>>,
object: GcView<ObjectData<'p>>,
part_i: usize,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
if part_i >= parts.len() {
let s = self.string_stack.pop().unwrap();
self.value_stack.push(ValueData::String(s.into()));
Expand Down Expand Up @@ -716,7 +707,7 @@ impl<'p> Evaluator<'_, 'p> {
part_i: usize,
fw: u32,
prec: u32,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let FormatPart::Code(ref code) = parts[part_i] else {
unreachable!();
};
Expand Down
17 changes: 7 additions & 10 deletions rsjsonnet-lang/src/program/eval/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Write as _;
use std::rc::Rc;

use super::super::{ArrayData, ObjectData, ValueData};
use super::{EvalError, EvalErrorKind, Evaluator, State, TraceItem};
use super::{EvalErrorKind, EvalResult, Evaluator, State, TraceItem};
use crate::gc::GcView;
use crate::interner::InternedStr;

Expand Down Expand Up @@ -51,7 +51,7 @@ impl ManifestJsonFormat {
}

impl<'p> Evaluator<'_, 'p> {
pub(super) fn do_manifest_ini_section(&mut self) -> Result<(), Box<EvalError>> {
pub(super) fn do_manifest_ini_section(&mut self) -> EvalResult<()> {
let ValueData::Object(object) = self.value_stack.pop().unwrap() else {
return Err(self.report_error(EvalErrorKind::Other {
span: None,
Expand Down Expand Up @@ -80,10 +80,7 @@ impl<'p> Evaluator<'_, 'p> {
Ok(())
}

pub(super) fn do_manifest_ini_section_item(
&mut self,
name: InternedStr<'p>,
) -> Result<(), Box<EvalError>> {
pub(super) fn do_manifest_ini_section_item(&mut self, name: InternedStr<'p>) -> EvalResult<()> {
let value = self.value_stack.pop().unwrap();
if let ValueData::Array(array) = value {
let array = array.view();
Expand All @@ -105,7 +102,7 @@ impl<'p> Evaluator<'_, 'p> {
Ok(())
}

pub(super) fn do_manifest_python(&mut self) -> Result<(), Box<EvalError>> {
pub(super) fn do_manifest_python(&mut self) -> EvalResult<()> {
let value = self.value_stack.pop().unwrap();
let result = self.string_stack.last_mut().unwrap();
match value {
Expand Down Expand Up @@ -190,7 +187,7 @@ impl<'p> Evaluator<'_, 'p> {
&mut self,
format: Rc<ManifestJsonFormat>,
depth: usize,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let value = self.value_stack.pop().unwrap();
let result = self.string_stack.last_mut().unwrap();
match value {
Expand Down Expand Up @@ -333,7 +330,7 @@ impl<'p> Evaluator<'_, 'p> {
depth: usize,
parent_is_array: bool,
parent_is_object: bool,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let value = self.value_stack.pop().unwrap();
let result = self.string_stack.last_mut().unwrap();
let indent = " ";
Expand Down Expand Up @@ -721,7 +718,7 @@ impl<'p> Evaluator<'_, 'p> {
indent: Rc<str>,
depth: usize,
single_line: bool,
) -> Result<(), Box<EvalError>> {
) -> EvalResult<()> {
let value = self.value_stack.pop().unwrap();
let result = self.string_stack.last_mut().unwrap();
match value {
Expand Down
Loading

0 comments on commit 2f22fdf

Please sign in to comment.