1+ use md5:: Digest ;
2+
13use super :: * ;
24use std:: {
35 cell:: RefCell ,
@@ -12,15 +14,23 @@ pub type WeakPBXObjectCollection = Weak<RefCell<PBXObjectCollection>>;
1214#[ derive( Default , Debug , derive_new:: new, derive_deref_rs:: Deref ) ]
1315pub struct PBXObjectCollection ( pub ( crate ) HashMap < String , PBXObject > ) ;
1416
17+ use rand:: distributions:: Alphanumeric ;
18+ use rand:: { thread_rng, Rng } ;
19+
1520/// TODO: make collections a HashSet of PBXObject with identifier included?
1621impl PBXObjectCollection {
17- pub ( crate ) fn set_inner ( & mut self , map : HashMap < String , PBXObject > ) {
18- self . 0 = map;
19- }
20-
2122 /// Add new object. same as insert but it auto create id and returns it
2223 pub fn push < O : Into < PBXObject > > ( & mut self , object : O ) -> String {
23- let id = uuid:: Uuid :: new_v4 ( ) . to_string ( ) ;
24+ let data: String = thread_rng ( )
25+ . sample_iter ( & Alphanumeric )
26+ . take ( 20 )
27+ . map ( char:: from)
28+ . collect ( ) ;
29+ let mut hasher = md5:: Md5 :: new ( ) ;
30+ let ref mut buf = [ 0u8 ; 128 ] ;
31+ hasher. update ( & data) ;
32+ let hash = hasher. finalize ( ) ;
33+ let id = base16ct:: upper:: encode_str ( & hash, buf) . unwrap ( ) [ ..24 ] . to_string ( ) ;
2434 self . insert ( id. clone ( ) , object. into ( ) ) ;
2535 id
2636 }
@@ -176,4 +186,8 @@ impl PBXObjectCollection {
176186 . unwrap_or_default ( )
177187 } )
178188 }
189+
190+ pub ( crate ) fn set_inner ( & mut self , map : HashMap < String , PBXObject > ) {
191+ self . 0 = map;
192+ }
179193}
0 commit comments