diff --git a/src/pbxproj/object/build/list.rs b/src/pbxproj/object/build/list.rs index 28b20f0..68eb1f5 100644 --- a/src/pbxproj/object/build/list.rs +++ b/src/pbxproj/object/build/list.rs @@ -26,10 +26,9 @@ impl XCConfigurationList { &'a self, data: &'a PBXRootObject, ) -> Vec<&'a XCBuildConfiguration> { - let objects = data.objects(); self.build_configuration_references .iter() - .map(|r| Some(objects.get(r)?.as_xc_build_configuration()?)) + .map(|r| Some(data.get(r)?.as_xc_build_configuration()?)) .flatten() .collect() } @@ -50,16 +49,14 @@ impl XCConfigurationList { let mut configurations = vec![]; let debug = XCBuildConfiguration::new("Debug".into(), Default::default(), None); let debug_id = uuid::Uuid::new_v4().to_string(); - data.objects_mut() - .insert(debug_id.clone(), PBXObject::XCBuildConfiguration(debug)); + data.insert(debug_id.clone(), PBXObject::XCBuildConfiguration(debug)); configurations.push(debug_id); let release = XCBuildConfiguration::new("Release".into(), Default::default(), None); let release_id = uuid::Uuid::new_v4().to_string(); - data.objects_mut() - .insert(release_id.clone(), PBXObject::XCBuildConfiguration(release)); + data.insert(release_id.clone(), PBXObject::XCBuildConfiguration(release)); configurations.push(release_id); @@ -70,8 +67,7 @@ impl XCConfigurationList { pub fn object_with_configuration_list(&self, data: &PBXRootObject) -> Option<&PBXObject> { // projects, Native target, aggregateTargets, legacyTargets build_configuration_list_reference - let _objects = data.objects(); - // objects.iter().find(|o| { + // data.iter().find(|o| { // match o { // PBXObject::PBXProject(p) => p // } diff --git a/src/pbxproj/object/collection.rs b/src/pbxproj/object/collection.rs new file mode 100644 index 0000000..0aad051 --- /dev/null +++ b/src/pbxproj/object/collection.rs @@ -0,0 +1,25 @@ +use super::PBXObject; +use std::collections::HashMap; + +/// [`PBXObject`] storage with convenient helper methods +#[derive(Debug, derive_new::new, derive_deref_rs::Deref)] +pub struct PBXObjectCollection(HashMap); + +impl PBXObjectCollection { + /// Get PBXTarget by the target name + pub fn get_target_by_name(&self, target_name: &str) -> Option<(&String, &PBXObject)> { + self.0.iter().find(|(_, o)| { + let target = match o { + PBXObject::PBXAggregateTarget(ref v) => &v.inner, + PBXObject::PBXLegacyTarget(ref v) => &v.inner, + PBXObject::PBXNativeTarget(ref v) => &v.inner, + _ => return false, + }; + if let Some(name) = target.name.as_ref() { + name == target_name + } else { + false + } + }) + } +} diff --git a/src/pbxproj/object/file/group.rs b/src/pbxproj/object/file/group.rs index aa9d664..2b80a73 100644 --- a/src/pbxproj/object/file/group.rs +++ b/src/pbxproj/object/file/group.rs @@ -27,10 +27,9 @@ impl PBXGroup { /// Group children. pub fn children<'a>(&'a self, data: &'a PBXRootObject) -> Vec<&'a PBXObject> { - let objects = data.objects(); self.children_references .iter() - .map(|r| objects.get(r)) + .map(|r| data.get(r)) .flatten() .collect::>() } diff --git a/src/pbxproj/object/mod.rs b/src/pbxproj/object/mod.rs index cd957ec..0010c05 100644 --- a/src/pbxproj/object/mod.rs +++ b/src/pbxproj/object/mod.rs @@ -5,8 +5,10 @@ mod project; mod swift_package; mod target; +mod collection; mod kind; mod product_type; + pub use kind::*; pub use product_type::*; @@ -17,3 +19,5 @@ pub use project::*; pub use swift_package::*; pub use target::*; + +pub use collection::*; diff --git a/src/pbxproj/object/swift_package/remote.rs b/src/pbxproj/object/swift_package/remote.rs index 14a8070..abb597e 100644 --- a/src/pbxproj/object/swift_package/remote.rs +++ b/src/pbxproj/object/swift_package/remote.rs @@ -10,7 +10,7 @@ pub struct XCRemoteSwiftPackageReference { /// Repository url. pub repository_url: Option, /// Version rules. - pub requirement: Option, + pub version_requirement: Option, } impl XCRemoteSwiftPackageReference { @@ -21,6 +21,12 @@ impl XCRemoteSwiftPackageReference { .map(|s| s.split("/").last()) .flatten() } + + /// Get a reference to the xcremote swift package reference's version requirement. + #[must_use] + pub fn version_requirement(&self) -> Option<&XCVersionRequirement> { + self.version_requirement.as_ref() + } } impl TryFrom for XCRemoteSwiftPackageReference { @@ -32,7 +38,7 @@ impl TryFrom for XCRemoteSwiftPackageReference { .remove_value("repository_url") .map(|v| v.try_into().ok()) .flatten(), - requirement: value + version_requirement: value .remove_value("requirement") .map(|v| v.try_into().ok()) .flatten(), diff --git a/src/pbxproj/object/swift_package/version.rs b/src/pbxproj/object/swift_package/version.rs index a476b51..738ae44 100644 --- a/src/pbxproj/object/swift_package/version.rs +++ b/src/pbxproj/object/swift_package/version.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use tap::Pipe; /// [`XCRemoteSwiftPackageReference`] version rules. -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub enum XCVersionRequirement { /// Version can be bumped up to the next major version. UpToNextMajorVersion(String), diff --git a/src/pbxproj/object/target/aggregated.rs b/src/pbxproj/object/target/aggregated.rs index 675a033..12f5416 100644 --- a/src/pbxproj/object/target/aggregated.rs +++ b/src/pbxproj/object/target/aggregated.rs @@ -7,7 +7,7 @@ use derive_deref_rs::Deref; #[derive(Debug, Deref, derive_new::new)] pub struct PBXAggregateTarget { #[deref] - inner: PBXTarget, + pub(crate) inner: PBXTarget, } impl TryFrom for PBXAggregateTarget { diff --git a/src/pbxproj/object/target/legacy.rs b/src/pbxproj/object/target/legacy.rs index b0ab7ed..5bb693c 100644 --- a/src/pbxproj/object/target/legacy.rs +++ b/src/pbxproj/object/target/legacy.rs @@ -15,7 +15,7 @@ pub struct PBXLegacyTarget { /// The directory where the build tool will be invoked during a build pub build_working_directory: Option, #[deref] - inner: PBXTarget, + pub(crate) inner: PBXTarget, } impl TryFrom for PBXLegacyTarget { diff --git a/src/pbxproj/object/target/native.rs b/src/pbxproj/object/target/native.rs index 7d8391b..6ae8b22 100644 --- a/src/pbxproj/object/target/native.rs +++ b/src/pbxproj/object/target/native.rs @@ -9,7 +9,7 @@ pub struct PBXNativeTarget { /// Target product install path. pub product_install_path: Option, #[deref] - inner: PBXTarget, + pub(crate) inner: PBXTarget, } impl PBXNativeTarget { diff --git a/src/pbxproj/rep.rs b/src/pbxproj/rep.rs index 60fd694..6b28df6 100644 --- a/src/pbxproj/rep.rs +++ b/src/pbxproj/rep.rs @@ -1,4 +1,4 @@ -use super::{PBXHashMap, PBXObject}; +use super::{PBXHashMap, PBXObject, PBXObjectCollection}; use anyhow::Result; use std::{ collections::HashMap, @@ -7,7 +7,7 @@ use std::{ use tap::Pipe; /// `Main` Representation of project.pbxproj file -#[derive(Debug, derive_new::new)] +#[derive(Debug, derive_new::new, derive_deref_rs::Deref)] pub struct PBXRootObject { /// archiveVersion archive_version: u8, @@ -16,7 +16,8 @@ pub struct PBXRootObject { /// classes classes: PBXHashMap, /// Objects - objects: HashMap, + #[deref] + objects: PBXObjectCollection, /// rootObjectReference root_object_reference: String, } @@ -45,28 +46,6 @@ impl PBXRootObject { pub fn root_object_reference(&self) -> &str { self.root_object_reference.as_ref() } - - /// Get a reference to the pbxproject data's objects. - // #[must_use] - pub fn objects(&self) -> &HashMap { - &self.objects - } - - /// Get a mutable reference to the pbxproject data's objects. - // #[must_use] - pub fn objects_mut(&mut self) -> &mut HashMap { - &mut self.objects - } - - /// Get object by reference. - pub fn get_object(&self, reference: &str) -> Option<&PBXObject> { - self.objects.get(reference) - } - - /// Get mutable object by reference. - pub fn get_mut_object(&mut self, reference: &str) -> Option<&mut PBXObject> { - self.objects.get_mut(reference) - } } impl TryFrom for PBXRootObject { @@ -87,7 +66,8 @@ impl TryFrom for PBXRootObject { .into_iter() .map(|(k, v)| anyhow::Ok((k, PBXObject::try_from(v)?))) .flatten() - .collect::>(), + .collect::>() + .pipe(PBXObjectCollection::new), root_object_reference, })