diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-06 02:48:36 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-06 02:48:36 +0900 |
| commit | b97e34358be5df05a3db9f5f3ef1502eaa94b1c0 (patch) | |
| tree | e0058b1ad79b60a3751f64ae6d07186cfddf1ee7 /crates/mozart-core/src/config.rs | |
| parent | 5254a9e9b698c3618229f4f802b39a82baf9169a (diff) | |
| download | php-mozart-b97e34358be5df05a3db9f5f3ef1502eaa94b1c0.tar.gz php-mozart-b97e34358be5df05a3db9f5f3ef1502eaa94b1c0.tar.zst php-mozart-b97e34358be5df05a3db9f5f3ef1502eaa94b1c0.zip | |
fix(cache): mirror Composer no-cache and cache-files-maxsize handling
--no-cache redirects COMPOSER_CACHE_DIR to /dev/null (mirrors Application::doRun).
cache_files_maxsize is now u64 with a "300MiB"-style string deserializer.
Cache::new() takes readonly instead of enabled; is_usable() detects null devices.
Diffstat (limited to 'crates/mozart-core/src/config.rs')
| -rw-r--r-- | crates/mozart-core/src/config.rs | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/crates/mozart-core/src/config.rs b/crates/mozart-core/src/config.rs index dd8a9de..cbb3ba6 100644 --- a/crates/mozart-core/src/config.rs +++ b/crates/mozart-core/src/config.rs @@ -10,6 +10,46 @@ use std::collections::BTreeMap; use crate::composer::composer_home; +/// Parse a size string like "300MiB", "1GB", "512k", or a plain integer string +/// into a byte count. Mirrors Composer's `Config::get('cache-files-maxsize')`. +fn parse_size_bytes(s: &str) -> Option<u64> { + let s = s.trim(); + let i = s.find(|c: char| c.is_ascii_alphabetic()).unwrap_or(s.len()); + let num: f64 = s[..i].trim().parse().ok()?; + let multiplier: f64 = match s[i..].trim().chars().next().map(|c| c.to_ascii_lowercase()) { + Some('g') => 1024.0 * 1024.0 * 1024.0, + Some('m') => 1024.0 * 1024.0, + Some('k') => 1024.0, + None => 1.0, + Some(_) => return None, + }; + Some((num * multiplier).max(0.0) as u64) +} + +fn deserialize_size_bytes<'de, D: serde::Deserializer<'de>>(d: D) -> Result<u64, D::Error> { + use serde::de::{Error, Visitor}; + struct V; + impl<'de> Visitor<'de> for V { + type Value = u64; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.write_str("a non-negative integer or a size string like \"300MiB\"") + } + fn visit_u64<E: Error>(self, v: u64) -> Result<u64, E> { + Ok(v) + } + fn visit_i64<E: Error>(self, v: i64) -> Result<u64, E> { + Ok(v.max(0) as u64) + } + fn visit_f64<E: Error>(self, v: f64) -> Result<u64, E> { + Ok(v.max(0.0) as u64) + } + fn visit_str<E: Error>(self, v: &str) -> Result<u64, E> { + parse_size_bytes(v).ok_or_else(|| E::custom(format!("invalid size: {v}"))) + } + } + d.deserialize_any(V) +} + /// Effective Composer configuration for a project. /// /// Known properties are typed fields; anything else lands in `extra`. @@ -31,7 +71,8 @@ pub struct Config { pub cache_repo_dir: String, pub cache_vcs_dir: String, pub cache_files_ttl: u64, - pub cache_files_maxsize: String, + #[serde(deserialize_with = "deserialize_size_bytes")] + pub cache_files_maxsize: u64, pub cache_read_only: bool, pub prepend_autoloader: bool, pub autoloader_suffix: Option<String>, @@ -74,7 +115,7 @@ impl Default for Config { cache_repo_dir: "{$cache-dir}/repo".to_string(), cache_vcs_dir: "{$cache-dir}/vcs".to_string(), cache_files_ttl: 15_552_000, - cache_files_maxsize: "300MiB".to_string(), + cache_files_maxsize: 300 * 1024 * 1024, cache_read_only: false, prepend_autoloader: true, autoloader_suffix: None, |
