Skip to content

Commit cb1123f

Browse files
committed
Auto merge of #13802 - weihanglo:refactor, r=epage
refactor(toml): extract dependency-to-source-id to function
2 parents 93edfb9 + d855cd6 commit cb1123f

File tree

1 file changed

+86
-84
lines changed

1 file changed

+86
-84
lines changed

src/cargo/util/toml/mod.rs

+86-84
Original file line numberDiff line numberDiff line change
@@ -1802,90 +1802,7 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
18021802
}
18031803
}
18041804

1805-
let new_source_id = match (
1806-
orig.git.as_ref(),
1807-
orig.path.as_ref(),
1808-
orig.registry.as_ref(),
1809-
orig.registry_index.as_ref(),
1810-
) {
1811-
(Some(_), _, Some(_), _) | (Some(_), _, _, Some(_)) => bail!(
1812-
"dependency ({}) specification is ambiguous. \
1813-
Only one of `git` or `registry` is allowed.",
1814-
name_in_toml
1815-
),
1816-
(_, _, Some(_), Some(_)) => bail!(
1817-
"dependency ({}) specification is ambiguous. \
1818-
Only one of `registry` or `registry-index` is allowed.",
1819-
name_in_toml
1820-
),
1821-
(Some(git), maybe_path, _, _) => {
1822-
if maybe_path.is_some() {
1823-
bail!(
1824-
"dependency ({}) specification is ambiguous. \
1825-
Only one of `git` or `path` is allowed.",
1826-
name_in_toml
1827-
);
1828-
}
1829-
1830-
let n_details = [&orig.branch, &orig.tag, &orig.rev]
1831-
.iter()
1832-
.filter(|d| d.is_some())
1833-
.count();
1834-
1835-
if n_details > 1 {
1836-
bail!(
1837-
"dependency ({}) specification is ambiguous. \
1838-
Only one of `branch`, `tag` or `rev` is allowed.",
1839-
name_in_toml
1840-
);
1841-
}
1842-
1843-
let reference = orig
1844-
.branch
1845-
.clone()
1846-
.map(GitReference::Branch)
1847-
.or_else(|| orig.tag.clone().map(GitReference::Tag))
1848-
.or_else(|| orig.rev.clone().map(GitReference::Rev))
1849-
.unwrap_or(GitReference::DefaultBranch);
1850-
let loc = git.into_url()?;
1851-
1852-
if let Some(fragment) = loc.fragment() {
1853-
let msg = format!(
1854-
"URL fragment `#{}` in git URL is ignored for dependency ({}). \
1855-
If you were trying to specify a specific git revision, \
1856-
use `rev = \"{}\"` in the dependency declaration.",
1857-
fragment, name_in_toml, fragment
1858-
);
1859-
manifest_ctx.warnings.push(msg)
1860-
}
1861-
1862-
SourceId::for_git(&loc, reference)?
1863-
}
1864-
(None, Some(path), _, _) => {
1865-
let path = path.resolve(manifest_ctx.gctx);
1866-
// If the source ID for the package we're parsing is a path
1867-
// source, then we normalize the path here to get rid of
1868-
// components like `..`.
1869-
//
1870-
// The purpose of this is to get a canonical ID for the package
1871-
// that we're depending on to ensure that builds of this package
1872-
// always end up hashing to the same value no matter where it's
1873-
// built from.
1874-
if manifest_ctx.source_id.is_path() {
1875-
let path = manifest_ctx.root.join(path);
1876-
let path = paths::normalize_path(&path);
1877-
SourceId::for_path(&path)?
1878-
} else {
1879-
manifest_ctx.source_id
1880-
}
1881-
}
1882-
(None, None, Some(registry), None) => SourceId::alt_registry(manifest_ctx.gctx, registry)?,
1883-
(None, None, None, Some(registry_index)) => {
1884-
let url = registry_index.into_url()?;
1885-
SourceId::for_registry(&url)?
1886-
}
1887-
(None, None, None, None) => SourceId::crates_io(manifest_ctx.gctx)?,
1888-
};
1805+
let new_source_id = to_dependency_source_id(orig, name_in_toml, manifest_ctx)?;
18891806

