diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-07-25 12:16:25 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-07-25 12:16:25 +0900 |
| commit | 42b5f9e321c918cef542c120ad21ba8a7339eb29 (patch) | |
| tree | 39f2b5a3cb856de9068f72fb670d170c09a2438f /crates/shirabe/src/dependency_resolver/operation/any_operation.rs | |
| parent | f93d9b49382c8f79fcea4f03361d50bda534bcc4 (diff) | |
| download | php-shirabe-42b5f9e321c918cef542c120ad21ba8a7339eb29.tar.gz php-shirabe-42b5f9e321c918cef542c120ad21ba8a7339eb29.tar.zst php-shirabe-42b5f9e321c918cef542c120ad21ba8a7339eb29.zip | |
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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/dependency_resolver/operation/any_operation.rs')
| -rw-r--r-- | crates/shirabe/src/dependency_resolver/operation/any_operation.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/crates/shirabe/src/dependency_resolver/operation/any_operation.rs b/crates/shirabe/src/dependency_resolver/operation/any_operation.rs new file mode 100644 index 00000000..6675b280 --- /dev/null +++ b/crates/shirabe/src/dependency_resolver/operation/any_operation.rs @@ -0,0 +1,92 @@ +//! ref: composer/src/Composer/DependencyResolver/Operation/OperationInterface.php +//! +//! PHP's `OperationInterface` is not ported as a trait. 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. The shared implementations PHP puts on `SolverOperation` (`getOperationType()`, +//! `__toString()`) live here. + +use crate::dependency_resolver::operation::{ + InstallOperation, MarkAliasInstalledOperation, MarkAliasUninstalledOperation, SolverOperation, + UninstallOperation, UpdateOperation, +}; +use crate::package::PackageInterfaceHandle; + +/// Any solver operation. +#[derive(Debug, Clone)] +pub enum AnyOperation { + Install(InstallOperation), + Update(UpdateOperation), + Uninstall(UninstallOperation), + MarkAliasInstalled(MarkAliasInstalledOperation), + MarkAliasUninstalled(MarkAliasUninstalledOperation), +} + +impl AnyOperation { + pub fn get_operation_type(&self) -> &'static str { + match self { + Self::Install(_) => InstallOperation::TYPE, + Self::Update(_) => UpdateOperation::TYPE, + Self::Uninstall(_) => UninstallOperation::TYPE, + Self::MarkAliasInstalled(_) => MarkAliasInstalledOperation::TYPE, + Self::MarkAliasUninstalled(_) => MarkAliasUninstalledOperation::TYPE, + } + } + + pub fn show(&self, lock: bool) -> String { + match self { + Self::Install(op) => op.show(lock), + Self::Update(op) => op.show(lock), + Self::Uninstall(op) => op.show(lock), + Self::MarkAliasInstalled(op) => op.show(lock), + Self::MarkAliasUninstalled(op) => op.show(lock), + } + } + + /// The package the operation results in. PHP spells this out at every call site as + /// `$op instanceof UpdateOperation ? $op->getTargetPackage() : $op->getPackage()`. + pub fn get_target_package(&self) -> PackageInterfaceHandle { + match self { + Self::Install(op) => op.get_package(), + Self::Update(op) => op.get_target_package(), + Self::Uninstall(op) => op.get_package(), + Self::MarkAliasInstalled(op) => op.get_package().into(), + Self::MarkAliasUninstalled(op) => op.get_package().into(), + } + } +} + +impl std::fmt::Display for AnyOperation { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.show(false)) + } +} + +impl From<InstallOperation> for AnyOperation { + fn from(op: InstallOperation) -> Self { + Self::Install(op) + } +} + +impl From<UpdateOperation> for AnyOperation { + fn from(op: UpdateOperation) -> Self { + Self::Update(op) + } +} + +impl From<UninstallOperation> for AnyOperation { + fn from(op: UninstallOperation) -> Self { + Self::Uninstall(op) + } +} + +impl From<MarkAliasInstalledOperation> for AnyOperation { + fn from(op: MarkAliasInstalledOperation) -> Self { + Self::MarkAliasInstalled(op) + } +} + +impl From<MarkAliasUninstalledOperation> for AnyOperation { + fn from(op: MarkAliasUninstalledOperation) -> Self { + Self::MarkAliasUninstalled(op) + } +} |
