Skip to content

Commit

Permalink
ref(pbxproj): root object no more deref to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed Jun 12, 2022
1 parent 08dde37 commit 5e8b060
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
16 changes: 7 additions & 9 deletions src/pbxproj/object/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ mod tests {
))
.unwrap();
let objects = PBXRootObject::try_from(file).unwrap();
let inner = objects.clone();
let inner = inner.borrow();
let inner = objects.objects();
let project = inner
.iter()
.find(|s| s.1.is_pbx_project())
.map(|(_, o)| o.as_pbx_project().unwrap().clone())
.unwrap();
drop(inner);
// root must live to next scope
(objects, project)
}
Expand Down Expand Up @@ -440,7 +440,7 @@ mod tests {

#[test]
fn test_add_swift_package_with_new_version() {
let (objects, project) = get_project("demo1");
let (root, project) = get_project("demo1");
let mut project = project.borrow_mut();
let new_package = project
.add_swift_package(
Expand All @@ -455,18 +455,17 @@ mod tests {
project.packages().unwrap().first().unwrap().1,
"new package should be added to project"
);
let objects = objects.borrow();
println!("{:#?}", objects.build_files());
println!("{:#?}", root.objects().build_files());
assert_eq!(
new_package,
objects.swift_package_references().first().unwrap().1,
root.objects().swift_package_references().first().unwrap().1,
"new package should be added in object collection"
);
}

#[test]
fn test_add_swift_package_new_package() {
let (objects, project) = get_project("demo1");
let (root, project) = get_project("demo1");
let mut project = project.borrow_mut();
let new_package = project
.add_swift_package(
Expand All @@ -481,8 +480,7 @@ mod tests {
project.packages().unwrap()[1].1,
"new package should be added to project"
);
let objects = objects.borrow();
let has_new_package = objects.swift_package_references().iter().any(|v| {
let has_new_package = root.objects().swift_package_references().iter().any(|v| {
v.1.borrow()
.repository_url
.eq(&new_package.borrow().repository_url)
Expand Down
27 changes: 22 additions & 5 deletions src/pbxproj/rep.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::{PBXHashMap, PBXObject, PBXObjectCollection};
use anyhow::Result;
use std::{
cell::RefCell,
cell::{Ref, RefCell, RefMut},
collections::HashMap,
path::{Path, PathBuf},
rc::Rc,
rc::{Rc, Weak},
};
use tap::Pipe;

/// `Main` Representation of project.pbxproj file
#[derive(Debug, derive_new::new, derive_deref_rs::Deref)]
#[derive(Debug, derive_new::new)]
pub struct PBXRootObject {
/// archiveVersion
archive_version: u8,
Expand All @@ -18,8 +18,7 @@ pub struct PBXRootObject {
/// classes
classes: PBXHashMap,
/// Objects
#[deref]
pub(crate) objects: Rc<RefCell<PBXObjectCollection>>,
objects: Rc<RefCell<PBXObjectCollection>>,
/// rootObjectReference
root_object_reference: String,
}
Expand Down Expand Up @@ -48,6 +47,24 @@ impl PBXRootObject {
pub fn root_object_reference(&self) -> &str {
self.root_object_reference.as_ref()
}

/// Get a reference to the pbxroot object's objects.
#[must_use]
pub fn objects<'a>(&'a self) -> Ref<'a, PBXObjectCollection> {
self.objects.borrow()
}

/// Get a reference to the pbxroot object's objects.
#[must_use]
pub fn objects_mut<'a>(&'a self) -> RefMut<'a, PBXObjectCollection> {
self.objects.borrow_mut()
}

/// Get a weak reference to objects.
#[must_use]
pub fn clone_objects<'a>(&'a self) -> Weak<RefCell<PBXObjectCollection>> {
Rc::downgrade(&self.objects)
}
}

impl TryFrom<PBXHashMap> for PBXRootObject {
Expand Down

0 comments on commit 5e8b060

Please sign in to comment.