diff options
Diffstat (limited to 'crates/shirabe/src/repository/vcs/vcs_driver.rs')
| -rw-r--r-- | crates/shirabe/src/repository/vcs/vcs_driver.rs | 119 |
1 files changed, 40 insertions, 79 deletions
diff --git a/crates/shirabe/src/repository/vcs/vcs_driver.rs b/crates/shirabe/src/repository/vcs/vcs_driver.rs index a16930f..3195822 100644 --- a/crates/shirabe/src/repository/vcs/vcs_driver.rs +++ b/crates/shirabe/src/repository/vcs/vcs_driver.rs @@ -11,72 +11,48 @@ use crate::config::Config; use crate::downloader::transport_exception::TransportException; use crate::io::io_interface::IOInterface; use crate::json::json_file::JsonFile; +use crate::repository::vcs::vcs_driver_interface::VcsDriverInterface; use crate::util::filesystem::Filesystem; use crate::util::http::response::Response; use crate::util::http_downloader::HttpDownloader; use crate::util::process_executor::ProcessExecutor; -#[derive(Debug)] -pub struct VcsDriver { - pub(crate) url: String, - pub(crate) origin_url: String, - pub(crate) repo_config: IndexMap<String, PhpMixed>, - pub(crate) io: Box<dyn IOInterface>, - pub(crate) config: Config, - pub(crate) process: ProcessExecutor, - pub(crate) http_downloader: HttpDownloader, - pub(crate) info_cache: IndexMap<String, Option<IndexMap<String, PhpMixed>>>, - pub(crate) cache: Option<Cache>, -} - -impl VcsDriver { - pub fn new( - mut repo_config: IndexMap<String, PhpMixed>, - io: Box<dyn IOInterface>, - config: Config, - http_downloader: HttpDownloader, - process: ProcessExecutor, - ) -> Self { - if let Some(PhpMixed::String(url)) = repo_config.get("url").cloned() { - if Filesystem::is_local_path(&url) { - let platform_path = Filesystem::get_platform_path(&url); - repo_config.insert("url".to_string(), PhpMixed::String(platform_path)); - } - } - - let url = repo_config - .get("url") - .and_then(|v| v.as_string()) - .unwrap_or("") - .to_string(); - - Self { - origin_url: url.clone(), - url, - repo_config, - io, - config, - http_downloader, - process, - info_cache: IndexMap::new(), - cache: None, - } - } +// TODO(phase-b): the constructor is `final` in PHP; concrete implementations must replicate the +// initialization logic (local-path normalization etc.) from the original new() body. +pub trait VcsDriver: VcsDriverInterface { + fn url(&self) -> &str; + fn url_mut(&mut self) -> &mut String; + fn origin_url(&self) -> &str; + fn origin_url_mut(&mut self) -> &mut String; + fn repo_config(&self) -> &IndexMap<String, PhpMixed>; + fn repo_config_mut(&mut self) -> &mut IndexMap<String, PhpMixed>; + fn io(&self) -> &dyn IOInterface; + fn io_mut(&mut self) -> &mut dyn IOInterface; + fn config(&self) -> &Config; + fn config_mut(&mut self) -> &mut Config; + fn process(&self) -> &ProcessExecutor; + fn process_mut(&mut self) -> &mut ProcessExecutor; + fn http_downloader(&self) -> &HttpDownloader; + fn http_downloader_mut(&mut self) -> &mut HttpDownloader; + fn info_cache(&self) -> &IndexMap<String, Option<IndexMap<String, PhpMixed>>>; + fn info_cache_mut(&mut self) -> &mut IndexMap<String, Option<IndexMap<String, PhpMixed>>>; + fn cache(&self) -> Option<&Cache>; + fn cache_mut(&mut self) -> Option<&mut Cache>; - pub(crate) fn should_cache(&self, identifier: &str) -> bool { - self.cache.is_some() && Preg::is_match("{^[a-f0-9]{40}$}iD", identifier).unwrap_or(false) + fn should_cache(&self, identifier: &str) -> bool { + self.cache().is_some() && Preg::is_match("{^[a-f0-9]{40}$}iD", identifier).unwrap_or(false) } - pub fn get_composer_information( + fn get_composer_information( &mut self, identifier: &str, ) -> anyhow::Result<Option<IndexMap<String, PhpMixed>>> { - if !self.info_cache.contains_key(identifier) { + if !self.info_cache().contains_key(identifier) { if self.should_cache(identifier) { - if let Some(res) = self.cache.as_ref().and_then(|c| c.read(identifier)) { + if let Some(res) = self.cache().and_then(|c| c.read(identifier)) { let parsed = JsonFile::parse_json(&res, None)?; - self.info_cache.insert(identifier.to_string(), parsed); - return Ok(self.info_cache.get(identifier).and_then(|v| v.clone())); + self.info_cache_mut().insert(identifier.to_string(), parsed); + return Ok(self.info_cache().get(identifier).and_then(|v| v.clone())); } } @@ -88,17 +64,18 @@ impl VcsDriver { composer_map, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, ); - self.cache.as_ref().map(|c| c.write(identifier, &encoded)); + self.cache().map(|c| c.write(identifier, &encoded)); } } - self.info_cache.insert(identifier.to_string(), composer); + self.info_cache_mut() + .insert(identifier.to_string(), composer); } - Ok(self.info_cache.get(identifier).and_then(|v| v.clone())) + Ok(self.info_cache().get(identifier).and_then(|v| v.clone())) } - pub(crate) fn get_base_composer_information( + fn get_base_composer_information( &mut self, identifier: &str, ) -> anyhow::Result<Option<IndexMap<String, PhpMixed>>> { @@ -137,44 +114,28 @@ impl VcsDriver { Ok(Some(composer)) } - pub fn has_composer_file(&mut self, identifier: &str) -> bool { + fn has_composer_file(&mut self, identifier: &str) -> bool { match self.get_composer_information(identifier) { Ok(Some(_)) => true, _ => false, } } - pub(crate) fn get_scheme(&self) -> &str { + fn get_scheme(&self) -> &str { if extension_loaded("openssl") { return "https"; } "http" } - pub(crate) fn get_contents(&self, url: &str) -> anyhow::Result<Response, TransportException> { + fn get_contents(&self, url: &str) -> anyhow::Result<Response, TransportException> { let options = self - .repo_config + .repo_config() .get("options") .cloned() .unwrap_or(PhpMixed::Array(IndexMap::new())); - self.http_downloader.get(url, &options) + self.http_downloader().get(url, &options) } - pub fn cleanup(&self) {} - - // abstract methods to be implemented by subclasses (via VcsDriverInterface trait) - pub(crate) fn get_file_content( - &self, - file: &str, - identifier: &str, - ) -> anyhow::Result<Option<String>> { - todo!() - } - - pub(crate) fn get_change_date( - &self, - identifier: &str, - ) -> anyhow::Result<Option<chrono::DateTime<chrono::Utc>>> { - todo!() - } + fn cleanup(&self) {} } |
