Skip to content

Commit

Permalink
Clean up errors in types/object.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
irh committed Sep 23, 2024
1 parent d51de13 commit 9f82994
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
7 changes: 7 additions & 0 deletions crates/runtime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum ErrorKind {
},
#[error("Execution timed out (the limit of {} seconds was reached)", .0.as_secs_f64())]
Timeout(Duration),
#[error("Unable to borrow an object that is already mutably borrowed")]
UnableToBorrowObject,
#[error(
"Unexpected arguments.\n Expected: {expected}\n Provided: |{}|",
value_types_as_string(unexpected)
Expand All @@ -41,6 +43,11 @@ pub enum ErrorKind {
expected: &'static str,
unexpected: KString,
},
#[error("{fn_name} is unimplemented for {object_type}")]
Unimplemented {
fn_name: &'static str,
object_type: KString,
},
#[error("Unable to perform operation '{op}' with '{}' and '{}'", lhs.type_as_string(), rhs.type_as_string())]
InvalidBinaryOp {
lhs: KValue,
Expand Down
34 changes: 16 additions & 18 deletions crates/runtime/src/types/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,42 +304,40 @@ impl KObject {
pub fn try_borrow(&self) -> Result<Borrow<dyn KotoObject>> {
self.object
.try_borrow()
.ok_or_else(|| "Attempting to borrow an object that is already mutably borrowed".into())
.ok_or_else(|| ErrorKind::UnableToBorrowObject.into())
}

/// Attempts to borrow the underlying object mutably
pub fn try_borrow_mut(&self) -> Result<BorrowMut<dyn KotoObject>> {
self.object
.try_borrow_mut()
.ok_or_else(|| "Attempting to borrow an object that is already mutably borrowed".into())
.ok_or_else(|| ErrorKind::UnableToBorrowObject.into())
}

/// Attempts to immutably borrow and cast the underlying object to the specified type
pub fn cast<T: KotoObject>(&self) -> Result<Borrow<T>> {
Borrow::filter_map(self.try_borrow()?, |object| object.downcast_ref::<T>()).map_err(|_| {
ErrorKind::UnexpectedObjectType {
expected: T::type_static(),
unexpected: self.try_borrow().map_or_else(
|_| "Unable to borrow an object that is already mutably borrowed()".into(),
|object| object.type_string(),
),
match self.try_borrow() {
Ok(object) => ErrorKind::UnexpectedObjectType {
expected: T::type_static(),
unexpected: object.type_string(),
}
.into(),
Err(e) => e,
}
.into()
})
}

/// Attempts to mutably borrow and cast the underlying object to the specified type
pub fn cast_mut<T: KotoObject>(&self) -> Result<BorrowMut<T>> {
BorrowMut::filter_map(self.try_borrow_mut()?, |object| object.downcast_mut::<T>()).map_err(
|_| {
ErrorKind::UnexpectedObjectType {
|_| match self.try_borrow() {
Ok(object) => ErrorKind::UnexpectedObjectType {
expected: T::type_static(),
unexpected: self.try_borrow().map_or_else(
|_| "Unable to borrow an object that is already mutably borrowed()".into(),
|object| object.type_string(),
),
unexpected: object.type_string(),
}
.into()
.into(),
Err(e) => e,
},
)
}
Expand Down Expand Up @@ -426,8 +424,8 @@ impl<'a, T: KotoObject> MethodContext<'a, T> {
}

/// Creates an error that describes an unimplemented method
fn unimplemented_error<T>(method: &str, object_type: KString) -> Result<T> {
runtime_error!("{method} is unimplemented for {object_type}")
fn unimplemented_error<T>(fn_name: &'static str, object_type: KString) -> Result<T> {
runtime_error!(ErrorKind::Unimplemented{fn_name, object_type})
}

/// An enum that indicates to the runtime if a [KotoObject] is iterable
Expand Down

0 comments on commit 9f82994

Please sign in to comment.