#
cargo/core/
package_id_spec.rs

    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
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215

    use std::collections::HashMap;

    use anyhow::{bail, Context as _};

    use crate::core::PackageId;
    use crate::core::PackageIdSpec;
    use crate::util::edit_distance;
    use crate::util::errors::CargoResult;

    pub trait PackageIdSpecQuery {
        /// Roughly equivalent to `PackageIdSpec::parse(spec)?.query(i)`
        fn query_str<I>(spec: &str, i: I) -> CargoResult<PackageId>
        where
            I: IntoIterator<Item = PackageId>;

        /// Checks whether the given `PackageId` matches the `PackageIdSpec`.
        fn matches(&self, package_id: PackageId) -> bool;

        /// Checks a list of `PackageId`s to find 1 that matches this `PackageIdSpec`. If 0, 2, or
        /// more are found, then this returns an error.
        fn query<I>(&self, i: I) -> CargoResult<PackageId>
        where
            I: IntoIterator<Item = PackageId>;
    }

    impl PackageIdSpecQuery for PackageIdSpec {
        fn query_str<I>(spec: &str, i: I) -> CargoResult<PackageId>
        where
            I: IntoIterator<Item = PackageId>,
        {
            let i: Vec<_> = i.into_iter().collect();
            let spec = PackageIdSpec::parse(spec).with_context(|| {
                let suggestion = edit_distance::closest_msg(spec, i.iter(), |id| id.name().as_str());
                format!("invalid package ID specification: `{}`{}", spec, suggestion)
            })?;
            spec.query(i)
        }

        fn matches(&self, package_id: PackageId) -> bool {
            if self.name() != package_id.name().as_str() {
                return false;
            }

            if let Some(ref v) = self.partial_version() {
                if !v.matches(package_id.version()) {
                    return false;
                }
            }

            if let Some(u) = &self.url() {
                if *u != package_id.source_id().url() {
                    return false;
                }
            }

            if let Some(k) = &self.kind() {
                if *k != package_id.source_id().kind() {
                    return false;
                }
            }

            true
        }

        fn query<I>(&self, i: I) -> CargoResult<PackageId>
        where
            I: IntoIterator<Item = PackageId>,
        {
            let all_ids: Vec<_> = i.into_iter().collect();
            let mut ids = all_ids.iter().copied().filter(|&id| self.matches(id));
            let Some(ret) = ids.next() else {
                let mut suggestion = String::new();
                let try_spec = |spec: PackageIdSpec, suggestion: &mut String| {
                    let try_matches: Vec<_> = all_ids
                        .iter()
                        .copied()
                        .filter(|&id| spec.matches(id))
                        .collect();
                    if !try_matches.is_empty() {
                        suggestion.push_str("\nDid you mean one of these?\n");
                        minimize(suggestion, &try_matches, self);
                    }
                };
                if self.url().is_some() {
                    let spec = PackageIdSpec::new(self.name().to_owned());
                    let spec = if let Some(version) = self.partial_version().cloned() {
                        spec.with_version(version)
                    } else {
                        spec
                    };
                    try_spec(spec, &mut suggestion);
                }
                if suggestion.is_empty() && self.version().is_some() {
                    try_spec(PackageIdSpec::new(self.name().to_owned()), &mut suggestion);
                }
                if suggestion.is_empty() {
                    suggestion.push_str(&edit_distance::closest_msg(
                        self.name(),
                        all_ids.iter(),
                        |id| id.name().as_str(),
                    ));
                }

                bail!(
                    "package ID specification `{}` did not match any packages{}",
                    self,
                    suggestion
                );
            };
            return match ids.next() {
                Some(other) => {
                    let mut msg = format!(
                        "There are multiple `{}` packages in \
                         your project, and the specification \
                         `{}` is ambiguous.\n\
                         Please re-run this command \
                         with one of the following \
                         specifications:",
                        self.name(),
                        self
                    );
                    let mut vec = vec![ret, other];
                    vec.extend(ids);
                    minimize(&mut msg, &vec, self);
                    Err(anyhow::format_err!("{}", msg))
                }
                None => Ok(ret),
            };

            fn minimize(msg: &mut String, ids: &[PackageId], spec: &PackageIdSpec) {
                let mut version_cnt = HashMap::new();
                for id in ids {
                    *version_cnt.entry(id.version()).or_insert(0) += 1;
                }
                for id in ids {
                    if version_cnt[id.version()] == 1 {
                        msg.push_str(&format!("\n  {}@{}", spec.name(), id.version()));
                    } else {
                        msg.push_str(&format!("\n  {}", id.to_spec()));
                    }
                }
            }
        }
    }

    #[cfg(test)]
    mod tests {
        use super::PackageIdSpec;
        use super::PackageIdSpecQuery;
        use crate::core::{PackageId, SourceId};
        use url::Url;

        #[test]
        fn matching() {
            let url = Url::parse("https://example.com").unwrap();
            let sid = SourceId::for_registry(&url).unwrap();

            let foo = PackageId::try_new("foo", "1.2.3", sid).unwrap();
            assert!(PackageIdSpec::parse("foo").unwrap().matches(foo));
            assert!(!PackageIdSpec::parse("bar").unwrap().matches(foo));
            assert!(PackageIdSpec::parse("foo:1.2.3").unwrap().matches(foo));
            assert!(!PackageIdSpec::parse("foo:1.2.2").unwrap().matches(foo));
            assert!(PackageIdSpec::parse("foo@1.2.3").unwrap().matches(foo));
            assert!(!PackageIdSpec::parse("foo@1.2.2").unwrap().matches(foo));
            assert!(PackageIdSpec::parse("foo@1.2").unwrap().matches(foo));
            assert!(PackageIdSpec::parse("https://example.com#foo@1.2")
                .unwrap()
                .matches(foo));
            assert!(!PackageIdSpec::parse("https://bob.com#foo@1.2")
                .unwrap()
                .matches(foo));
            assert!(PackageIdSpec::parse("registry+https://example.com#foo@1.2")
                .unwrap()
                .matches(foo));
            assert!(!PackageIdSpec::parse("git+https://example.com#foo@1.2")
                .unwrap()
                .matches(foo));

            let meta = PackageId::try_new("meta", "1.2.3+hello", sid).unwrap();
            assert!(PackageIdSpec::parse("meta").unwrap().matches(meta));
            assert!(PackageIdSpec::parse("meta@1").unwrap().matches(meta));
            assert!(PackageIdSpec::parse("meta@1.2").unwrap().matches(meta));
            assert!(PackageIdSpec::parse("meta@1.2.3").unwrap().matches(meta));
            assert!(!PackageIdSpec::parse("meta@1.2.3-alpha.0")
                .unwrap()
                .matches(meta));
            assert!(PackageIdSpec::parse("meta@1.2.3+hello")
                .unwrap()
                .matches(meta));
            assert!(!PackageIdSpec::parse("meta@1.2.3+bye")
                .unwrap()
                .matches(meta));

            let pre = PackageId::try_new("pre", "1.2.3-alpha.0", sid).unwrap();
            assert!(PackageIdSpec::parse("pre").unwrap().matches(pre));
            assert!(!PackageIdSpec::parse("pre@1").unwrap().matches(pre));
            assert!(!PackageIdSpec::parse("pre@1.2").unwrap().matches(pre));
            assert!(!PackageIdSpec::parse("pre@1.2.3").unwrap().matches(pre));
            assert!(PackageIdSpec::parse("pre@1.2.3-alpha.0")
                .unwrap()
                .matches(pre));
            assert!(!PackageIdSpec::parse("pre@1.2.3-alpha.1")
                .unwrap()
                .matches(pre));
            assert!(!PackageIdSpec::parse("pre@1.2.3-beta.0")
                .unwrap()
                .matches(pre));
            assert!(!PackageIdSpec::parse("pre@1.2.3+hello")
                .unwrap()
                .matches(pre));
            assert!(!PackageIdSpec::parse("pre@1.2.3-alpha.0+hello")
                .unwrap()
                .matches(pre));
        }
    }