18901807
let (pkg_name, explicit_name_in_toml) = match orig.package {
18911808
Some(ref s) => (&s[..], Some(name_in_toml)),
@@ -1964,6 +1881,91 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
19641881
Ok(dep)
19651882
}
19661883

1884+
fn to_dependency_source_id<P: ResolveToPath + Clone>(
1885+
orig: &manifest::TomlDetailedDependency<P>,
1886+
name_in_toml: &str,
1887+
manifest_ctx: &mut ManifestContext<'_, '_>,
1888+
) -> CargoResult<SourceId> {
1889+
match (
1890+
orig.git.as_ref(),
1891+
orig.path.as_ref(),
1892+
orig.registry.as_deref(),
1893+
orig.registry_index.as_ref(),
1894+
) {
1895+
(Some(_git), _, Some(_registry), _) | (Some(_git), _, _, Some(_registry)) => bail!(
1896+
"dependency ({name_in_toml}) specification is ambiguous. \
1897+
Only one of `git` or `registry` is allowed.",
1898+
),
1899+
(_, _, Some(_registry), Some(_registry_index)) => bail!(
1900+
"dependency ({name_in_toml}) specification is ambiguous. \
1901+
Only one of `registry` or `registry-index` is allowed.",
1902+
),
1903+
(Some(_git), Some(_path), None, None) => {
1904+
bail!(
1905+
"dependency ({name_in_toml}) specification is ambiguous. \
1906+
Only one of `git` or `path` is allowed.",
1907+
);
1908+
}
1909+
(Some(git), None, None, None) => {
1910+
let n_details = [&orig.branch, &orig.tag, &orig.rev]
1911+
.iter()
1912+
.filter(|d| d.is_some())
1913+
.count();
1914+
1915+
if n_details > 1 {
1916+
bail!(
1917+
"dependency ({name_in_toml}) specification is ambiguous. \
1918+
Only one of `branch`, `tag` or `rev` is allowed.",
1919+
);
1920+
}
1921+
1922+
let reference = orig
1923+
.branch
1924+
.clone()
1925+
.map(GitReference::Branch)
1926+
.or_else(|| orig.tag.clone().map(GitReference::Tag))
1927+
.or_else(|| orig.rev.clone().map(GitReference::Rev))
1928+
.unwrap_or(GitReference::DefaultBranch);
1929+
let loc = git.into_url()?;
1930+
1931+
if let Some(fragment) = loc.fragment() {
1932+
let msg = format!(
1933+
"URL fragment `#{fragment}` in git URL is ignored for dependency ({name_in_toml}). \
1934+
If you were trying to specify a specific git revision, \
1935+
use `rev = \"{fragment}\"` in the dependency declaration.",
1936+
);
1937+
manifest_ctx.warnings.push(msg);
1938+
}
1939+
1940+
SourceId::for_git(&loc, reference)
1941+
}
1942+
(None, Some(path), _, _) => {
1943+
let path = path.resolve(manifest_ctx.gctx);
1944+
// If the source ID for the package we're parsing is a path
1945+
// source, then we normalize the path here to get rid of
1946+
// components like `..`.
1947+
//
1948+
// The purpose of this is to get a canonical ID for the package
1949+
// that we're depending on to ensure that builds of this package
1950+
// always end up hashing to the same value no matter where it's
1951+
// built from.
1952+
if manifest_ctx.source_id.is_path() {
1953+
let path = manifest_ctx.root.join(path);
1954+
let path = paths::normalize_path(&path);
1955+
SourceId::for_path(&path)
1956+
} else {
1957+
Ok(manifest_ctx.source_id)
1958+
}
1959+
}
1960+
(None, None, Some(registry), None) => SourceId::alt_registry(manifest_ctx.gctx, registry),
1961+
(None, None, None, Some(registry_index)) => {
1962+
let url = registry_index.into_url()?;
1963+
SourceId::for_registry(&url)
1964+
}
1965+
(None, None, None, None) => SourceId::crates_io(manifest_ctx.gctx),
1966+
}
1967+
}
1968+
19671969
pub trait ResolveToPath {
19681970
fn resolve(&self, gctx: &GlobalContext) -> PathBuf;
19691971
}

0 commit comments

Comments
 (0)