1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::borrow::Cow;
use std::ops::Deref;

pub use anyhow::{anyhow, bail, ensure, Result};
#[cfg(feature = "polars")]
use polars::prelude::PolarsError;
use thiserror::Error;

#[derive(Debug)]
pub struct ErrInfo(Cow<'static, str>);

impl<T> From<T> for ErrInfo
where
    T: Into<Cow<'static, str>>,
{
    #[inline]
    fn from(msg: T) -> Self {
        Self(msg.into())
    }
}

impl AsRef<str> for ErrInfo {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl Deref for ErrInfo {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::fmt::Display for ErrInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Error, Debug)]
pub enum TError {
    #[error("The length of both vec doesn't match, left: {left} right: {right}")]
    LengthMismatch { left: usize, right: usize },
    #[error("Index out of bounds: index: {idx}, length: {len}")]
    IdxOut { idx: usize, len: usize },
    #[error(transparent)]
    Other(#[from] std::io::Error),
    #[error("Parse error: {0}")]
    ParseError(ErrInfo),
    #[error("{0}")]
    Str(ErrInfo),
    #[error("unknown error")]
    Unknown,
}

pub type TResult<T> = Result<T, TError>;

#[macro_export]
macro_rules! terr {
    (oob($idx: expr, $len: expr)) => {
        $crate::__private::must_use(
            $crate::TError::IdxOut { idx: $idx, len: $len }
        )
    };
    (lm, $left: expr, $right: expr) => {
        $crate::__private::must_use(
            $crate::TError::LengthMismatch {
                left: $left,
                right: $right,
            }
        )
    };
    (func=$func: ident, $fmt:literal $(, $arg:expr)* $(,)?) => {
        $crate::__private::must_use(
            $crate::TError::Str(
                format!("function {}: {}", stringify!($func), format!($fmt, $($arg),*)).into()
             )
        )
    };
    ($variant:ident: $fmt:literal $(, $arg:expr)* $(,)?) => {
        $crate::__private::must_use(
            $crate::TError::$variant(format!($fmt, $($arg),*).into())
        )
    };
    ($fmt:literal $(, $arg:expr)* $(,)?) => {
        $crate::__private::must_use(
            $crate::TError::Str(format!($fmt, $($arg),*).into())
        )
    };

    ($variant: ident: $err: expr $(,)?) => {
        $crate::__private::must_use(
            $crate::TError::$variant($err.into())
        )
    };
    ($err: expr) => {
        $crate::terr!(Str: $err)
    };
    () => {
        $crate::__private::must_use(
            $crate::TError::Unknown
        )
    };
}

#[macro_export]
macro_rules! tbail {
    ($($tt:tt)+) => {
        return Err($crate::terr!($($tt)+))
    };
}

#[macro_export]
macro_rules! tensure {
    ($cond:expr, $($tt:tt)+) => {
        if !$cond {
            $crate::tbail!($($tt)+);
        }
    };
}

#[cfg(feature = "polars")]
impl From<TError> for PolarsError {
    fn from(e: TError) -> Self {
        PolarsError::ComputeError(format!("{}", e).into())
    }
}

// Not public, referenced by macros only.
#[doc(hidden)]
pub mod __private {
    #[doc(hidden)]
    #[inline]
    #[cold]
    #[must_use]
    pub fn must_use(error: crate::TError) -> crate::TError {
        error
    }
}