1use std::fmt::{Debug, Display};
2use thiserror::*;
3
4#[derive(Error, Debug, Clone, PartialEq)]
5#[error("{} ({})",.e,.s)]
6pub struct SWrap<E: Debug + Display> {
7 e: E,
8 s: &'static str,
9}
10
11#[derive(Error, Debug, Clone, PartialEq)]
12#[error("{} ({})",.e,.s)]
13pub struct SgWrap<E: Debug + Display> {
14 e: E,
15 s: String,
16}
17
18#[derive(Error, Debug, Clone, PartialEq)]
19#[error("{}",.0)]
20pub struct SError(pub &'static str);
21#[derive(Error, Debug, Clone, PartialEq)]
22#[error("{}",.0)]
23pub struct SgError(pub String);
24
25pub fn e_str<T>(s: &'static str) -> anyhow::Result<T> {
26 Err(SError(s).into())
27}
28
29pub fn e_string<T>(s: String) -> anyhow::Result<T> {
30 Err(SgError(s).into())
31}
32pub trait OpError: Sized {
33 type V;
34 fn op_err(self) -> Option<Self::V>;
35 fn e_str(self, s: &'static str) -> anyhow::Result<Self::V> {
36 self.op_err().ok_or(SError(s).into())
37 }
38 fn e_string(self, s: String) -> anyhow::Result<Self::V> {
39 self.op_err().ok_or(SgError(s).into())
40 }
41}
42
43impl<V> OpError for Option<V> {
44 type V = V;
45 fn op_err(self) -> Self {
46 self
47 }
48}
49
50pub trait ResError: Sized {
51 type V;
52 type E: Debug + Display + Send + Sync + 'static;
53 fn res_err(self) -> Result<Self::V, Self::E>;
54 fn e_str(self, s: &'static str) -> anyhow::Result<Self::V> {
55 self.res_err().map_err(|e| SWrap { s, e }.into())
56 }
57 fn e_string(self, s: String) -> anyhow::Result<Self::V> {
58 self.res_err().map_err(|e| SgWrap { s, e }.into())
59 }
60}
61
62impl<T, E: Debug + Display + Sync + Send + 'static> ResError for Result<T, E> {
63 type V = T;
64 type E = E;
65 fn res_err(self) -> Self {
66 self
67 }
68}