From 42b5f9e321c918cef542c120ad21ba8a7339eb29 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 25 Jul 2026 12:16:25 +0900 Subject: refactor(operation): replace OperationInterface with AnyOperation enum Operations are only ever constructed by the dependency resolver, so a plugin has no way to inject an implementation of its own and the set is closed. Modelling it as an enum, like AnyPackage, removes the OperationInterface trait together with its two parallel downcast mechanisms (as_any() + downcast_ref, and as_*_operation()) and the get_package() default method that panicked on UpdateOperation. The PHP idiom `$op instanceof UpdateOperation ? getTargetPackage() : getPackage()`, written out at six call sites, becomes AnyOperation::get_target_package(). InstallationManager's three blocks that matched on the type string and then recovered the type with expect() collapse into exhaustive matches. SolverOperation keeps only its TYPE constant; the shared getOperationType()/__toString() implementations move to AnyOperation, which also drops the five Self::TYPE.to_string() allocations. Co-Authored-By: Claude Opus 5 (1M context) --- .../shirabe/src/dependency_resolver/transaction.rs | 80 ++++++++-------------- 1 file changed, 28 insertions(+), 52 deletions(-) (limited to 'crates/shirabe/src/dependency_resolver/transaction.rs') diff --git a/crates/shirabe/src/dependency_resolver/transaction.rs b/crates/shirabe/src/dependency_resolver/transaction.rs index 07564ed0..4abe85d4 100644 --- a/crates/shirabe/src/dependency_resolver/transaction.rs +++ b/crates/shirabe/src/dependency_resolver/transaction.rs @@ -1,9 +1,9 @@ //! ref: composer/src/Composer/DependencyResolver/Transaction.php +use crate::dependency_resolver::operation::AnyOperation; use crate::dependency_resolver::operation::InstallOperation; use crate::dependency_resolver::operation::MarkAliasInstalledOperation; use crate::dependency_resolver::operation::MarkAliasUninstalledOperation; -use crate::dependency_resolver::operation::OperationInterface; use crate::dependency_resolver::operation::UninstallOperation; use crate::dependency_resolver::operation::UpdateOperation; use crate::package::AliasPackageHandle; @@ -21,7 +21,7 @@ use shirabe_php_shim::{ #[derive(Debug, Clone)] pub struct Transaction { /// @var OperationInterface[] - pub(crate) operations: Vec>, + pub(crate) operations: Vec, /// Packages present at the beginning of the transaction /// @var PackageInterface[] @@ -64,7 +64,7 @@ impl Transaction { this } - pub fn get_operations(&self) -> &Vec> { + pub fn get_operations(&self) -> &Vec { &self.operations } @@ -108,8 +108,8 @@ impl Transaction { } /// @return OperationInterface[] - pub(crate) fn calculate_operations(&mut self) -> Vec> { - let mut operations: Vec> = vec![]; + pub(crate) fn calculate_operations(&mut self) -> Vec { + let mut operations: Vec = vec![]; let mut present_package_map: IndexMap = IndexMap::new(); let mut remove_map: IndexMap = IndexMap::new(); @@ -163,7 +163,7 @@ impl Transaction { if present_alias_map.contains(&alias_key) { remove_alias_map.shift_remove(&alias_key); } else { - operations.push(std::rc::Rc::new(MarkAliasInstalledOperation::new(alias))); + operations.push(MarkAliasInstalledOperation::new(alias).into()); } } else if let Some(source) = present_package_map.get(&package.get_name()).cloned() { // do we need to update? @@ -187,14 +187,12 @@ impl Transaction { || package.get_source_reference() != present.get_source_reference() || abandoned_or_replacement_changed { - operations.push(std::rc::Rc::new(UpdateOperation::new( - source.clone(), - package.clone(), - ))); + operations + .push(UpdateOperation::new(source.clone(), package.clone()).into()); } remove_map.shift_remove(&package.get_name()); } else { - operations.push(std::rc::Rc::new(InstallOperation::new(package.clone()))); + operations.push(InstallOperation::new(package.clone()).into()); remove_map.shift_remove(&package.get_name()); } } @@ -202,16 +200,10 @@ impl Transaction { for (_name, package) in remove_map { // PHP: array_unshift($operations, new Operation\UninstallOperation($package)); - array_unshift( - &mut operations, - std::rc::Rc::new(UninstallOperation::new(package)) - as std::rc::Rc, - ); + array_unshift(&mut operations, UninstallOperation::new(package).into()); } for (_name_version, package) in remove_alias_map { - operations.push(std::rc::Rc::new(MarkAliasUninstalledOperation::new( - package, - ))); + operations.push(MarkAliasUninstalledOperation::new(package).into()); } let operations = self.move_plugins_to_front(operations); @@ -279,29 +271,22 @@ impl Transaction { /// /// @param OperationInterface[] $operations /// @return OperationInterface[] reordered operation list - fn move_plugins_to_front( - &self, - mut operations: Vec>, - ) -> Vec> { - let mut dl_modifying_plugins_no_deps: Vec> = vec![]; - let mut dl_modifying_plugins_with_deps: Vec> = vec![]; + fn move_plugins_to_front(&self, mut operations: Vec) -> Vec { + let mut dl_modifying_plugins_no_deps: Vec = vec![]; + let mut dl_modifying_plugins_with_deps: Vec = vec![]; let mut dl_modifying_plugin_requires: Vec = vec![]; - let mut plugins_no_deps: Vec> = vec![]; - let mut plugins_with_deps: Vec> = vec![]; + let mut plugins_no_deps: Vec = vec![]; + let mut plugins_with_deps: Vec = vec![]; let mut plugin_requires: Vec = vec![]; let mut to_remove: Vec = vec![]; for idx in (0..operations.len()).rev() { let op = &operations[idx]; - let package: PackageInterfaceHandle = if let Some(install_op) = - op.as_ref().as_any().downcast_ref::() - { - install_op.get_package().clone() - } else if let Some(update_op) = op.as_ref().as_any().downcast_ref::() { - update_op.get_target_package().clone() - } else { - continue; + let package: PackageInterfaceHandle = match op { + AnyOperation::Install(install_op) => install_op.get_package(), + AnyOperation::Update(update_op) => update_op.get_target_package(), + _ => continue, }; let extra = package.get_extra(); @@ -373,7 +358,7 @@ impl Transaction { } // PHP: array_merge($dlModifyingPluginsNoDeps, $dlModifyingPluginsWithDeps, $pluginsNoDeps, $pluginsWithDeps, $operations) - let mut result: Vec> = vec![]; + let mut result: Vec = vec![]; result.extend(dl_modifying_plugins_no_deps); result.extend(dl_modifying_plugins_with_deps); result.extend(plugins_no_deps); @@ -387,23 +372,14 @@ impl Transaction { /// /// @param OperationInterface[] $operations /// @return OperationInterface[] reordered operation list - fn move_uninstalls_to_front( - &self, - mut operations: Vec>, - ) -> Vec> { - let mut uninst_ops: Vec> = vec![]; + fn move_uninstalls_to_front(&self, mut operations: Vec) -> Vec { + let mut uninst_ops: Vec = vec![]; let mut to_remove: Vec = vec![]; for (idx, op) in operations.iter().enumerate() { - let is_uninstall = op - .as_ref() - .as_any() - .downcast_ref::() - .is_some() - || op - .as_ref() - .as_any() - .downcast_ref::() - .is_some(); + let is_uninstall = matches!( + op, + AnyOperation::Uninstall(_) | AnyOperation::MarkAliasUninstalled(_) + ); if is_uninstall { uninst_ops.push(op.clone()); to_remove.push(idx); @@ -415,7 +391,7 @@ impl Transaction { operations.remove(idx); } - let mut result: Vec> = vec![]; + let mut result: Vec = vec![]; result.extend(uninst_ops); result.extend(operations); result -- cgit v1.3.1