@@ -29,19 +29,19 @@ impl MinMaxAccumulator {
2929 }
3030 }
3131
32- pub fn new ( sub_type : String ) -> Self {
32+ pub fn new ( sub_type : String ) -> Result < Self , String > {
3333 match sub_type. as_str ( ) {
34- "min" => Self :: new_min ( ) ,
35- "max" => Self :: new_max ( ) ,
36- _ => panic ! ( "sub_type must be 'min' or 'max'" ) ,
34+ "min" => Ok ( Self :: new_min ( ) ) ,
35+ "max" => Ok ( Self :: new_max ( ) ) ,
36+ _ => Err ( format ! ( "sub_type must be 'min' or 'max', got '{sub_type}'" ) ) ,
3737 }
3838 }
3939
40- pub fn with_value ( value : f64 , sub_type : String ) -> Self {
40+ pub fn with_value ( value : f64 , sub_type : String ) -> Result < Self , String > {
4141 if sub_type != "min" && sub_type != "max" {
42- panic ! ( "sub_type must be 'min' or 'max'" ) ;
42+ return Err ( format ! ( "sub_type must be 'min' or 'max', got '{sub_type}'" ) ) ;
4343 }
44- Self { value, sub_type }
44+ Ok ( Self { value, sub_type } )
4545 }
4646
4747 pub fn update ( & mut self , value : f64 ) {
@@ -56,7 +56,7 @@ impl MinMaxAccumulator {
5656 self . value = value;
5757 }
5858 }
59- _ => panic ! ( "Invalid sub_type" ) ,
59+ _ => unreachable ! ( "MinMaxAccumulator sub_type is always 'min' or 'max' " ) ,
6060 }
6161 }
6262
@@ -73,7 +73,7 @@ impl MinMaxAccumulator {
7373 return Err ( "sub_type must be 'min' or 'max'" . into ( ) ) ;
7474 }
7575
76- Ok ( Self :: with_value ( value, sub_type) )
76+ Ok ( Self :: with_value ( value, sub_type) ? )
7777 }
7878
7979 pub fn deserialize_from_bytes ( buffer : & [ u8 ] ) -> Result < Self , Box < dyn std:: error:: Error > > {
@@ -91,7 +91,7 @@ impl MinMaxAccumulator {
9191 _ => return Err ( "Invalid sub_type byte" . into ( ) ) ,
9292 } ;
9393
94- Ok ( Self :: with_value ( value, sub_type) )
94+ Ok ( Self :: with_value ( value, sub_type) ? )
9595 }
9696}
9797
@@ -108,7 +108,7 @@ impl SerializableToSink for MinMaxAccumulator {
108108 let sub_type_byte = match self . sub_type . as_str ( ) {
109109 "min" => 0u8 ,
110110 "max" => 1u8 ,
111- _ => panic ! ( "Invalid sub_type" ) ,
111+ _ => unreachable ! ( "MinMaxAccumulator sub_type is always 'min' or 'max' " ) ,
112112 } ;
113113 bytes. push ( sub_type_byte) ;
114114 bytes
@@ -132,7 +132,7 @@ impl MergeableAccumulator<MinMaxAccumulator> for MinMaxAccumulator {
132132 }
133133 }
134134
135- let mut result = MinMaxAccumulator :: new ( sub_type. clone ( ) ) ;
135+ let mut result = MinMaxAccumulator :: new ( sub_type. clone ( ) ) ? ;
136136
137137 for acc in accumulators {
138138 result. update ( acc. value ) ;
@@ -282,7 +282,7 @@ impl SingleSubpopulationAggregateFactory for MinMaxAccumulatorFactory {
282282 Ok ( Box :: new ( MinMaxAccumulator :: with_value (
283283 result_value,
284284 self . sub_type . clone ( ) ,
285- ) ) )
285+ ) ? ) )
286286 }
287287
288288 fn create_default ( & self ) -> Box < dyn SingleSubpopulationAggregate > {
@@ -330,9 +330,9 @@ mod tests {
330330
331331 #[ test]
332332 fn test_merge_min_accumulators ( ) {
333- let acc1 = MinMaxAccumulator :: with_value ( 10.0 , "min" . to_string ( ) ) ;
334- let acc2 = MinMaxAccumulator :: with_value ( 5.0 , "min" . to_string ( ) ) ;
335- let acc3 = MinMaxAccumulator :: with_value ( 15.0 , "min" . to_string ( ) ) ;
333+ let acc1 = MinMaxAccumulator :: with_value ( 10.0 , "min" . to_string ( ) ) . unwrap ( ) ;
334+ let acc2 = MinMaxAccumulator :: with_value ( 5.0 , "min" . to_string ( ) ) . unwrap ( ) ;
335+ let acc3 = MinMaxAccumulator :: with_value ( 15.0 , "min" . to_string ( ) ) . unwrap ( ) ;
336336
337337 let merged =
338338 <MinMaxAccumulator as MergeableAccumulator < MinMaxAccumulator > >:: merge_accumulators (
@@ -345,9 +345,9 @@ mod tests {
345345
346346 #[ test]
347347 fn test_merge_max_accumulators ( ) {
348- let acc1 = MinMaxAccumulator :: with_value ( 10.0 , "max" . to_string ( ) ) ;
349- let acc2 = MinMaxAccumulator :: with_value ( 5.0 , "max" . to_string ( ) ) ;
350- let acc3 = MinMaxAccumulator :: with_value ( 15.0 , "max" . to_string ( ) ) ;
348+ let acc1 = MinMaxAccumulator :: with_value ( 10.0 , "max" . to_string ( ) ) . unwrap ( ) ;
349+ let acc2 = MinMaxAccumulator :: with_value ( 5.0 , "max" . to_string ( ) ) . unwrap ( ) ;
350+ let acc3 = MinMaxAccumulator :: with_value ( 15.0 , "max" . to_string ( ) ) . unwrap ( ) ;
351351
352352 let merged =
353353 <MinMaxAccumulator as MergeableAccumulator < MinMaxAccumulator > >:: merge_accumulators (
@@ -360,8 +360,8 @@ mod tests {
360360
361361 #[ test]
362362 fn test_merge_different_types_error ( ) {
363- let acc1 = MinMaxAccumulator :: with_value ( 10.0 , "min" . to_string ( ) ) ;
364- let acc2 = MinMaxAccumulator :: with_value ( 5.0 , "max" . to_string ( ) ) ;
363+ let acc1 = MinMaxAccumulator :: with_value ( 10.0 , "min" . to_string ( ) ) . unwrap ( ) ;
364+ let acc2 = MinMaxAccumulator :: with_value ( 5.0 , "max" . to_string ( ) ) . unwrap ( ) ;
365365
366366 assert ! (
367367 <MinMaxAccumulator as MergeableAccumulator <MinMaxAccumulator >>:: merge_accumulators(
@@ -373,7 +373,7 @@ mod tests {
373373
374374 #[ test]
375375 fn test_serialization ( ) {
376- let acc = MinMaxAccumulator :: with_value ( 42.5 , "min" . to_string ( ) ) ;
376+ let acc = MinMaxAccumulator :: with_value ( 42.5 , "min" . to_string ( ) ) . unwrap ( ) ;
377377
378378 // Test JSON serialization
379379 let json = acc. serialize_to_json ( ) ;
@@ -391,7 +391,7 @@ mod tests {
391391 #[ test]
392392 fn test_single_subpopulation_aggregate_trait ( ) {
393393 let acc: Box < dyn SingleSubpopulationAggregate > =
394- Box :: new ( MinMaxAccumulator :: with_value ( 42.0 , "max" . to_string ( ) ) ) ;
394+ Box :: new ( MinMaxAccumulator :: with_value ( 42.0 , "max" . to_string ( ) ) . unwrap ( ) ) ;
395395
396396 assert_eq ! ( acc. query( Statistic :: Max , None ) . unwrap( ) , 42.0 ) ;
397397 assert ! ( acc. query( Statistic :: Min , None ) . is_err( ) ) ;
0 commit comments