1#[non_exhaustive]
3#[derive(Clone,Debug,PartialEq,Eq)]
4pub enum NTResult<T: Sized + Clone>{
5Eval(T), Overflow, DNE, Infinite, InfiniteSet, CompExceeded, CompOverflow, Undefined, }
22
23
24impl<T: Sized + Clone + Default> NTResult<T>{
25
26pub fn unwrap(&self) -> T{
28 match self{
29 NTResult::Eval(x) => x.clone(),
30 NTResult::Overflow=> panic!("Result does not fit in datatype"),
31 NTResult::DNE => panic!("Does Not Exist"),
32 NTResult::Infinite => panic!("Infinitely large solution"),
33 NTResult::InfiniteSet => panic!("Infinite number of solutions"),
34 NTResult::CompExceeded => panic!("Computation exceeded hardcoded limit"),
35 NTResult::CompOverflow => panic!("Overflowed during computation"),
36 NTResult::Undefined => panic!("Undefined solution"),
37 }
38 }
39
40 pub fn expect(&self, signal: &str)-> T{
42 match self{
43 NTResult::Eval(x) => x.clone(),
44 _=> panic!("{}",signal)
45 }
46 }
47 pub fn unwrap_or(&self, res: T) -> T{
49 match self{
50 NTResult::Eval(x) => x.clone(),
51 _=> res,
52 }
53 }
54 pub fn is_infinite(&self) -> bool{
56 match self{
57 NTResult::Infinite => true,
58 _=> false,
59 }
60 }
61 pub fn is_infiniteset(&self) -> bool{
63 match self{
64 NTResult::InfiniteSet => true,
65 _=> false,
66 }
67 }
68 pub fn is_dne(&self) -> bool{
70 match self{
71 NTResult::DNE => true,
72 _=> false,
73 }
74 }
75 pub fn is_overflow(&self) -> bool{
77 match self{
78 NTResult::Overflow => true,
79 _=> false,
80 }
81 }
82 pub fn is_comp_overflow(&self) -> bool{
84 match self{
85 NTResult::CompOverflow => true,
86 _=> false,
87 }
88 }
89 pub fn is_comp_exceeded(&self) -> bool{
91 match self{
92 NTResult::CompExceeded => true,
93 _=> false,
94 }
95 }
96 pub fn is_undefined(&self) -> bool{
98 match self{
99 NTResult::Undefined => true,
100 _=> false,
101 }
102 }
103 pub fn from_option( opt: Option<T>, ntvariant: Self) -> Self{
125 match opt{
126 Some(x) => NTResult::Eval(x),
127 None => ntvariant,
128 }
129 }
130
131pub fn map<U: Clone,F: FnOnce(T) -> U>(&self, func : F) -> NTResult<U>{
133 match self {
134 NTResult::Eval(x) => NTResult::Eval((func)(x.clone())),
135 NTResult::Overflow => NTResult::Overflow,
136 NTResult::DNE => NTResult::DNE,
137 NTResult::Infinite => NTResult::Infinite,
138 NTResult::InfiniteSet => NTResult::InfiniteSet,
139 NTResult::CompExceeded => NTResult::CompExceeded,
140 NTResult::CompOverflow => NTResult::CompOverflow,
141 NTResult::Undefined => NTResult::Undefined,
142 }
143 }
144 pub fn ffi(&self)-> (T,u8){
164 match self {
165 NTResult::Eval(x) => (x.clone(),0u8),
166 NTResult::Overflow => (T::default(),1u8),
167 NTResult::DNE => (T::default(),2u8),
168 NTResult::Infinite => (T::default(),3u8),
169 NTResult::InfiniteSet => (T::default(),4u8),
170 NTResult::CompExceeded => (T::default(),5u8),
171 NTResult::CompOverflow => (T::default(),6u8),
172 NTResult::Undefined => (T::default(),7u8),
173 }
174 }
175
176}
177
178impl<T: Clone + Default + Sized + std::fmt::Display > std::fmt::Display for NTResult<T>{
179 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180 match self{
181
182 NTResult::Eval(x) => write!(f,"{}",x),
183 NTResult::Overflow => write!(f,"Overflow"),
184 NTResult::DNE => write!(f,"DNE"),
185 NTResult::Infinite => write!(f,"Infinite"),
186 NTResult::InfiniteSet => write!(f,"Infinite solutions"),
187 NTResult::CompExceeded => write!(f,"Exceeded computation bound"),
188 NTResult::CompOverflow => write!(f,"Overflowed during computation"),
189 NTResult::Undefined => write!(f,"Undefined"),
190 }
191 }
192 }