@@ -5,11 +5,8 @@ use chrono::{DateTime, Utc};
55
66use crate :: rules_based:: {
77 error:: EvaluationError ,
8- ufc:: {
9- Allocation , Assignment , AssignmentReason , CompiledFlagsConfig , Flag , Shard , Split ,
10- VariationType ,
11- } ,
12- Configuration , EvaluationContext , Timestamp ,
8+ ufc:: { Allocation , Assignment , AssignmentReason , CompiledFlagsConfig , Flag , Shard , Split } ,
9+ Configuration , EvaluationContext , ExpectedFlagType , Timestamp ,
1310} ;
1411
1512/// Evaluate the specified feature flag for the given subject and return assigned variation and
@@ -18,7 +15,7 @@ pub fn get_assignment(
1815 configuration : Option < & Configuration > ,
1916 flag_key : & str ,
2017 subject : & EvaluationContext ,
21- expected_type : Option < VariationType > ,
18+ expected_type : ExpectedFlagType ,
2219 now : DateTime < Utc > ,
2320) -> Result < Assignment , EvaluationError > {
2421 let Some ( config) = configuration else {
@@ -37,7 +34,7 @@ impl Configuration {
3734 & self ,
3835 flag_key : & str ,
3936 context : & EvaluationContext ,
40- expected_type : Option < VariationType > ,
37+ expected_type : ExpectedFlagType ,
4138 now : DateTime < Utc > ,
4239 ) -> Result < Assignment , EvaluationError > {
4340 let result = self
@@ -72,16 +69,10 @@ impl CompiledFlagsConfig {
7269 & self ,
7370 flag_key : & str ,
7471 subject : & EvaluationContext ,
75- expected_type : Option < VariationType > ,
72+ expected_type : ExpectedFlagType ,
7673 now : DateTime < Utc > ,
7774 ) -> Result < Assignment , EvaluationError > {
78- let flag = self . get_flag ( flag_key) ?;
79-
80- if let Some ( ty) = expected_type {
81- flag. verify_type ( ty) ?;
82- }
83-
84- flag. eval ( subject, now)
75+ self . get_flag ( flag_key) ?. eval ( subject, expected_type, now)
8576 }
8677
8778 fn get_flag ( & self , flag_key : & str ) -> Result < & Flag , EvaluationError > {
@@ -94,22 +85,19 @@ impl CompiledFlagsConfig {
9485}
9586
9687impl Flag {
97- fn verify_type ( & self , ty : VariationType ) -> Result < ( ) , EvaluationError > {
98- if self . variation_type == ty {
99- Ok ( ( ) )
100- } else {
101- Err ( EvaluationError :: TypeMismatch {
102- expected : ty,
103- found : self . variation_type ,
104- } )
105- }
106- }
107-
10888 fn eval (
10989 & self ,
11090 subject : & EvaluationContext ,
91+ expected_type : ExpectedFlagType ,
11192 now : DateTime < Utc > ,
11293 ) -> Result < Assignment , EvaluationError > {
94+ if !expected_type. is_compatible ( self . variation_type . into ( ) ) {
95+ return Err ( EvaluationError :: TypeMismatch {
96+ expected : expected_type,
97+ found : self . variation_type . into ( ) ,
98+ } ) ;
99+ }
100+
113101 let Some ( ( allocation, ( split, reason) ) ) = self . allocations . iter ( ) . find_map ( |allocation| {
114102 let result = allocation. get_matching_split ( subject, now) ;
115103 result
@@ -212,15 +200,15 @@ mod tests {
212200
213201 use crate :: rules_based:: {
214202 eval:: get_assignment,
215- ufc:: { AssignmentValue , UniversalFlagConfig , VariationType } ,
216- Attribute , Configuration , EvaluationContext , Str ,
203+ ufc:: { AssignmentValue , UniversalFlagConfig } ,
204+ Attribute , Configuration , EvaluationContext , FlagType , Str ,
217205 } ;
218206
219207 #[ derive( Debug , Serialize , Deserialize ) ]
220208 #[ serde( rename_all = "camelCase" ) ]
221209 struct TestCase {
222210 flag : String ,
223- variation_type : VariationType ,
211+ variation_type : FlagType ,
224212 default_value : Arc < serde_json:: value:: RawValue > ,
225213 targeting_key : Str ,
226214 attributes : Arc < HashMap < Str , Attribute > > ,
@@ -251,27 +239,31 @@ mod tests {
251239 let test_cases: Vec < TestCase > = serde_json:: from_reader ( f) . unwrap ( ) ;
252240
253241 for test_case in test_cases {
254- let default_assignment =
255- AssignmentValue :: from_wire ( test_case. variation_type , test_case. default_value )
256- . unwrap ( ) ;
242+ let default_assignment = AssignmentValue :: from_wire (
243+ test_case. variation_type . into ( ) ,
244+ test_case. default_value ,
245+ )
246+ . unwrap ( ) ;
257247
258248 print ! ( "test subject {:?} ... " , test_case. targeting_key) ;
259249 let subject = EvaluationContext :: new ( test_case. targeting_key , test_case. attributes ) ;
260250 let result = get_assignment (
261251 Some ( & config) ,
262252 & test_case. flag ,
263253 & subject,
264- Some ( test_case. variation_type ) ,
254+ test_case. variation_type . into ( ) ,
265255 now,
266256 ) ;
267257
268258 let result_assingment = result
269259 . as_ref ( )
270260 . map ( |assignment| & assignment. value )
271261 . unwrap_or ( & default_assignment) ;
272- let expected_assignment =
273- AssignmentValue :: from_wire ( test_case. variation_type , test_case. result . value )
274- . unwrap ( ) ;
262+ let expected_assignment = AssignmentValue :: from_wire (
263+ test_case. variation_type . into ( ) ,
264+ test_case. result . value ,
265+ )
266+ . unwrap ( ) ;
275267
276268 assert_eq ! ( result_assingment, & expected_assignment) ;
277269 println ! ( "ok" ) ;
0 commit comments