aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/composer/metadata_minifier/metadata_minifier.rs
blob: f97209d71ad496242c51f91d5a440d8d00b286ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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().map_or(true, |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.
}