use crate::{Handled, Error};
use core::fmt;
#[doc(hidden)]
#[cfg(feature = "std")]
#[inline]
pub fn __map_try_erased<T, F>(r: core::result::Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>, on_err: F) -> core::result::Result<T, Handled<Error>>
where
F: FnOnce(Handled<Error>) -> Handled<Error>,
{
r.map_err(|e| on_err(Handled::wrap_box(e)))
}
#[doc(hidden)]
#[cfg(feature = "std")]
#[inline]
pub fn __wrap_frame(e: Box<dyn std::error::Error + Send + Sync + 'static>, file: &'static str, line: u32, col: u32) -> Handled<Error> {
Handled::wrap_box_with_frame(e, file, line, col)
}
#[doc(hidden)]
pub struct __ThrowExpr<T>(pub T);
impl<T, E: fmt::Display> __ThrowExpr<core::result::Result<T, Handled<E>>> {
#[inline]
pub fn __thrown(self) -> Handled<E> {
match self.0 {
Ok(_) => panic!("throw block returned Ok"),
Err(e) => e,
}
}
}
impl<T, E: std::error::Error + Send + Sync + 'static> __ThrowExpr<core::result::Result<T, E>> {
#[inline]
pub fn __thrown_erased(self) -> Handled<Error> {
match self.0 {
Ok(_) => panic!("throw block returned Ok"),
Err(e) => Handled::wrap(e),
}
}
}
impl __ThrowExpr<Handled<Error>> {
#[inline]
pub fn __thrown(self) -> Handled<Error> {
self.0
}
}
impl<E: fmt::Display> __ThrowExpr<Handled<E>> {
#[inline]
pub fn __thrown_generic(self) -> Handled<E> {
self.0
}
}
impl __ThrowExpr<&str> {
#[inline]
pub fn __thrown(self) -> Handled<Error> {
Handled::msg(self.0)
}
}
impl __ThrowExpr<String> {
#[inline]
pub fn __thrown(self) -> Handled<Error> {
Handled::msg(self.0)
}
}
#[doc(hidden)]
pub trait __Thrown {
fn __thrown(self) -> Handled<Error>;
}
impl<E: std::error::Error + Send + Sync + 'static> __Thrown for __ThrowExpr<E> {
#[inline]
fn __thrown(self) -> Handled<Error> {
Handled::wrap(self.0)
}
}
#[doc(hidden)]
#[inline]
pub fn __with_finally<T, F, G>(f: F, finally: G) -> T
where
F: FnOnce() -> T,
G: FnOnce(),
{
let result = f();
finally();
result
}
#[doc(hidden)]
#[inline]
pub fn __convert_try_catch_result<T, E: Into<Handled<Error>>>(
result: core::result::Result<T, E>,
file: &'static str,
line: u32,
col: u32,
) -> core::result::Result<T, Handled<Error>> {
result.map_err(|e| e.into().frame(file, line, col))
}
#[doc(hidden)]
#[inline]
pub fn __convert_try_catch_result_str<T>(
result: core::result::Result<T, &str>,
file: &'static str,
line: u32,
col: u32,
) -> core::result::Result<T, Handled<Error>> {
result.map_err(|e| Handled::msg(e).frame(file, line, col))
}
#[doc(hidden)]
pub trait TryCatchResult<T> {
fn into_handled_result(self, file: &'static str, line: u32, col: u32) -> core::result::Result<T, Handled<Error>>;
}
impl<T, E: Into<Handled<Error>>> TryCatchResult<T> for core::result::Result<T, E> {
#[inline]
fn into_handled_result(self, file: &'static str, line: u32, col: u32) -> core::result::Result<T, Handled<Error>> {
self.map_err(|e| e.into().frame(file, line, col))
}
}
#[doc(hidden)]
pub struct TryCatchConvert;
impl TryCatchConvert {
#[inline]
pub fn run<T, E: Into<Handled<Error>>>(
result: core::result::Result<T, E>,
file: &'static str,
line: u32,
col: u32,
) -> core::result::Result<T, Handled<Error>> {
result.map_err(|e| e.into().frame(file, line, col))
}
}
#[doc(hidden)]
#[inline]
pub fn __try_catch_any<T, E: core::fmt::Display + Send + Sync + 'static, F>(
f: F,
file: &'static str,
line: u32,
col: u32,
) -> core::result::Result<T, Handled<Error>>
where
F: FnOnce() -> core::result::Result<T, E>,
{
f().map_err(|e| Handled::msg(e.to_string()).frame(file, line, col))
}
#[doc(hidden)]
pub struct __ErrWrap<E>(pub E);
impl __ErrWrap<&str> {
#[inline]
pub fn __into_handled(self) -> Handled<Error> {
Handled::msg(self.0)
}
}
impl __ErrWrap<String> {
#[inline]
pub fn __into_handled(self) -> Handled<Error> {
Handled::msg(self.0)
}
}
impl __ErrWrap<Box<dyn std::error::Error + Send + Sync + 'static>> {
#[inline]
pub fn __into_handled(self) -> Handled<Error> {
Handled::wrap_box(self.0)
}
}
impl __ErrWrap<Handled<Error>> {
#[inline]
pub fn __into_handled(self) -> Handled<Error> {
self.0
}
}
#[doc(hidden)]
pub trait __IntoHandled {
fn __into_handled(self) -> Handled<Error>;
}
impl<E: std::error::Error + Send + Sync + 'static> __IntoHandled for __ErrWrap<E> {
#[inline]
fn __into_handled(self) -> Handled<Error> {
Handled::wrap(self.0)
}
}