Skip to content

Commit ae4897a

Browse files
committed
Add Value::is_error and Value::as_error helpers
1 parent 4ac87c7 commit ae4897a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/value.rs

+16
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,22 @@ impl Value {
451451
self.as_buffer().is_some()
452452
}
453453

454+
/// Returns `true` if the value is an [`Error`].
455+
#[inline]
456+
pub fn is_error(&self) -> bool {
457+
self.as_error().is_some()
458+
}
459+
460+
/// Cast the value to [`Error`].
461+
///
462+
/// If the value is an [`Error`], returns it or `None` otherwise.
463+
pub fn as_error(&self) -> Option<&Error> {
464+
match self {
465+
Value::Error(e) => Some(e),
466+
_ => None,
467+
}
468+
}
469+
454470
/// Wrap reference to this Value into [`SerializableValue`].
455471
///
456472
/// This allows customizing serialization behavior using serde.

tests/value.rs

+8
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,13 @@ fn test_value_conversions() -> Result<()> {
231231
Some(&"hello")
232232
);
233233

234+
assert!(Value::Error(Box::new(Error::runtime("some error"))).is_error());
235+
assert_eq!(
236+
(Value::Error(Box::new(Error::runtime("some error"))).as_error())
237+
.unwrap()
238+
.to_string(),
239+
"runtime error: some error"
240+
);
241+
234242
Ok(())
235243
}

0 commit comments

Comments
 (0)