@@ -2,8 +2,13 @@ mod kind;
22mod obj;
33mod setget;
44mod source_tree;
5+
56use super :: * ;
6- use std:: { cell:: RefCell , collections:: HashSet , rc:: Rc } ;
7+ use std:: {
8+ cell:: RefCell ,
9+ collections:: HashSet ,
10+ rc:: { Rc , Weak } ,
11+ } ;
712
813pub use kind:: * ;
914pub use source_tree:: * ;
@@ -50,22 +55,11 @@ pub struct PBXFSReference {
5055 /// Version group type. (only relevant for XCVersionGroup)
5156 version_group_type : Option < String > ,
5257
53- pub ( crate ) parent_reference : Option < String > ,
58+ parent : Weak < RefCell < Self > > ,
5459 pub ( crate ) objects : WeakPBXObjectCollection ,
5560}
5661
5762impl PBXFSReference {
58- /// Get a reference to the pbxfile element's parent reference.
59- #[ must_use]
60- pub fn parent ( & self ) -> Option < Rc < RefCell < PBXFSReference > > > {
61- self . objects
62- . upgrade ( ) ?
63- . borrow ( )
64- . get ( self . parent_reference . as_ref ( ) ?) ?
65- . as_pbxfs_reference ( )
66- . map ( |r| r. clone ( ) )
67- }
68-
6963 /// Get Group children.
7064 /// WARN: This will return empty if self is of type file
7165 pub fn children ( & self ) -> Vec < Rc < RefCell < PBXFSReference > > > {
@@ -105,6 +99,27 @@ impl PBXFSReference {
10599 }
106100 } )
107101 }
102+
103+ pub ( crate ) fn assign_parent_to_children ( & self , this : Weak < RefCell < Self > > ) {
104+ if self . is_group ( ) {
105+ self . children ( ) . into_iter ( ) . for_each ( |o| {
106+ let mut fs_reference = o. borrow_mut ( ) ;
107+ fs_reference. parent = this. clone ( ) ;
108+ fs_reference. assign_parent_to_children ( Rc :: downgrade ( & o) )
109+ } ) ;
110+ }
111+ }
112+
113+ /// Set the pbxfsreference's parent.
114+ pub fn set_parent ( & mut self , parent : Weak < RefCell < Self > > ) {
115+ self . parent = parent;
116+ }
117+
118+ /// Get a reference to the pbxfsreference's parent.
119+ #[ must_use]
120+ pub fn parent ( & self ) -> Option < Rc < RefCell < Self > > > {
121+ self . parent . upgrade ( )
122+ }
108123}
109124
110125impl Eq for PBXFSReference { }
@@ -114,7 +129,6 @@ impl PartialEq for PBXFSReference {
114129 && self . source_tree == other. source_tree
115130 && self . path == other. path
116131 && self . name == other. name
117- && self . parent_reference == other. parent_reference
118132 && self . children_references == other. children_references
119133 && self . current_version_reference == other. current_version_reference
120134 && self . version_group_type == other. version_group_type
@@ -134,3 +148,29 @@ impl PartialEq for PBXFSReference {
134148 == other. plist_structure_definition_identifier
135149 }
136150}
151+
152+ #[ test]
153+ fn test_parent ( ) {
154+ use crate :: pbxproj:: test_demo_file;
155+ let project = test_demo_file ! ( demo1) ;
156+ let main_group = project
157+ . objects ( )
158+ . projects ( )
159+ . first ( )
160+ . unwrap ( )
161+ . 1
162+ . borrow ( )
163+ . main_group ( ) ;
164+
165+ let main_group = main_group. borrow ( ) ;
166+
167+ let source_group = main_group. get_subgroup ( "Source" ) . unwrap ( ) ;
168+ let source_group = source_group. borrow ( ) ;
169+ let parent = source_group. parent ( ) ;
170+ println ! ( "{:#?}" , main_group) ;
171+
172+ assert_eq ! (
173+ parent. unwrap( ) . borrow( ) . children_references( ) ,
174+ main_group. children_references( )
175+ )
176+ }
0 commit comments