@@ -601,13 +601,13 @@ pub struct RootMetadataBuilder {
601
601
consistent_snapshot : bool ,
602
602
keys : HashMap < KeyId , PublicKey > ,
603
603
root_threshold : u32 ,
604
- root_key_ids : Vec < KeyId > ,
604
+ root_key_ids : HashSet < KeyId > ,
605
605
snapshot_threshold : u32 ,
606
- snapshot_key_ids : Vec < KeyId > ,
606
+ snapshot_key_ids : HashSet < KeyId > ,
607
607
targets_threshold : u32 ,
608
- targets_key_ids : Vec < KeyId > ,
608
+ targets_key_ids : HashSet < KeyId > ,
609
609
timestamp_threshold : u32 ,
610
- timestamp_key_ids : Vec < KeyId > ,
610
+ timestamp_key_ids : HashSet < KeyId > ,
611
611
}
612
612
613
613
impl RootMetadataBuilder {
@@ -624,13 +624,13 @@ impl RootMetadataBuilder {
624
624
consistent_snapshot : true ,
625
625
keys : HashMap :: new ( ) ,
626
626
root_threshold : 1 ,
627
- root_key_ids : Vec :: new ( ) ,
627
+ root_key_ids : HashSet :: new ( ) ,
628
628
snapshot_threshold : 1 ,
629
- snapshot_key_ids : Vec :: new ( ) ,
629
+ snapshot_key_ids : HashSet :: new ( ) ,
630
630
targets_threshold : 1 ,
631
- targets_key_ids : Vec :: new ( ) ,
631
+ targets_key_ids : HashSet :: new ( ) ,
632
632
timestamp_threshold : 1 ,
633
- timestamp_key_ids : Vec :: new ( ) ,
633
+ timestamp_key_ids : HashSet :: new ( ) ,
634
634
}
635
635
}
636
636
@@ -662,7 +662,7 @@ impl RootMetadataBuilder {
662
662
pub fn root_key ( mut self , public_key : PublicKey ) -> Self {
663
663
let key_id = public_key. key_id ( ) . clone ( ) ;
664
664
self . keys . insert ( key_id. clone ( ) , public_key) ;
665
- self . root_key_ids . push ( key_id) ;
665
+ self . root_key_ids . insert ( key_id) ;
666
666
self
667
667
}
668
668
@@ -676,7 +676,7 @@ impl RootMetadataBuilder {
676
676
pub fn snapshot_key ( mut self , public_key : PublicKey ) -> Self {
677
677
let key_id = public_key. key_id ( ) . clone ( ) ;
678
678
self . keys . insert ( key_id. clone ( ) , public_key) ;
679
- self . snapshot_key_ids . push ( key_id) ;
679
+ self . snapshot_key_ids . insert ( key_id) ;
680
680
self
681
681
}
682
682
@@ -690,7 +690,7 @@ impl RootMetadataBuilder {
690
690
pub fn targets_key ( mut self , public_key : PublicKey ) -> Self {
691
691
let key_id = public_key. key_id ( ) . clone ( ) ;
692
692
self . keys . insert ( key_id. clone ( ) , public_key) ;
693
- self . targets_key_ids . push ( key_id) ;
693
+ self . targets_key_ids . insert ( key_id) ;
694
694
self
695
695
}
696
696
@@ -704,7 +704,7 @@ impl RootMetadataBuilder {
704
704
pub fn timestamp_key ( mut self , public_key : PublicKey ) -> Self {
705
705
let key_id = public_key. key_id ( ) . clone ( ) ;
706
706
self . keys . insert ( key_id. clone ( ) , public_key) ;
707
- self . timestamp_key_ids . push ( key_id) ;
707
+ self . timestamp_key_ids . insert ( key_id) ;
708
708
self
709
709
}
710
710
@@ -813,46 +813,34 @@ impl RootMetadata {
813
813
814
814
/// An iterator over all the trusted root public keys.
815
815
pub fn root_keys ( & self ) -> impl Iterator < Item = & PublicKey > {
816
- self . keys ( ) . iter ( ) . filter_map ( move |( k, v) | {
817
- if self . root . key_ids ( ) . contains ( k) {
818
- Some ( v)
819
- } else {
820
- None
821
- }
822
- } )
823
- }
824
-
825
- /// An iterator over all the trusted snapshot public keys.
826
- pub fn snapshot_keys ( & self ) -> impl Iterator < Item = & PublicKey > {
827
- self . keys ( ) . iter ( ) . filter_map ( move |( k, v) | {
828
- if self . snapshot . key_ids ( ) . contains ( k) {
829
- Some ( v)
830
- } else {
831
- None
832
- }
833
- } )
816
+ self . root
817
+ . key_ids ( )
818
+ . iter ( )
819
+ . filter_map ( |key_id| self . keys . get ( key_id) )
834
820
}
835
821
836
822
/// An iterator over all the trusted targets public keys.
837
823
pub fn targets_keys ( & self ) -> impl Iterator < Item = & PublicKey > {
838
- self . keys ( ) . iter ( ) . filter_map ( move |( k, v) | {
839
- if self . targets . key_ids ( ) . contains ( k) {
840
- Some ( v)
841
- } else {
842
- None
843
- }
844
- } )
824
+ self . targets
825
+ . key_ids ( )
826
+ . iter ( )
827
+ . filter_map ( |key_id| self . keys . get ( key_id) )
828
+ }
829
+
830
+ /// An iterator over all the trusted snapshot public keys.
831
+ pub fn snapshot_keys ( & self ) -> impl Iterator < Item = & PublicKey > {
832
+ self . snapshot
833
+ . key_ids ( )
834
+ . iter ( )
835
+ . filter_map ( |key_id| self . keys . get ( key_id) )
845
836
}
846
837
847
838
/// An iterator over all the trusted timestamp public keys.
848
839
pub fn timestamp_keys ( & self ) -> impl Iterator < Item = & PublicKey > {
849
- self . keys ( ) . iter ( ) . filter_map ( move |( k, v) | {
850
- if self . timestamp . key_ids ( ) . contains ( k) {
851
- Some ( v)
852
- } else {
853
- None
854
- }
855
- } )
840
+ self . timestamp
841
+ . key_ids ( )
842
+ . iter ( )
843
+ . filter_map ( |key_id| self . keys . get ( key_id) )
856
844
}
857
845
858
846
/// An immutable reference to the root role's definition.
@@ -912,12 +900,12 @@ impl<'de> Deserialize<'de> for RootMetadata {
912
900
#[ derive( Clone , Debug , PartialEq , Eq ) ]
913
901
pub struct RoleDefinition {
914
902
threshold : u32 ,
915
- key_ids : Vec < KeyId > ,
903
+ key_ids : HashSet < KeyId > ,
916
904
}
917
905
918
906
impl RoleDefinition {
919
- /// Create a new ` RoleDefinition` with a given threshold and set of authorized ` KeyID` s.
920
- pub fn new ( threshold : u32 , key_ids : Vec < KeyId > ) -> Result < Self > {
907
+ /// Create a new [ RoleDefinition] with a given threshold and set of authorized [ KeyID] s.
908
+ pub fn new ( threshold : u32 , key_ids : HashSet < KeyId > ) -> Result < Self > {
921
909
if threshold < 1 {
922
910
return Err ( Error :: IllegalArgument ( format ! ( "Threshold: {}" , threshold) ) ) ;
923
911
}
@@ -945,7 +933,7 @@ impl RoleDefinition {
945
933
}
946
934
947
935
/// An immutable reference to the set of `KeyID`s that are authorized to sign the role.
948
- pub fn key_ids ( & self ) -> & [ KeyId ] {
936
+ pub fn key_ids ( & self ) -> & HashSet < KeyId > {
949
937
& self . key_ids
950
938
}
951
939
}
@@ -2412,7 +2400,7 @@ mod test {
2412
2400
#[ test]
2413
2401
fn serde_role_definition ( ) {
2414
2402
// keyid ordering must be preserved.
2415
- let keyids = vec ! [
2403
+ let keyids = hashset ! [
2416
2404
KeyId :: from_str( "40e35e8f6003ab90d104710cf88901edab931597401f91c19eeb366060ab3d53" )
2417
2405
. unwrap( ) ,
2418
2406
KeyId :: from_str( "01892c662c8cd79fab20edec21de1dcb8b75d9353103face7fe086ff5c0098e4" )
@@ -2424,8 +2412,8 @@ mod test {
2424
2412
let jsn = json ! ( {
2425
2413
"threshold" : 3 ,
2426
2414
"keyids" : [
2427
- "40e35e8f6003ab90d104710cf88901edab931597401f91c19eeb366060ab3d53" ,
2428
2415
"01892c662c8cd79fab20edec21de1dcb8b75d9353103face7fe086ff5c0098e4" ,
2416
+ "40e35e8f6003ab90d104710cf88901edab931597401f91c19eeb366060ab3d53" ,
2429
2417
"4750eaf6878740780d6f97b12dbad079fb012bec88c78de2c380add56d3f51db" ,
2430
2418
] ,
2431
2419
} ) ;
@@ -3389,7 +3377,7 @@ mod test {
3389
3377
fn deserialize_json_role_definition_illegal_threshold ( ) {
3390
3378
let role_def = RoleDefinition :: new (
3391
3379
1 ,
3392
- vec ! [ Ed25519PrivateKey :: from_pkcs8( ED25519_1_PK8 )
3380
+ hashset ! [ Ed25519PrivateKey :: from_pkcs8( ED25519_1_PK8 )
3393
3381
. unwrap( )
3394
3382
. public( )
3395
3383
. key_id( )
@@ -3407,7 +3395,7 @@ mod test {
3407
3395
3408
3396
let role_def = RoleDefinition :: new (
3409
3397
2 ,
3410
- vec ! [
3398
+ hashset ! [
3411
3399
Ed25519PrivateKey :: from_pkcs8( ED25519_1_PK8 )
3412
3400
. unwrap( )
3413
3401
. public( )
@@ -3457,7 +3445,7 @@ mod test {
3457
3445
. public ( )
3458
3446
. key_id ( )
3459
3447
. clone ( ) ;
3460
- let role_def = RoleDefinition :: new ( 1 , vec ! [ key_id. clone( ) ] ) . unwrap ( ) ;
3448
+ let role_def = RoleDefinition :: new ( 1 , hashset ! [ key_id. clone( ) ] ) . unwrap ( ) ;
3461
3449
let mut jsn = serde_json:: to_value ( & role_def) . unwrap ( ) ;
3462
3450
3463
3451
match jsn. as_object_mut ( ) {
0 commit comments