diff options
Diffstat (limited to 'crates/shirabe-metadata-minifier/src')
| -rw-r--r-- | crates/shirabe-metadata-minifier/src/lib.rs | 3 | ||||
| -rw-r--r-- | crates/shirabe-metadata-minifier/src/metadata_minifier.rs | 38 |
2 files changed, 41 insertions, 0 deletions
diff --git a/crates/shirabe-metadata-minifier/src/lib.rs b/crates/shirabe-metadata-minifier/src/lib.rs new file mode 100644 index 0000000..342786f --- /dev/null +++ b/crates/shirabe-metadata-minifier/src/lib.rs @@ -0,0 +1,3 @@ +mod metadata_minifier; + +pub use metadata_minifier::*; diff --git a/crates/shirabe-metadata-minifier/src/metadata_minifier.rs b/crates/shirabe-metadata-minifier/src/metadata_minifier.rs new file mode 100644 index 0000000..3f1a4fc --- /dev/null +++ b/crates/shirabe-metadata-minifier/src/metadata_minifier.rs @@ -0,0 +1,38 @@ +//! ref: composer/vendor/composer/metadata-minifier/src/MetadataMinifier.php + +use indexmap::IndexMap; +use shirabe_php_shim::PhpMixed; + +#[derive(Debug)] +pub struct MetadataMinifier; + +impl MetadataMinifier { + pub fn expand(versions: Vec<IndexMap<String, PhpMixed>>) -> Vec<IndexMap<String, PhpMixed>> { + let mut expanded: Vec<IndexMap<String, PhpMixed>> = Vec::new(); + let mut expanded_version: Option<IndexMap<String, PhpMixed>> = None; + for version_data in versions { + if expanded_version.as_ref().is_none_or(|ev| ev.is_empty()) { + expanded.push(version_data.clone()); + expanded_version = Some(version_data); + continue; + } + + // add any changes from the previous version to the expanded one + let ev = expanded_version.as_mut().unwrap(); + for (key, val) in version_data { + if matches!(&val, PhpMixed::String(s) if s == "__unset") { + ev.shift_remove(&key); + } else { + ev.insert(key, val); + } + } + + expanded.push(ev.clone()); + } + + expanded + } + + // MetadataMinifier::minify() is not ported because it is not used in Composer itself. + // The function is mainly for package repositories. +} |
