Skip to content

Commit a5d2a1d

Browse files
committed
refactor(toml): extract dependency-to-source-id to function
1 parent 955503e commit a5d2a1d

File tree

1 file changed

+85
-75
lines changed

1 file changed

+85
-75
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 85 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,90 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
18101810
}
18111811
}
18121812

1813+
let new_source_id = to_dependency_source_id(orig, name_in_toml, manifest_ctx)?;
1814+
1815+
let (pkg_name, explicit_name_in_toml) = match orig.package {
1816+
Some(ref s) => (&s[..], Some(name_in_toml)),
1817+
None => (name_in_toml, None),
1818+
};
1819+
1820+
let version = orig.version.as_deref();
1821+
let mut dep = Dependency::parse(pkg_name, version, new_source_id)?;
1822+
deprecated_underscore(
1823+
&orig.default_features2,
1824+
&orig.default_features,
1825+
"default-features",
1826+
name_in_toml,
1827+
"dependency",
1828+
manifest_ctx.warnings,
1829+
);
1830+
dep.set_features(orig.features.iter().flatten())
1831+
.set_default_features(orig.default_features().unwrap_or(true))
1832+
.set_optional(orig.optional.unwrap_or(false))
1833+
.set_platform(manifest_ctx.platform.clone());
1834+
if let Some(registry) = &orig.registry {
1835+
let registry_id = SourceId::alt_registry(manifest_ctx.gctx, registry)?;
1836+
dep.set_registry_id(registry_id);
1837+
}
1838+
if let Some(registry_index) = &orig.registry_index {
1839+
let url = registry_index.into_url()?;
1840+
let registry_id = SourceId::for_registry(&url)?;
1841+
dep.set_registry_id(registry_id);
1842+
}
1843+
1844+
if let Some(kind) = kind {
1845+
dep.set_kind(kind);
1846+
}
1847+
if let Some(name_in_toml) = explicit_name_in_toml {
1848+
dep.set_explicit_name_in_toml(name_in_toml);
1849+
}
1850+
1851+
if let Some(p) = orig.public {
1852+
dep.set_public(p);
1853+
}
1854+
1855+
if let (Some(artifact), is_lib, target) = (
1856+
orig.artifact.as_ref(),
1857+
orig.lib.unwrap_or(false),
1858+
orig.target.as_deref(),
1859+
) {
1860+
if manifest_ctx.gctx.cli_unstable().bindeps {
1861+
let artifact = Artifact::parse(&artifact.0, is_lib, target)?;
1862+
if dep.kind() != DepKind::Build
1863+
&& artifact.target() == Some(ArtifactTarget::BuildDependencyAssumeTarget)
1864+
{
1865+
bail!(
1866+
r#"`target = "target"` in normal- or dev-dependencies has no effect ({})"#,
1867+
name_in_toml
1868+
);
1869+
}
1870+
dep.set_artifact(artifact)
1871+
} else {
1872+
bail!("`artifact = …` requires `-Z bindeps` ({})", name_in_toml);
1873+
}
1874+
} else if orig.lib.is_some() || orig.target.is_some() {
1875+
for (is_set, specifier) in [
1876+
(orig.lib.is_some(), "lib"),
1877+
(orig.target.is_some(), "target"),
1878+
] {
1879+
if !is_set {
1880+
continue;
1881+
}
1882+
bail!(
1883+
"'{}' specifier cannot be used without an 'artifact = …' value ({})",
1884+
specifier,
1885+
name_in_toml
1886+
)
1887+
}
1888+
}
1889+
Ok(dep)
1890+
}
1891+
1892+
fn to_dependency_source_id<P: ResolveToPath + Clone>(
1893+
orig: &manifest::TomlDetailedDependency<P>,
1894+
name_in_toml: &str,
1895+
manifest_ctx: &mut ManifestContext<'_, '_>,
1896+
) -> CargoResult<SourceId> {
18131897
let new_source_id = match (
18141898
orig.git.as_ref(),
18151899
orig.path.as_ref(),
@@ -1895,81 +1979,7 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
18951979
(None, None, None, None) => SourceId::crates_io(manifest_ctx.gctx)?,
18961980
};
18971981

1898-
let (pkg_name, explicit_name_in_toml) = match orig.package {
1899-
Some(ref s) => (&s[..], Some(name_in_toml)),
1900-
None => (name_in_toml, None),
1901-
};
1902-
1903-
let version = orig.version.as_deref();
1904-
let mut dep = Dependency::parse(pkg_name, version, new_source_id)?;
1905-
deprecated_underscore(
1906-
&orig.default_features2,
1907-
&orig.default_features,
1908-
"default-features",
1909-
name_in_toml,
1910-
"dependency",
1911-
manifest_ctx.warnings,
1912-
);
1913-
dep.set_features(orig.features.iter().flatten())
1914-
.set_default_features(orig.default_features().unwrap_or(true))
1915-
.set_optional(orig.optional.unwrap_or(false))
1916-
.set_platform(manifest_ctx.platform.clone());
1917-
if let Some(registry) = &orig.registry {
1918-
let registry_id = SourceId::alt_registry(manifest_ctx.gctx, registry)?;
1919-
dep.set_registry_id(registry_id);
1920-
}
1921-
if let Some(registry_index) = &orig.registry_index {
1922-
let url = registry_index.into_url()?;
1923-
let registry_id = SourceId::for_registry(&url)?;
1924-
dep.set_registry_id(registry_id);
1925-
}
1926-
1927-
if let Some(kind) = kind {
1928-
dep.set_kind(kind);
1929-
}
1930-
if let Some(name_in_toml) = explicit_name_in_toml {
1931-
dep.set_explicit_name_in_toml(name_in_toml);
1932-
}
1933-
1934-
if let Some(p) = orig.public {
1935-
dep.set_public(p);
1936-
}
1937-
1938-
if let (Some(artifact), is_lib, target) = (
1939-
orig.artifact.as_ref(),
1940-
orig.lib.unwrap_or(false),
1941-
orig.target.as_deref(),
1942-
) {
1943-
if manifest_ctx.gctx.cli_unstable().bindeps {
1944-
let artifact = Artifact::parse(&artifact.0, is_lib, target)?;
1945-
if dep.kind() != DepKind::Build
1946-
&& artifact.target() == Some(ArtifactTarget::BuildDependencyAssumeTarget)
1947-
{
1948-
bail!(
1949-
r#"`target = "target"` in normal- or dev-dependencies has no effect ({})"#,
1950-
name_in_toml
1951-
);
1952-
}
1953-
dep.set_artifact(artifact)
1954-
} else {
1955-
bail!("`artifact = …` requires `-Z bindeps` ({})", name_in_toml);
1956-
}
1957-
} else if orig.lib.is_some() || orig.target.is_some() {
1958-
for (is_set, specifier) in [
1959-
(orig.lib.is_some(), "lib"),
1960-
(orig.target.is_some(), "target"),
1961-
] {
1962-
if !is_set {
1963-
continue;
1964-
}
1965-
bail!(
1966-
"'{}' specifier cannot be used without an 'artifact = …' value ({})",
1967-
specifier,
1968-
name_in_toml
1969-
)
1970-
}
1971-
}
1972-
Ok(dep)
1982+
Ok(new_source_id)
19731983
}
19741984

19751985
pub trait ResolveToPath {

0 commit comments

Comments
 (0)