message: Split workspace/validate() into multiple functions
diff: @@ -588,17 +588,17 @@ impl<'cfg> Workspace<'cfg> {
  return Ok(());
  }
  - let mut roots = Vec::new();
  - {
  + self.validate_unique_names()?;
  + self.validate_workspace_roots()?;
  + self.validate_members()?;
  + self.error_if_manifest_not_in_members()?;
  + self.validate_manifest()
  + }
  +
  + fn validate_unique_names(&self) -> CargoResult<()> {
  let mut names = BTreeMap::new();
  for member in self.members.iter() {
  let package = self.packages.get(member);
  - match *package.workspace_config() {
  - WorkspaceConfig::Root(_) => {
  - roots.push(member.parent().unwrap().to_path_buf());
  - }
  - WorkspaceConfig::Member { .. } => {}
  - }
  let name = match *package {
  MaybePackage::Package(ref p) => p.name(),
  MaybePackage::Virtual(_) => continue,
  @@ -614,9 +614,21 @@ impl<'cfg> Workspace<'cfg> {
  );
  }
  }
  + Ok(())
  }
  + fn validate_workspace_roots(&self) -> CargoResult<()> {
  + let roots: Vec<PathBuf> = self
  + .members
  + .iter()
  + .filter(|&member| {
  + let config = self.packages.get(member).workspace_config();
  + matches!(config, WorkspaceConfig::Root(_))
  + })
  + .map(|member| member.parent().unwrap().to_path_buf())
  + .collect();
  match roots.len() {
  + 1 => Ok(()),
  0 => anyhow::bail!(
  "`package.workspace` configuration points to a crate \
  which is not configured with [workspace]: \n\
  @@ -625,7 +637,6 @@ impl<'cfg> Workspace<'cfg> {
  self.current_manifest.display(),
  self.root_manifest.as_ref().unwrap().display()
  ),
  - 1 => {}
  _ => {
  anyhow::bail!(
  "multiple workspace roots found in the same workspace:\n{}",
  @@ -637,7 +648,9 @@ impl<'cfg> Workspace<'cfg> {
  );
  }
  }
  + }
  + fn validate_members(&mut self) -> CargoResult<()> {
  for member in self.members.clone() {
  let root = self.find_root(&member)?;
  if root == self.root_manifest {
  @@ -665,15 +678,20 @@ impl<'cfg> Workspace<'cfg> {
  }
  }
  }
  + Ok(())
  + }
  +
  + fn error_if_manifest_not_in_members(&mut self) -> CargoResult<()> {
  + if self.members.contains(&self.current_manifest) {
  + return Ok(());
  + }
  - if !self.members.contains(&self.current_manifest) {
  let root = self.root_manifest.as_ref().unwrap();
  let root_dir = root.parent().unwrap();
  let current_dir = self.current_manifest.parent().unwrap();
  let root_pkg = self.packages.get(root);
  - // FIXME: Make this more generic by using a relative path resolver between member and
  - // root.
  + // FIXME: Make this more generic by using a relative path resolver between member and root.
  let members_msg = match current_dir.strip_prefix(root_dir) {
  Ok(rel) => format!(
  "this may be fixable by adding `{}` to the \
  @@ -721,6 +739,7 @@ impl<'cfg> Workspace<'cfg> {
  );
  }
  + fn validate_manifest(&mut self) -> CargoResult<()> {
  if let Some(ref root_manifest) = self.root_manifest {
  for pkg in self
  .members()
  @@ -751,7 +770,6 @@ impl<'cfg> Workspace<'cfg> {
  }
  }
  }
  -
  Ok(())
  }
  