cargo_edit_9/
crate_spec.rs

1//! Crate name parsing.
2use super::errors::*;
3use super::get_manifest_from_path;
4use super::Dependency;
5
6/// User-specified crate
7///
8/// This can be a
9/// - Name (e.g. `docopt`)
10/// - Name and a version req (e.g. `docopt@^0.8`)
11/// - Path
12#[derive(Debug)]
13pub enum CrateSpec {
14    /// Name with optional version req
15    PkgId {
16        /// Crate name
17        name: String,
18        /// Optional version requirement
19        version_req: Option<String>,
20    },
21    /// Path to a crate root
22    Path(std::path::PathBuf),
23}
24
25impl CrateSpec {
26    /// Convert a string to a `Crate`
27    pub fn resolve(pkg_id: &str) -> CargoResult<Self> {
28        let path = std::path::Path::new(pkg_id);
29        // For improved error messages, treat it like a path if it looks like one
30        let id = if is_path_like(pkg_id) || path.exists() {
31            Self::Path(path.to_owned())
32        } else {
33            let (name, version) = pkg_id
34                .split_once('@')
35                .map(|(n, v)| (n, Some(v)))
36                .unwrap_or((pkg_id, None));
37
38            let invalid: Vec<_> = name
39                .chars()
40                .filter(|c| !is_name_char(*c))
41                .map(|c| c.to_string())
42                .collect();
43            if !invalid.is_empty() {
44                return Err(anyhow::format_err!(
45                    "Invalid name `{}`: {}",
46                    name,
47                    invalid.join(", ")
48                ));
49            }
50
51            if let Some(version) = version {
52                semver::VersionReq::parse(version)
53                    .with_context(|| format!("Invalid version requirement `{}`", version))?;
54            }
55
56            Self::PkgId {
57                name: name.to_owned(),
58                version_req: version.map(|s| s.to_owned()),
59            }
60        };
61
62        Ok(id)
63    }
64
65    /// Whether the version req is known or not
66    pub fn has_version(&self) -> bool {
67        match self {
68            Self::PkgId {
69                name: _,
70                version_req,
71            } => version_req.is_some(),
72            Self::Path(_path) => {
73                // We'll get it from the manifest
74                true
75            }
76        }
77    }
78
79    /// Generate a dependency entry for this crate specifier
80    pub fn to_dependency(&self) -> CargoResult<Dependency> {
81        let dep = match self {
82            Self::PkgId { name, version_req } => {
83                let mut dep = Dependency::new(name);
84                if let Some(version_req) = version_req {
85                    dep = dep.set_version(version_req);
86                }
87                dep
88            }
89            Self::Path(path) => {
90                let manifest = get_manifest_from_path(path)?;
91                let crate_name = manifest.package_name()?;
92                let path = dunce::canonicalize(path)?;
93                let available_features = manifest.features()?;
94                Dependency::new(crate_name)
95                    .set_path(path)
96                    .set_available_features(available_features)
97            }
98        };
99
100        Ok(dep)
101    }
102}
103
104impl std::str::FromStr for CrateSpec {
105    type Err = Error;
106
107    fn from_str(s: &str) -> CargoResult<Self> {
108        Self::resolve(s)
109    }
110}
111
112fn is_name_char(c: char) -> bool {
113    c.is_alphanumeric() || ['-', '_'].contains(&c)
114}
115
116fn is_path_like(s: &str) -> bool {
117    s.contains('/') || s.contains('\\')
118}