Skip to content

Commit 500ca56

Browse files
authored
Merge pull request #248 from sl-sh-dev/feature/slightly-better-errors
new PR #1: BridgeError to add fn_name context to errors
2 parents 779e4c1 + 1d65af5 commit 500ca56

11 files changed

Lines changed: 300 additions & 318 deletions

File tree

bridge_adapters/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
use compile_state::state::{CompileEnvironment, SloshVm, SloshVmTrait};
2-
use slvm::{CallFuncSig, Value};
2+
use slvm::{CallFuncSig, VMError, VMResult, Value};
3+
use std::error::Error;
4+
use std::fmt::{Display, Formatter};
35

46
pub mod lisp_adapters;
57

8+
pub type BridgeResult<T> = Result<T, BridgeError>;
9+
10+
#[derive(Debug, Clone)]
11+
pub enum BridgeError {
12+
Error(String),
13+
}
14+
15+
impl Error for BridgeError {}
16+
17+
impl BridgeError {
18+
pub fn with_fn<T>(br: BridgeResult<T>, fn_name: impl AsRef<str>) -> VMResult<T> {
19+
br.map_err(|e| VMError::new_conversion(format!("{}: {}", fn_name.as_ref(), e)))
20+
}
21+
}
22+
23+
impl Display for BridgeError {
24+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25+
match self {
26+
BridgeError::Error(s) => write!(f, "{s}"),
27+
}
28+
}
29+
}
30+
631
pub fn add_builtin(
732
env: &mut SloshVm,
833
name: &str,

bridge_adapters/src/lisp_adapters.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@
3030
//! ```ignore
3131
//!
3232
//! impl<'a> SlFromRef<'a, Value> for &'a str {
33-
//! fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
33+
//! fn sl_from_ref(value: Value, vm: &'a SloshVm) -> BridgeResult<Self> {
3434
//! (&value).sl_as_ref(vm)
3535
//! }
3636
//! }
3737
//!
3838
//! impl<'a> SlAsRef<'a, str> for &Value {
39-
//! fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a str> {
39+
//! fn sl_as_ref(&self, vm: &'a SloshVm) -> BridgeResult<&'a str> {
4040
//! match self {
4141
//! Value::String(h) => Ok(vm.get_string(*h)),
4242
//! Value::StringConst(i) => Ok(vm.get_interned(*i)),
4343
//! _ => Err(VMError::new_conversion(
44-
//! ErrorStrings::fix_me_mismatched_type(
44+
//! ErrorStrings::mismatched_type(
4545
//! String::from(ValueTypes::from([
4646
//! ValueType::String,
4747
//! ValueType::StringConst,
@@ -60,17 +60,17 @@
6060
//! ```ignore
6161
//!
6262
//! impl<'a> SlFromRefMut<'a, Value> for &'a mut String {
63-
//! fn sl_from_ref_mut(value: Value, vm: &'a mut SloshVm) -> VMResult<Self> {
63+
//! fn sl_from_ref_mut(value: Value, vm: &'a mut SloshVm) -> BridgeResult<Self> {
6464
//! (&value).sl_as_mut(vm)
6565
//! }
6666
//! }
6767
//!
6868
//! impl<'a> SlAsMut<'a, String> for &Value {
69-
//! fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> VMResult<&'a mut String> {
69+
//! fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> BridgeResult<&'a mut String> {
7070
//! match self {
7171
//! Value::String(h) => vm.get_string_mut(*h),
7272
//! _ => Err(VMError::new_conversion(
73-
//! ErrorStrings::fix_me_mismatched_type(
73+
//! ErrorStrings::mismatched_type(
7474
//! <&'static str>::from(ValueType::String),
7575
//! self.display_type(vm),
7676
//! ),
@@ -101,7 +101,7 @@
101101
//! TODO #220 - returning values via annotated or lifetime?
102102
//! To avoid allocations when converting a slosh &Value back to a rust type that was mutated
103103
//! don't return anything. If it is necessary for the API to return some value.
104-
//! ...either use an annotation on an input argument `fn myfun(#[likeThis] returnme: &mut String, someotherval: String) -> VMResult<()>`
104+
//! ...either use an annotation on an input argument `fn myfun(#[likeThis] returnme: &mut String, someotherval: String) -> BridgeResult<()>`
105105
//! or a lifetime... might be easier to do the annotation.
106106
//!
107107
//!
@@ -243,7 +243,9 @@ use bridge_types::BridgedType;
243243
use bridge_types::{Keyword, KeywordAsString, LooseString, SloshChar, Symbol, SymbolAsString};
244244
use compile_state::state::SloshVm;
245245

246-
use slvm::{VMResult, Value};
246+
use crate::BridgeResult;
247+
use slvm::Value;
248+
247249
mod collections;
248250
pub mod numbers;
249251
pub mod primitives;
@@ -255,14 +257,14 @@ where
255257
Self: BridgedType,
256258
{
257259
/// Converts to this type from the input type.
258-
fn sl_from(value: T, vm: &mut SloshVm) -> VMResult<Self>;
260+
fn sl_from(value: T, vm: &mut SloshVm) -> BridgeResult<Self>;
259261
}
260262

261263
impl<T> SlFrom<Vec<T>> for Value
262264
where
263265
T: SlInto<Value>,
264266
{
265-
fn sl_from(value: Vec<T>, vm: &mut SloshVm) -> VMResult<Self> {
267+
fn sl_from(value: Vec<T>, vm: &mut SloshVm) -> BridgeResult<Self> {
266268
let mut u = Vec::with_capacity(value.len());
267269
for v in value {
268270
u.push(v.sl_into(vm)?);
@@ -275,7 +277,7 @@ impl<'a, T> SlFromRef<'a, slvm::Value> for Vec<T>
275277
where
276278
T: SlFromRef<'a, Value> + 'a,
277279
{
278-
fn sl_from_ref(value: slvm::Value, vm: &'a SloshVm) -> VMResult<Self> {
280+
fn sl_from_ref(value: slvm::Value, vm: &'a SloshVm) -> BridgeResult<Self> {
279281
let mut res = vec![];
280282
for val in value.iter(vm) {
281283
let t: T = val.sl_into_ref(vm)?;
@@ -291,14 +293,14 @@ where
291293
T: BridgedType,
292294
{
293295
/// Converts this type into the (usually inferred) input type.
294-
fn sl_into(self, vm: &mut SloshVm) -> VMResult<T>;
296+
fn sl_into(self, vm: &mut SloshVm) -> BridgeResult<T>;
295297
}
296298

297299
impl<T, U> SlInto<U> for T
298300
where
299301
U: SlFrom<T>,
300302
{
301-
fn sl_into(self, vm: &mut SloshVm) -> VMResult<U> {
303+
fn sl_into(self, vm: &mut SloshVm) -> BridgeResult<U> {
302304
U::sl_from(self, vm)
303305
}
304306
}
@@ -308,7 +310,7 @@ where
308310
Self: Sized,
309311
{
310312
/// Converts to this type from the input type.
311-
fn sl_from_ref(value: T, vm: &'a SloshVm) -> VMResult<Self>;
313+
fn sl_from_ref(value: T, vm: &'a SloshVm) -> BridgeResult<Self>;
312314
}
313315

314316
/// Inverse of [`SlFromRef`]
@@ -318,7 +320,7 @@ where
318320
Self: BridgedType,
319321
{
320322
/// Converts to this type from the input type.
321-
fn sl_into_ref(self, vm: &'a SloshVm) -> VMResult<T>;
323+
fn sl_into_ref(self, vm: &'a SloshVm) -> BridgeResult<T>;
322324
}
323325

324326
impl<'a, T, U> SlIntoRef<'a, U> for T
@@ -327,7 +329,7 @@ where
327329
U: SlFromRef<'a, T>,
328330
U: 'a,
329331
{
330-
fn sl_into_ref(self, vm: &'a SloshVm) -> VMResult<U> {
332+
fn sl_into_ref(self, vm: &'a SloshVm) -> BridgeResult<U> {
331333
U::sl_from_ref(self, vm)
332334
}
333335
}
@@ -337,7 +339,7 @@ where
337339
Self: Sized,
338340
{
339341
/// Converts to this type from the input type.
340-
fn sl_from_ref_mut(value: T, vm: &'a mut SloshVm) -> VMResult<Self>;
342+
fn sl_from_ref_mut(value: T, vm: &'a mut SloshVm) -> BridgeResult<Self>;
341343
}
342344

343345
/// Inverse of [`SlFromRefMut`]
@@ -347,7 +349,7 @@ where
347349
Self: BridgedType,
348350
{
349351
/// Converts to this type from the input type.
350-
fn sl_into_ref_mut(self, vm: &'a mut SloshVm) -> VMResult<T>;
352+
fn sl_into_ref_mut(self, vm: &'a mut SloshVm) -> BridgeResult<T>;
351353
}
352354

353355
impl<'a, T, U> SlIntoRefMut<'a, U> for T
@@ -356,7 +358,7 @@ where
356358
U: SlFromRefMut<'a, T>,
357359
U: 'a,
358360
{
359-
fn sl_into_ref_mut(self, vm: &'a mut SloshVm) -> VMResult<U> {
361+
fn sl_into_ref_mut(self, vm: &'a mut SloshVm) -> BridgeResult<U> {
360362
U::sl_from_ref_mut(self, vm)
361363
}
362364
}
@@ -367,7 +369,7 @@ where
367369
Self: BridgedType,
368370
{
369371
/// Converts this type into a shared reference of the (usually inferred) input type.
370-
fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a T>;
372+
fn sl_as_ref(&self, vm: &'a SloshVm) -> BridgeResult<&'a T>;
371373
}
372374

373375
// SlAsRef lifts over &
@@ -377,7 +379,7 @@ where
377379
&'a T: BridgedType,
378380
{
379381
#[inline]
380-
fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a U> {
382+
fn sl_as_ref(&self, vm: &'a SloshVm) -> BridgeResult<&'a U> {
381383
<T as SlAsRef<'a, U>>::sl_as_ref(*self, vm)
382384
}
383385
}
@@ -389,7 +391,7 @@ where
389391
&'a mut T: BridgedType,
390392
{
391393
#[inline]
392-
fn sl_as_ref(&self, vm: &'a SloshVm) -> VMResult<&'a U> {
394+
fn sl_as_ref(&self, vm: &'a SloshVm) -> BridgeResult<&'a U> {
393395
<T as SlAsRef<'a, U>>::sl_as_ref(*self, vm)
394396
}
395397
}
@@ -399,7 +401,7 @@ where
399401
Self: BridgedType,
400402
{
401403
/// Converts this type into a mutable reference of the (usually inferred) input type.
402-
fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> VMResult<&'a mut T>;
404+
fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> BridgeResult<&'a mut T>;
403405
}
404406

405407
// SlAsMut lifts over &mut
@@ -409,13 +411,13 @@ where
409411
&'a mut T: BridgedType,
410412
{
411413
#[inline]
412-
fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> VMResult<&'a mut U> {
414+
fn sl_as_mut(&mut self, vm: &'a mut SloshVm) -> BridgeResult<&'a mut U> {
413415
(*self).sl_as_mut(vm)
414416
}
415417
}
416418

417419
impl SlFrom<Value> for Value {
418-
fn sl_from(value: Value, _vm: &mut SloshVm) -> VMResult<Self> {
420+
fn sl_from(value: Value, _vm: &mut SloshVm) -> BridgeResult<Self> {
419421
Ok(value)
420422
}
421423
}
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
use crate::lisp_adapters::{SlFromRef, SlFromRefMut};
2+
use crate::{BridgeError, BridgeResult};
23
use bridge_types::ErrorStrings;
34
use compile_state::state::SloshVm;
45
use slvm::vm_hashmap::VMHashMap;
5-
use slvm::{VMError, VMResult, Value, ValueType};
6+
use slvm::{Value, ValueType};
67

78
impl<'a> SlFromRef<'a, Value> for &'a VMHashMap {
8-
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> VMResult<Self> {
9+
fn sl_from_ref(value: Value, vm: &'a SloshVm) -> BridgeResult<Self> {
910
match value {
1011
Value::Map(h) => Ok(vm.get_map(h)),
11-
_ => Err(VMError::new_conversion(
12-
ErrorStrings::fix_me_mismatched_type(
13-
<&'static str>::from(ValueType::Map),
14-
value.display_type(vm),
15-
),
16-
)),
12+
_ => Err(BridgeError::Error(ErrorStrings::mismatched_type(
13+
<&'static str>::from(ValueType::Map),
14+
value.display_type(vm),
15+
))),
1716
}
1817
}
1918
}
2019

2120
impl<'a> SlFromRefMut<'a, Value> for &'a mut VMHashMap {
22-
fn sl_from_ref_mut(value: Value, vm: &'a mut SloshVm) -> VMResult<Self> {
21+
fn sl_from_ref_mut(value: Value, vm: &'a mut SloshVm) -> BridgeResult<Self> {
2322
match value {
24-
Value::Map(h) => Ok(vm.get_map_mut(h)?),
25-
_ => Err(VMError::new_conversion(
26-
ErrorStrings::fix_me_mismatched_type(
27-
<&'static str>::from(ValueType::Map),
28-
value.display_type(vm),
29-
),
30-
)),
23+
Value::Map(h) => vm
24+
.get_map_mut(h)
25+
.map_err(|e| BridgeError::Error(e.to_string())),
26+
_ => Err(BridgeError::Error(ErrorStrings::mismatched_type(
27+
<&'static str>::from(ValueType::Map),
28+
value.display_type(vm),
29+
))),
3130
}
3231
}
3332
}

0 commit comments

Comments
 (0)