2025-01-01 04:12:39 +09:00
|
|
|
// Copyright 2018-2025 the Deno authors. MIT license.
|
2024-05-09 13:49:10 -07:00
|
|
|
|
|
|
|
use std::convert::Infallible;
|
2025-01-08 14:52:32 -08:00
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fmt::Display;
|
|
|
|
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::error::CoreError;
|
|
|
|
use deno_error::JsErrorBox;
|
|
|
|
use deno_error::JsErrorClass;
|
2024-05-09 13:49:10 -07:00
|
|
|
|
|
|
|
pub trait InfallibleResultExt<T> {
|
|
|
|
fn unwrap_infallible(self) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> InfallibleResultExt<T> for Result<T, Infallible> {
|
|
|
|
fn unwrap_infallible(self) -> T {
|
|
|
|
match self {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(never) => match never {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-01-08 14:52:32 -08:00
|
|
|
|
|
|
|
pub fn any_and_jserrorbox_downcast_ref<
|
|
|
|
E: Display + Debug + Send + Sync + 'static,
|
|
|
|
>(
|
|
|
|
err: &AnyError,
|
|
|
|
) -> Option<&E> {
|
|
|
|
err
|
|
|
|
.downcast_ref::<E>()
|
|
|
|
.or_else(|| {
|
|
|
|
err
|
|
|
|
.downcast_ref::<JsErrorBox>()
|
|
|
|
.and_then(|e| e.as_any().downcast_ref::<E>())
|
|
|
|
})
|
|
|
|
.or_else(|| {
|
|
|
|
err.downcast_ref::<CoreError>().and_then(|e| match e {
|
|
|
|
CoreError::JsNative(e) => e.as_any().downcast_ref::<E>(),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|