File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments