aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/repository
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-10 00:54:22 +0900
committernsfisis <nsfisis@gmail.com>2026-06-10 00:54:58 +0900
commite583112899cbea7494ffdd73d7de380dd5f808c4 (patch)
tree1ec002a4d9ab5af5f81ff3bc0ae0ea6a92e44c10 /crates/shirabe/src/repository
parent436e12381dd79e419dce755718be17b66d73e17f (diff)
downloadphp-shirabe-e583112899cbea7494ffdd73d7de380dd5f808c4.tar.gz
php-shirabe-e583112899cbea7494ffdd73d7de380dd5f808c4.tar.zst
php-shirabe-e583112899cbea7494ffdd73d7de380dd5f808c4.zip
feat(phase-c): resolve exception-handling phase-b TODOs
* Catch specific exception types instead of broad/placeholder handling. * Drop the shim Countable trait.
Diffstat (limited to 'crates/shirabe/src/repository')
-rw-r--r--crates/shirabe/src/repository/array_repository.rs34
-rw-r--r--crates/shirabe/src/repository/composer_repository.rs10
-rw-r--r--crates/shirabe/src/repository/composite_repository.rs13
-rw-r--r--crates/shirabe/src/repository/filter_repository.rs26
-rw-r--r--crates/shirabe/src/repository/handle.rs3
-rw-r--r--crates/shirabe/src/repository/installed_array_repository.rs12
-rw-r--r--crates/shirabe/src/repository/installed_filesystem_repository.rs7
-rw-r--r--crates/shirabe/src/repository/installed_repository.rs6
-rw-r--r--crates/shirabe/src/repository/lock_array_repository.rs7
-rw-r--r--crates/shirabe/src/repository/platform_repository.rs6
-rw-r--r--crates/shirabe/src/repository/repository_interface.rs5
-rw-r--r--crates/shirabe/src/repository/root_package_repository.rs6
-rw-r--r--crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs5
-rw-r--r--crates/shirabe/src/repository/vcs/vcs_driver.rs12
-rw-r--r--crates/shirabe/src/repository/vcs_repository.rs1
-rw-r--r--crates/shirabe/src/repository/writable_array_repository.rs5
16 files changed, 66 insertions, 92 deletions
diff --git a/crates/shirabe/src/repository/array_repository.rs b/crates/shirabe/src/repository/array_repository.rs
index 1eeb428..df25235 100644
--- a/crates/shirabe/src/repository/array_repository.rs
+++ b/crates/shirabe/src/repository/array_repository.rs
@@ -7,7 +7,7 @@ use std::rc::Weak;
use anyhow::Result;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::Preg;
-use shirabe_php_shim::{Countable, LogicException, implode, preg_quote, strtolower};
+use shirabe_php_shim::{implode, preg_quote, strtolower};
use shirabe_semver::constraint::AnyConstraint;
use shirabe_semver::constraint::SimpleConstraint;
@@ -39,19 +39,10 @@ impl ArrayRepository {
if self.packages.borrow().is_none() {
self.initialize();
}
-
- if self.packages.borrow().is_none() {
- // TODO(phase-b): propagate the error.
- // PHP: throw new \LogicException('initialize failed to initialize the packages array')
- panic!(
- "{}",
- LogicException {
- message: "initialize failed to initialize the packages array".to_string(),
- code: 0,
- }
- .message
- );
- }
+ assert!(
+ self.packages.borrow().is_some(),
+ "initialize failed to initialize the packages array"
+ );
self.packages
.borrow()
@@ -155,25 +146,22 @@ impl ArrayRepository {
}
}
-impl Countable for ArrayRepository {
+impl RepositoryInterface for ArrayRepository {
/// Returns the number of packages in this repository
- ///
- /// @return 0|positive-int Number of packages
- fn count(&self) -> i64 {
+ fn count(&self) -> anyhow::Result<usize> {
if self.packages.borrow().is_none() {
self.initialize();
}
- self.packages.borrow().as_ref().unwrap().len() as i64
+ Ok(self.packages.borrow().as_ref().unwrap().len())
}
-}
-impl RepositoryInterface for ArrayRepository {
fn get_repo_name(&self) -> String {
+ let count = self.count().expect("ArrayRepository::count is infallible");
format!(
"array repo (defining {} package{})",
- self.count(),
- if self.count() > 1 { "s" } else { "" },
+ count,
+ if count > 1 { "s" } else { "" },
)
}
diff --git a/crates/shirabe/src/repository/composer_repository.rs b/crates/shirabe/src/repository/composer_repository.rs
index f634e23..f07eb73 100644
--- a/crates/shirabe/src/repository/composer_repository.rs
+++ b/crates/shirabe/src/repository/composer_repository.rs
@@ -4,7 +4,7 @@ use indexmap::IndexMap;
use shirabe_external_packages::composer::metadata_minifier::MetadataMinifier;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_php_shim::{
- Countable, InvalidArgumentException, LogicException, PHP_EOL, PhpMixed, RuntimeException,
+ InvalidArgumentException, LogicException, PHP_EOL, PhpMixed, RuntimeException,
UnexpectedValueException, extension_loaded, hash, http_build_query, in_array, json_decode,
parse_url_all, realpath, strtolower, strtr, urlencode, var_export,
};
@@ -1212,7 +1212,7 @@ impl ComposerRepository {
}
}
- if Countable::count(&self.inner) > 0 {
+ if self.inner.count()? > 0 {
for (k, v) in self.inner.get_providers(package_name.to_string())? {
let mut entry: IndexMap<String, PhpMixed> = IndexMap::new();
entry.insert("name".to_string(), PhpMixed::String(v.name));
@@ -3401,13 +3401,11 @@ fn clone_root_data(rd: &RootData) -> RootData {
}
}
-impl shirabe_php_shim::Countable for ComposerRepository {
- fn count(&self) -> i64 {
+impl RepositoryInterface for ComposerRepository {
+ fn count(&self) -> anyhow::Result<usize> {
self.inner.count()
}
-}
-impl RepositoryInterface for ComposerRepository {
fn has_package(&self, package: PackageInterfaceHandle) -> bool {
self.inner.has_package(package)
}
diff --git a/crates/shirabe/src/repository/composite_repository.rs b/crates/shirabe/src/repository/composite_repository.rs
index 97fad44..dafedfc 100644
--- a/crates/shirabe/src/repository/composite_repository.rs
+++ b/crates/shirabe/src/repository/composite_repository.rs
@@ -62,13 +62,16 @@ impl CompositeRepository {
}
}
-impl shirabe_php_shim::Countable for CompositeRepository {
- fn count(&self) -> i64 {
- self.repositories.iter().map(|r| r.count()).sum()
+impl RepositoryInterface for CompositeRepository {
+ fn count(&self) -> anyhow::Result<usize> {
+ let mut total = 0;
+ for repository in &self.repositories {
+ total += repository.count()?;
+ }
+
+ Ok(total)
}
-}
-impl RepositoryInterface for CompositeRepository {
fn get_repo_name(&self) -> String {
let names: Vec<String> = self
.repositories
diff --git a/crates/shirabe/src/repository/filter_repository.rs b/crates/shirabe/src/repository/filter_repository.rs
index a49382a..0e05c25 100644
--- a/crates/shirabe/src/repository/filter_repository.rs
+++ b/crates/shirabe/src/repository/filter_repository.rs
@@ -148,26 +148,20 @@ impl FilterRepository {
}
}
-impl shirabe_php_shim::Countable for FilterRepository {
- fn count(&self) -> i64 {
- if self.repo.count() > 0 {
- // TODO(phase-b): propagate the error
- // self.get_packages()?.len() as i64
- self.repo
- .get_packages()
- .map(|pkgs| {
- pkgs.iter()
- .filter(|p| self.is_allowed(&p.get_name()))
- .count() as i64
- })
- .unwrap_or(0)
+impl RepositoryInterface for FilterRepository {
+ fn count(&self) -> anyhow::Result<usize> {
+ if self.repo.count()? > 0 {
+ Ok(self
+ .repo
+ .get_packages()?
+ .iter()
+ .filter(|p| self.is_allowed(&p.get_name()))
+ .count())
} else {
- 0
+ Ok(0)
}
}
-}
-impl RepositoryInterface for FilterRepository {
fn has_package(&self, package: PackageInterfaceHandle) -> bool {
self.repo.has_package(package)
}
diff --git a/crates/shirabe/src/repository/handle.rs b/crates/shirabe/src/repository/handle.rs
index aa79d5e..6721bb2 100644
--- a/crates/shirabe/src/repository/handle.rs
+++ b/crates/shirabe/src/repository/handle.rs
@@ -4,7 +4,6 @@ use std::cell::{Ref, RefCell, RefMut};
use std::rc::{Rc, Weak};
use indexmap::IndexMap;
-use shirabe_php_shim::Countable;
use shirabe_semver::constraint::AnyConstraint;
use crate::package::BasePackageHandle;
@@ -84,7 +83,7 @@ impl RepositoryInterfaceHandle {
.map(PlatformRepositoryHandle::from_rc)
}
- pub fn count(&self) -> i64 {
+ pub fn count(&self) -> anyhow::Result<usize> {
self.0.borrow().count()
}
diff --git a/crates/shirabe/src/repository/installed_array_repository.rs b/crates/shirabe/src/repository/installed_array_repository.rs
index 64098e9..487a6da 100644
--- a/crates/shirabe/src/repository/installed_array_repository.rs
+++ b/crates/shirabe/src/repository/installed_array_repository.rs
@@ -1,7 +1,6 @@
//! ref: composer/src/Composer/Repository/InstalledArrayRepository.php
use indexmap::IndexMap;
-use shirabe_php_shim::Countable;
use shirabe_semver::constraint::AnyConstraint;
use crate::package::BasePackageHandle;
@@ -41,7 +40,10 @@ impl InstalledRepositoryInterface for InstalledArrayRepository {
}
fn is_fresh(&self) -> bool {
- self.inner.count() == 0
+ self.inner
+ .count()
+ .expect("WritableArrayRepository::count is infallible")
+ == 0
}
}
@@ -87,13 +89,11 @@ impl WritableRepositoryInterface for InstalledArrayRepository {
}
}
-impl Countable for InstalledArrayRepository {
- fn count(&self) -> i64 {
+impl RepositoryInterface for InstalledArrayRepository {
+ fn count(&self) -> anyhow::Result<usize> {
todo!()
}
-}
-impl RepositoryInterface for InstalledArrayRepository {
fn has_package(&self, _package: PackageInterfaceHandle) -> bool {
todo!()
}
diff --git a/crates/shirabe/src/repository/installed_filesystem_repository.rs b/crates/shirabe/src/repository/installed_filesystem_repository.rs
index 1f6be2c..185749e 100644
--- a/crates/shirabe/src/repository/installed_filesystem_repository.rs
+++ b/crates/shirabe/src/repository/installed_filesystem_repository.rs
@@ -2,7 +2,6 @@
use anyhow::Result;
use indexmap::IndexMap;
-use shirabe_php_shim::Countable;
use shirabe_semver::constraint::AnyConstraint;
use crate::json::JsonFile;
@@ -97,13 +96,11 @@ impl WritableRepositoryInterface for InstalledFilesystemRepository {
}
}
-impl Countable for InstalledFilesystemRepository {
- fn count(&self) -> i64 {
+impl RepositoryInterface for InstalledFilesystemRepository {
+ fn count(&self) -> anyhow::Result<usize> {
todo!()
}
-}
-impl RepositoryInterface for InstalledFilesystemRepository {
fn has_package(&self, _package: PackageInterfaceHandle) -> bool {
todo!()
}
diff --git a/crates/shirabe/src/repository/installed_repository.rs b/crates/shirabe/src/repository/installed_repository.rs
index 8b611ae..f6b9643 100644
--- a/crates/shirabe/src/repository/installed_repository.rs
+++ b/crates/shirabe/src/repository/installed_repository.rs
@@ -381,13 +381,11 @@ impl InstalledRepository {
}
}
-impl shirabe_php_shim::Countable for InstalledRepository {
- fn count(&self) -> i64 {
+impl RepositoryInterface for InstalledRepository {
+ fn count(&self) -> anyhow::Result<usize> {
self.inner.count()
}
-}
-impl RepositoryInterface for InstalledRepository {
fn get_repo_name(&self) -> String {
let names: Vec<String> = self
.inner
diff --git a/crates/shirabe/src/repository/lock_array_repository.rs b/crates/shirabe/src/repository/lock_array_repository.rs
index 250ca90..390fae3 100644
--- a/crates/shirabe/src/repository/lock_array_repository.rs
+++ b/crates/shirabe/src/repository/lock_array_repository.rs
@@ -9,7 +9,6 @@ use crate::repository::{
RepositoryInterfaceWeakHandle, SearchResult,
};
use indexmap::IndexMap;
-use shirabe_php_shim::Countable;
use shirabe_semver::constraint::AnyConstraint;
#[derive(Debug)]
@@ -31,13 +30,11 @@ impl LockArrayRepository {
}
}
-impl Countable for LockArrayRepository {
- fn count(&self) -> i64 {
+impl RepositoryInterface for LockArrayRepository {
+ fn count(&self) -> anyhow::Result<usize> {
self.inner.count()
}
-}
-impl RepositoryInterface for LockArrayRepository {
fn has_package(&self, package: PackageInterfaceHandle) -> bool {
self.inner.has_package(package)
}
diff --git a/crates/shirabe/src/repository/platform_repository.rs b/crates/shirabe/src/repository/platform_repository.rs
index a35874d..1dec137 100644
--- a/crates/shirabe/src/repository/platform_repository.rs
+++ b/crates/shirabe/src/repository/platform_repository.rs
@@ -1939,13 +1939,11 @@ impl PlatformRepository {
}
}
-impl shirabe_php_shim::Countable for PlatformRepository {
- fn count(&self) -> i64 {
+impl crate::repository::RepositoryInterface for PlatformRepository {
+ fn count(&self) -> anyhow::Result<usize> {
self.inner.count()
}
-}
-impl crate::repository::RepositoryInterface for PlatformRepository {
fn has_package(&self, package: PackageInterfaceHandle) -> bool {
self.inner.has_package(package)
}
diff --git a/crates/shirabe/src/repository/repository_interface.rs b/crates/shirabe/src/repository/repository_interface.rs
index e3a5548..6dbc01d 100644
--- a/crates/shirabe/src/repository/repository_interface.rs
+++ b/crates/shirabe/src/repository/repository_interface.rs
@@ -4,7 +4,6 @@ use crate::package::BasePackageHandle;
use crate::package::PackageInterfaceHandle;
use crate::repository::AdvisoryProviderInterface;
use indexmap::IndexMap;
-use shirabe_php_shim::Countable;
use shirabe_semver::constraint::AnyConstraint;
pub enum FindPackageConstraint {
@@ -52,7 +51,9 @@ pub const SEARCH_FULLTEXT: i64 = 0;
pub const SEARCH_NAME: i64 = 1;
pub const SEARCH_VENDOR: i64 = 2;
-pub trait RepositoryInterface: Countable + std::fmt::Debug {
+pub trait RepositoryInterface: std::fmt::Debug {
+ fn count(&self) -> anyhow::Result<usize>;
+
fn has_package(&self, package: PackageInterfaceHandle) -> bool;
fn find_package(
diff --git a/crates/shirabe/src/repository/root_package_repository.rs b/crates/shirabe/src/repository/root_package_repository.rs
index 985f86e..4357ed6 100644
--- a/crates/shirabe/src/repository/root_package_repository.rs
+++ b/crates/shirabe/src/repository/root_package_repository.rs
@@ -25,13 +25,11 @@ impl RootPackageRepository {
}
}
-impl shirabe_php_shim::Countable for RootPackageRepository {
- fn count(&self) -> i64 {
+impl RepositoryInterface for RootPackageRepository {
+ fn count(&self) -> anyhow::Result<usize> {
self.inner.count()
}
-}
-impl RepositoryInterface for RootPackageRepository {
fn has_package(&self, package: PackageInterfaceHandle) -> bool {
self.inner.has_package(package)
}
diff --git a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs
index de1f814..7eb253a 100644
--- a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs
+++ b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs
@@ -707,7 +707,6 @@ impl GitBitbucketDriver {
match self.inner.get_contents(url) {
Ok(r) => Ok(r),
Err(e) => {
- // TODO(phase-b): only handle TransportException
let mut bitbucket_util = Bitbucket::new(
self.inner.io.clone(),
self.inner.config.clone(),
@@ -772,7 +771,9 @@ impl GitBitbucketDriver {
match self.setup_fallback_driver(&self.generate_ssh_url()) {
Ok(()) => Ok(true),
Err(e) => {
- // TODO(phase-b): only catch RuntimeException
+ // TODO(phase-c): PHP catches \RuntimeException (and all its subclasses), letting
+ // other exceptions propagate without this cleanup. Modeling that precisely needs the
+ // PHP exception hierarchy, which is intentionally not reproduced (see CLAUDE.md).
self.fallback_driver = None;
self.inner.io.write_error(&format!(
diff --git a/crates/shirabe/src/repository/vcs/vcs_driver.rs b/crates/shirabe/src/repository/vcs/vcs_driver.rs
index dc88387..1c05933 100644
--- a/crates/shirabe/src/repository/vcs/vcs_driver.rs
+++ b/crates/shirabe/src/repository/vcs/vcs_driver.rs
@@ -78,11 +78,13 @@ impl VcsDriverBase {
PhpMixed::Array(a) => a.into_iter().map(|(k, v)| (k, *v)).collect(),
_ => IndexMap::new(),
};
- // TODO(phase-b): map anyhow::Error from HttpDownloader::get into TransportException.
self.http_downloader
.borrow_mut()
.get(url, options)
- .map_err(|e| TransportException::new(e.to_string(), 0))
+ .map_err(|e| match e.downcast::<TransportException>() {
+ Ok(te) => te,
+ Err(other) => TransportException::new(other.to_string(), 0),
+ })
}
// Helper for concrete drivers: produces the same value as the trait default
@@ -319,11 +321,13 @@ pub trait VcsDriver: VcsDriverInterface {
PhpMixed::Array(a) => a.into_iter().map(|(k, v)| (k, *v)).collect(),
_ => IndexMap::new(),
};
- // TODO(phase-b): map anyhow::Error from HttpDownloader::get into TransportException.
self.http_downloader()
.borrow_mut()
.get(url, options)
- .map_err(|e| TransportException::new(e.to_string(), 0))
+ .map_err(|e| match e.downcast::<TransportException>() {
+ Ok(te) => te,
+ Err(other) => TransportException::new(other.to_string(), 0),
+ })
}
fn cleanup(&self) {}
diff --git a/crates/shirabe/src/repository/vcs_repository.rs b/crates/shirabe/src/repository/vcs_repository.rs
index 7817172..ef58fe2 100644
--- a/crates/shirabe/src/repository/vcs_repository.rs
+++ b/crates/shirabe/src/repository/vcs_repository.rs
@@ -315,7 +315,6 @@ impl VcsRepository {
}
Ok(None) => {}
Err(e) => {
- // TODO(phase-b): unify exception handling below
if let Some(te) = e.downcast_ref::<TransportException>() {
if self.should_rethrow_transport_exception(te) {
return Err(e);
diff --git a/crates/shirabe/src/repository/writable_array_repository.rs b/crates/shirabe/src/repository/writable_array_repository.rs
index 1c07b8c..bf9d094 100644
--- a/crates/shirabe/src/repository/writable_array_repository.rs
+++ b/crates/shirabe/src/repository/writable_array_repository.rs
@@ -5,7 +5,6 @@ use crate::repository::ArrayRepository;
use crate::repository::RepositoryInterface;
use crate::repository::RepositoryInterfaceWeakHandle;
use anyhow::Result;
-use shirabe_php_shim::Countable;
#[derive(Debug)]
pub struct WritableArrayRepository {
@@ -84,7 +83,7 @@ impl WritableArrayRepository {
self.inner.get_repo_name()
}
- pub fn count(&self) -> i64 {
- Countable::count(&self.inner)
+ pub fn count(&self) -> anyhow::Result<usize> {
+ self.inner.count()
}
}