Skip to content

Commit

Permalink
Research: exception type.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Apr 17, 2024
1 parent 9a763dd commit c1e354e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
60 changes: 60 additions & 0 deletions jaq-interpret/src/exn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use alloc::{string::String, string::ToString, vec::Vec};
use core::fmt::{self, Display};

enum Exn<V> {
Err(Error<V>),
TailCall(crate::filter::TailCall<V>),
Break,
UpdateIndexError,
}

impl<V> Exn<V> {
pub fn get_err(self) -> Result<Error<V>, Self> {
match self {
Self::Err(e) => Ok(e),
_ => Err(self),
}
}
}

impl<V> From<Error<V>> for Exn<V> {
fn from(e: Error<V>) -> Self {
Exn::Err(e)
}
}

pub struct Error<V>(Part<V, Vec<Part<V, String>>>);

enum Part<V, S> {
Val(V),
Str(S),
}

impl<V> Error<V> {
pub fn new(v: V) -> Self {
Self(Part::Val(v))
}
}

impl<V: From<String> + Display> Error<V> {
/// Convert the error into a value to be used by `catch` filters.
pub fn into_val(self) -> V {
if let Part::Val(v) = self.0 {
v
} else {
V::from(self.to_string())
}
}
}

impl<V: Display> Display for Error<V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.0 {
Part::Val(v) => v.fmt(f),
Part::Str(parts) => parts.iter().try_for_each(|part| match part {
Part::Val(v) => v.fmt(f),
Part::Str(s) => s.fmt(f),
}),
}
}
}
3 changes: 3 additions & 0 deletions jaq-interpret/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub mod results;
mod stack;
mod val;

#[allow(dead_code)]
mod exn;

pub use error::Error;
pub use filter::{Args, FilterT, Native, Owned as Filter, RunPtr, UpdatePtr};
pub use rc_iter::RcIter;
Expand Down

0 comments on commit c1e354e

Please sign in to comment.