12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use std:: { convert:: Infallible , fmt:: Display , num:: NonZeroUsize } ;
16
-
17
- use swimos_utilities:: format:: comma_sep;
18
- use thiserror:: Error ;
19
- use tracing:: warn;
15
+ use std:: { convert:: Infallible , fmt:: Display } ;
20
16
17
+ /// Configuration for the downlink runtime to instruct it how to respond to bad envelopes.
21
18
#[ derive( Debug , PartialEq , Eq ) ]
22
19
pub enum BadFrameResponse < E > {
23
20
/// Instruction to ignore the bad envelope.
@@ -63,6 +60,7 @@ impl<E: std::error::Error> BadFrameStrategy<E> for AlwaysAbortStrategy {
63
60
}
64
61
}
65
62
63
+ /// Error type returned by [`ReportStrategy`].
66
64
#[ derive( Debug ) ]
67
65
pub struct ErrReport {
68
66
message : String ,
@@ -84,6 +82,7 @@ impl ErrReport {
84
82
}
85
83
}
86
84
85
+ /// [`BadFrameStrategy`] that creates an [`ErrReport`] from the error returned by the inner strategy.
87
86
pub struct ReportStrategy < S > {
88
87
inner : S ,
89
88
}
@@ -117,6 +116,7 @@ where
117
116
}
118
117
}
119
118
119
+ #[ doc( hidden) ]
120
120
pub type BoxedReportStrategy < ' a , E > = Box < dyn BadFrameStrategy < E , Report = ErrReport > + Send + ' a > ;
121
121
122
122
impl < ' a , E > BadFrameStrategy < E > for BoxedReportStrategy < ' a , E > {
@@ -139,76 +139,10 @@ impl<E> BadFrameStrategy<E> for AlwaysIgnoreStrategy {
139
139
}
140
140
}
141
141
142
- /// A strategy that will ignore several bad envelopes, collecting the errors, and then
143
- /// abort.
144
- pub struct CountStrategy < E > {
145
- max : usize ,
146
- count : usize ,
147
- errors : Vec < E > ,
148
- }
149
-
150
- impl < E > CountStrategy < E > {
151
- pub fn new ( max : NonZeroUsize ) -> Self {
152
- CountStrategy {
153
- max : max. get ( ) ,
154
- count : 0 ,
155
- errors : vec ! [ ] ,
156
- }
157
- }
158
- }
159
-
160
- /// A collection of errors, accumulated by a [`CountStrategy`].
161
- #[ derive( Debug , Error , PartialEq , Eq ) ]
162
- #[ error( "Too many bad frames: {errors}" ) ]
163
- pub struct ErrorLog < E > {
164
- errors : Errors < E > ,
165
- }
166
-
167
- impl < E > AsRef < [ E ] > for ErrorLog < E > {
168
- fn as_ref ( & self ) -> & [ E ] {
169
- & self . errors . 0
170
- }
171
- }
172
-
173
- #[ derive( Debug , PartialEq , Eq ) ]
174
- pub struct Errors < E > ( Vec < E > ) ;
175
-
176
- impl < E : Display > Display for Errors < E > {
177
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
178
- write ! ( f, "[{}]" , comma_sep( & self . 0 ) )
179
- }
180
- }
181
-
182
- impl < E : std:: error:: Error > BadFrameStrategy < E > for CountStrategy < E > {
183
- type Report = ErrorLog < E > ;
184
-
185
- fn failed_with ( & mut self , error : E ) -> BadFrameResponse < Self :: Report > {
186
- let CountStrategy { max, count, errors } = self ;
187
- * count += 1 ;
188
- if * count == * max {
189
- errors. push ( error) ;
190
- * count = 0 ;
191
- BadFrameResponse :: Abort ( ErrorLog {
192
- errors : Errors ( std:: mem:: take ( errors) ) ,
193
- } )
194
- } else {
195
- warn ! (
196
- "Received {n} of a maximum of {m} invalid map messages: {e}" ,
197
- n = * count,
198
- m = * max,
199
- e = error
200
- ) ;
201
- errors. push ( error) ;
202
- BadFrameResponse :: Ignore
203
- }
204
- }
205
- }
206
-
207
142
#[ cfg( test) ]
208
143
mod tests {
209
144
210
145
use super :: * ;
211
- use swimos_utilities:: non_zero_usize;
212
146
use thiserror:: Error ;
213
147
214
148
#[ derive( Debug , Error , PartialEq , Eq ) ]
@@ -231,34 +165,4 @@ mod tests {
231
165
let response = handler. failed_with ( TestError ( "failed" . to_string ( ) ) ) ;
232
166
assert_eq ! ( response, BadFrameResponse :: Ignore ) ;
233
167
}
234
-
235
- const MAX : NonZeroUsize = non_zero_usize ! ( 3 ) ;
236
-
237
- #[ test]
238
- fn count_errors ( ) {
239
- let mut handler = CountStrategy :: new ( MAX ) ;
240
-
241
- let first = handler. failed_with ( TestError ( "first" . to_string ( ) ) ) ;
242
- assert_eq ! ( BadFrameResponse :: Ignore , first) ;
243
-
244
- let second = handler. failed_with ( TestError ( "second" . to_string ( ) ) ) ;
245
- assert_eq ! ( BadFrameResponse :: Ignore , second) ;
246
-
247
- let third = handler. failed_with ( TestError ( "third" . to_string ( ) ) ) ;
248
- match third {
249
- BadFrameResponse :: Abort ( report) => {
250
- assert_eq ! (
251
- report. as_ref( ) ,
252
- & [
253
- TestError ( "first" . to_string( ) ) ,
254
- TestError ( "second" . to_string( ) ) ,
255
- TestError ( "third" . to_string( ) )
256
- ]
257
- )
258
- }
259
- BadFrameResponse :: Ignore => {
260
- panic ! ( "Error ignored." ) ;
261
- }
262
- }
263
- }
264
168
}
0 commit comments