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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use core::marker::PhantomData;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Error {
PrevEndIsUnbounded {
current_index: u16,
},
NextStartIsUnbounded {
current_index: u16,
},
NextStartIsLessThanCurrent {
current_index: u16,
},
/// When an index argument was a `n ..= usize::MAX` range.
InclusiveUptoUsizeMax {
current_index: u16,
},
OverlappingIndexArgs {
left: u16,
right: u16,
},
}
#[derive(Debug, Copy, Clone)]
pub struct ErrorTuple {
pub kind: ErrorKind,
pub first: usize,
pub second: usize,
}
impl ErrorTuple {
const fn new(kind: ErrorKind, first: usize, second: usize) -> Self {
Self {
kind,
first,
second,
}
}
pub const OK: Self = Self::new(ErrorKind::OK, 0, 0);
}
pub const fn result_to_tuple(result: Result<(), Error>) -> ErrorTuple {
match result {
Ok(()) => ErrorTuple::OK,
Err(m_error) => m_error.to_tuple(),
}
}
impl Error {
pub const fn to_tuple(&self) -> ErrorTuple {
match *self {
Error::PrevEndIsUnbounded { current_index } => {
ErrorTuple::new(ErrorKind::PrevEndIsUnbounded, current_index as _, 0)
}
Error::NextStartIsUnbounded { current_index } => {
ErrorTuple::new(ErrorKind::NextStartIsUnbounded, current_index as _, 0)
}
Error::NextStartIsLessThanCurrent { current_index } => {
ErrorTuple::new(ErrorKind::NextStartIsLessThanCurrent, current_index as _, 0)
}
Error::InclusiveUptoUsizeMax { current_index } => {
ErrorTuple::new(ErrorKind::InclusiveUptoUsizeMax, current_index as _, 0)
}
Error::OverlappingIndexArgs { left, right } => {
ErrorTuple::new(ErrorKind::OverlappingIndexArgs, left as _, right as _)
}
}
}
/*
/// Converts this error to a usize, this is potentially lossy.
const fn to_usize(&self) -> usize {
const MAX_CURR_IND: u16 = 1000;
let (discr, index) = match *self {
Error::PrevEndIsUnbounded { current_index } => (1, current_index),
Error::NextStartIsUnbounded { current_index } => (2, current_index),
Error::NextStartIsLessThanCurrent { current_index } => (3, current_index),
Error::InclusiveUptoUsizeMax { current_index } => (4, current_index),
Error::OverlappingIndexArgs { right, .. } => (5, right),
};
["current index can't be larger than 1000"][(index > MAX_CURR_IND) as usize];
(discr * MAX_CURR_IND + index) as usize
}
pub const fn panic(&self) -> ! {
let usize_value = self.to_usize();
macro_rules! panic_if_err {
($variant:ident) => {
match self {
Error::$variant { .. } => usize_value,
_ => 0,
}
};
}
["Expected previous PrenormIndex to have a bounded end"][panic_if_err!(PrevEndIsUnbounded)];
["Expected next PrenormIndex to have a bounded start"][panic_if_err!(NextStartIsUnbounded)];
["Expected next PrenormIndex to have a larger or equal start index"]
[panic_if_err!(NextStartIsLessThanCurrent)];
["multindex does not support `n ..= usize::MAX` ranges,use `n .. usize::MAX` instead"]
[panic_if_err!(InclusiveUptoUsizeMax)];
["At least one of the indices/range arguments overlaps with another one"]
[panic_if_err!(OverlappingIndexArgs)];
loop {}
}
*/
}
macro_rules! declare_error_tys {
(
$(
$variant:ident => $error:ident <$($typaram:ident),* $(,)*>,
)*
) => (
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ErrorKind {
$($variant,)*
}
make_type_picker! {
for[A, B,] struct ErrorPicker[A, B,];
values_to_types = [
$( ErrorKind::$variant => $error<$($typaram),*>, )*
];
}
$(
pub struct $error<$($typaram),*>(
PhantomData<fn()->($($typaram,)*)>,
);
impl<$($typaram,)*> $error<$($typaram,)*> {
pub const NEW: Self = Self(PhantomData);
}
)*
)
}
declare_error_tys!(
OK => NoErrorsFound<>,
PrevEndIsUnbounded => PreviousEndIsUnbounded__CurrentArgumentIs<A>,
NextStartIsUnbounded => NextStartIsUnbounded__CurrentArgumentIs<A>,
NextStartIsLessThanCurrent => NextStartIsLessThanCurrent__CurrentArgumentIs<A>,
InclusiveUptoUsizeMax => InclusiveUptoUsizeMax__CurrentArgumentis<A>,
OverlappingIndexArgs => OverlappingIndexArguments__ArgumentsAre<A, B>,
);