11
11
// specific language governing permissions and limitations under
12
12
// each license.
13
13
14
+ use std:: collections:: HashMap ;
15
+
14
16
use chrono:: { SecondsFormat , Utc } ;
15
17
#[ cfg( feature = "json_schema" ) ]
16
18
use schemars:: JsonSchema ;
@@ -38,6 +40,8 @@ pub struct AssertionMetadata {
38
40
reference : Option < HashedUri > ,
39
41
#[ serde( rename = "dataSource" , skip_serializing_if = "Option::is_none" ) ]
40
42
data_source : Option < DataSource > ,
43
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
44
+ localizations : Option < Vec < HashMap < String , HashMap < String , String > > > > , // not implemented
41
45
#[ serde( rename = "regionOfInterest" , skip_serializing_if = "Option::is_none" ) ]
42
46
region_of_interest : Option < RegionOfInterest > ,
43
47
}
@@ -56,6 +60,7 @@ impl AssertionMetadata {
56
60
) ) ,
57
61
reference : None ,
58
62
data_source : None ,
63
+ localizations : None ,
59
64
region_of_interest : None ,
60
65
}
61
66
}
@@ -70,6 +75,11 @@ impl AssertionMetadata {
70
75
self . date_time . as_deref ( )
71
76
}
72
77
78
+ /// Returns the vec of localizations maps if they exist.
79
+ pub fn localizations ( & self ) -> Option < & Vec < HashMap < String , HashMap < String , String > > > > {
80
+ self . localizations . as_ref ( )
81
+ }
82
+
73
83
/// Returns the [`DataSource`] for this assertion if it exists.
74
84
pub fn data_source ( & self ) -> Option < & DataSource > {
75
85
self . data_source . as_ref ( )
@@ -121,6 +131,15 @@ impl AssertionMetadata {
121
131
self . region_of_interest = Some ( region_of_interest) ;
122
132
self
123
133
}
134
+
135
+ /// Sets all localizations, replacing any existing ones
136
+ pub fn set_localizations (
137
+ mut self ,
138
+ localizations : Vec < HashMap < String , HashMap < String , String > > > ,
139
+ ) -> Self {
140
+ self . localizations = Some ( localizations) ;
141
+ self
142
+ }
124
143
}
125
144
126
145
impl Default for AssertionMetadata {
@@ -312,8 +331,15 @@ pub mod tests {
312
331
#[ test]
313
332
fn assertion_metadata ( ) {
314
333
let review = ReviewRating :: new ( "foo" , Some ( "bar" . to_owned ( ) ) , 3 ) ;
334
+ let mut translations = HashMap :: new ( ) ;
335
+ translations. insert ( "en-US" . to_owned ( ) , "Kevin's Five Cats" . to_owned ( ) ) ;
336
+ translations. insert ( "es-MX" . to_owned ( ) , "Los Cinco Gatos de Kevin" . to_owned ( ) ) ;
337
+ let mut localizations = HashMap :: new ( ) ;
338
+ localizations. insert ( "dc:title" . to_owned ( ) , translations) ;
339
+
315
340
let original = AssertionMetadata :: new ( )
316
341
. add_review ( review)
342
+ . set_localizations ( vec ! [ localizations] )
317
343
. set_region_of_interest ( RegionOfInterest {
318
344
region : vec ! [ Range {
319
345
range_type: RangeType :: Temporal ,
@@ -326,6 +352,7 @@ pub mod tests {
326
352
} ] ,
327
353
..Default :: default ( )
328
354
} ) ;
355
+
329
356
println ! ( "{:}" , & original) ;
330
357
let assertion = original. to_assertion ( ) . expect ( "build_assertion" ) ;
331
358
assert_eq ! ( assertion. mime_type( ) , "application/cbor" ) ;
@@ -334,10 +361,25 @@ pub mod tests {
334
361
println ! ( "{:?}" , serde_json:: to_string( & result) ) ;
335
362
assert_eq ! ( original. date_time, result. date_time) ;
336
363
assert_eq ! ( original. reviews, result. reviews) ;
364
+ let localizations = result. localizations . as_ref ( ) . unwrap ( ) ;
365
+ assert_eq ! (
366
+ localizations[ 0 ]
367
+ . get( "dc:title" )
368
+ . unwrap( )
369
+ . get( "en-US" )
370
+ . unwrap( ) ,
371
+ "Kevin's Five Cats"
372
+ ) ;
337
373
assert_eq ! (
338
374
original. region_of_interest. as_ref( ) ,
339
375
result. region_of_interest( )
340
- )
341
- //assert_eq!(original.reviews.unwrap().len(), 1);
376
+ ) ;
377
+
378
+ // Test round-trip serialization
379
+ let assertion = original. to_assertion ( ) . expect ( "build_assertion" ) ;
380
+ let result = AssertionMetadata :: from_assertion ( & assertion) . expect ( "extract_assertion" ) ;
381
+
382
+ assert_eq ! ( original. localizations, result. localizations) ;
383
+ assert_eq ! ( original. reviews. unwrap( ) . len( ) , 1 ) ;
342
384
}
343
385
}
0 commit comments