aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/package/base_package.rs
blob: 54abc13ee9d0e67f230862301d5620a5b4cee56a (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! ref: composer/src/Composer/Package/BasePackage.php

use std::sync::LazyLock;

use indexmap::IndexMap;
use shirabe_php_shim::preg_quote;

use crate::package::DisplayMode;
use crate::package::Link;
use crate::package::PackageInterface;
use crate::repository::PlatformRepository;
use crate::repository::RepositoryInterfaceHandle;

pub struct SupportedLinkType {
    pub description: &'static str,
    pub method: &'static str,
}

pub static SUPPORTED_LINK_TYPES: LazyLock<IndexMap<&'static str, SupportedLinkType>> =
    LazyLock::new(|| {
        let mut m = IndexMap::new();
        m.insert(
            "require",
            SupportedLinkType {
                description: "requires",
                method: Link::TYPE_REQUIRE,
            },
        );
        m.insert(
            "conflict",
            SupportedLinkType {
                description: "conflicts",
                method: Link::TYPE_CONFLICT,
            },
        );
        m.insert(
            "provide",
            SupportedLinkType {
                description: "provides",
                method: Link::TYPE_PROVIDE,
            },
        );
        m.insert(
            "replace",
            SupportedLinkType {
                description: "replaces",
                method: Link::TYPE_REPLACE,
            },
        );
        m.insert(
            "require-dev",
            SupportedLinkType {
                description: "requires (for development)",
                method: Link::TYPE_DEV_REQUIRE,
            },
        );
        m
    });

pub static STABILITIES: LazyLock<IndexMap<&'static str, i64>> = LazyLock::new(|| {
    let mut m = IndexMap::new();
    m.insert("stable", 0i64);
    m.insert("RC", 5i64);
    m.insert("beta", 10i64);
    m.insert("alpha", 15i64);
    m.insert("dev", 20i64);
    m
});

pub const STABILITY_STABLE: i64 = 0;
pub const STABILITY_RC: i64 = 5;
pub const STABILITY_BETA: i64 = 10;
pub const STABILITY_ALPHA: i64 = 15;
pub const STABILITY_DEV: i64 = 20;

pub trait BasePackage: PackageInterface + std::fmt::Display {
    fn id(&self) -> i64;
    fn id_mut(&mut self) -> &mut i64;
    fn name(&self) -> &str;
    fn name_mut(&mut self) -> &mut String;
    fn pretty_name(&self) -> &str;
    fn pretty_name_mut(&mut self) -> &mut String;
    fn repository_opt(&self) -> Option<RepositoryInterfaceHandle>;
    fn set_repository_box(&mut self, repository: RepositoryInterfaceHandle);
    fn take_repository(&mut self) -> Option<RepositoryInterfaceHandle>;

    fn as_alias_package_mut(&mut self) -> Option<&mut crate::package::AliasPackage> {
        None
    }

    fn is_platform(&self) -> bool {
        self.repository_opt()
            .map_or(false, |r| r.is::<PlatformRepository>())
    }

    fn get_full_pretty_version(&self, truncate: bool, display_mode: DisplayMode) -> String {
        if display_mode == DisplayMode::SourceRefIfDev
            && (!self.is_dev()
                || (!["hg", "git"].contains(&self.get_source_type().unwrap_or_default())
                    && (self.get_source_type().unwrap_or_default() != ""
                        || self.get_dist_reference().unwrap_or_default() == "")))
        {
            return self.get_pretty_version().to_string();
        }

        let reference: Option<&str> = match display_mode {
            DisplayMode::SourceRefIfDev => {
                if self.get_source_reference().unwrap_or_default() != "" {
                    self.get_source_reference()
                } else {
                    self.get_dist_reference()
                }
            }
            DisplayMode::SourceRef => self.get_source_reference(),
            DisplayMode::DistRef => self.get_dist_reference(),
        };

        let reference = match reference {
            None => return self.get_pretty_version().to_string(),
            Some(r) => r,
        };

        if truncate && reference.len() == 40 && self.get_source_type() != Some("svn") {
            return format!("{} {}", self.get_pretty_version(), &reference[..7]);
        }

        format!("{} {}", self.get_pretty_version(), reference)
    }

    fn get_stability_priority(&self) -> i64 {
        *STABILITIES
            .get(self.get_stability())
            .unwrap_or(&STABILITY_STABLE)
    }

    fn php_clone(&mut self) {
        self.take_repository();
        *self.id_mut() = -1;
    }
}

pub fn package_name_to_regexp(allow_pattern: &str) -> String {
    package_name_to_regexp2(allow_pattern, "{^%s$}i")
}

pub fn package_name_to_regexp2(allow_pattern: &str, wrap: &str) -> String {
    let cleaned = preg_quote(allow_pattern, None).replace("\\*", ".*");
    wrap.replace("%s", &cleaned)
}

pub fn package_names_to_regexp(package_names: &[String], wrap: &str) -> String {
    let patterns: Vec<String> = package_names
        .iter()
        .map(|name| package_name_to_regexp2(name, "%s"))
        .collect();
    wrap.replace("%s", &patterns.join("|"))
}