Skip to content

Commit 22dd4bf

Browse files
committed
Auto merge of #11059 - cassaundra:edit-refactor, r=epage
Expose cargo add internals as edit API Move the manifest editing utilities from cargo add to a new `cargo::util::edit` module as part of prep work for `cargo remove` (#10520). No other substantive changes have been made, as this PR is intended only to reduce the refactoring surface area of the implementation of the feature itself. ## Background In cargo edit, there are a number of top-level modules which enable editing of Cargo manifest files (including `src/dependency.rs` and `src/manifest.rs`). In #10472, these files were added instead as a submodule of the cargo add command, with the stated intention of breaking them out later for subsequent `cargo-edit` subcommands. This PR follows through on that expectation. ## Decisions Concerns raised in #10472 regarding this change: - Where should the editing API should live? - Proposal: `cargo::ops::edit` - Justification: precedent has been set by `cargo::ops::resolve` and others to have utils shared by multiple ops live in `cargo::ops`. This is also serves to be a rather conservative API change. - Concerns: the name `edit` could be overly general for those unfamiliar with the cargo edit project (see alternatives) - Alternatives: - `cargo::edit`: this seems to me to be too top level, and would confuse users trying to discover the cargo API - `cargo::util::edit`: if we want to expose this at a higher level, perhaps renaming to act as a counterpart to `crate::util::toml` - For each of these, replace `edit` with `toml_edit`, `toml_mut`, `manifest_edit`, `manifest_mut`, `edit_toml`, `edit_manifest` etc. for a more specific module name - Any more specific naming of types reduce clashes (e.g. `Dependency` or `Manifest` being fairly generic) - Currently the only thing distinguishing these similarly named types is their path, which the `edit` module makes more clear - Alternatives: rename to `EditDependency`/`EditManifest`, `TomlDependency`/`TomlManifest`, etc.
2 parents 0825039 + d1b041d commit 22dd4bf

File tree

7 files changed

+116
-97
lines changed

7 files changed

+116
-97
lines changed

Diff for: src/bin/cargo/commands/add.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use cargo::core::FeatureValue;
66
use cargo::ops::cargo_add::add;
77
use cargo::ops::cargo_add::AddOptions;
88
use cargo::ops::cargo_add::DepOp;
9-
use cargo::ops::cargo_add::DepTable;
109
use cargo::ops::resolve_ws;
1110
use cargo::util::command_prelude::*;
1211
use cargo::util::interning::InternedString;
12+
use cargo::util::toml_mut::manifest::DepTable;
1313
use cargo::CargoResult;
1414

1515
pub fn cli() -> clap::Command<'static> {

Diff for: src/cargo/ops/cargo_add/crate_spec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use anyhow::Context as _;
44

55
use super::Dependency;
6-
use super::RegistrySource;
6+
use crate::util::toml_mut::dependency::RegistrySource;
77
use crate::util::validate_package_name;
88
use crate::CargoResult;
99

Diff for: src/cargo/ops/cargo_add/mod.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Core of cargo-add command
22
33
mod crate_spec;
4-
mod dependency;
5-
mod manifest;
64

75
use std::collections::BTreeMap;
86
use std::collections::BTreeSet;
@@ -26,18 +24,17 @@ use crate::core::Registry;
2624
use crate::core::Shell;
2725
use crate::core::Summary;
2826
use crate::core::Workspace;
27+
use crate::util::toml_mut::dependency::Dependency;
28+
use crate::util::toml_mut::dependency::GitSource;
29+
use crate::util::toml_mut::dependency::MaybeWorkspace;
30+
use crate::util::toml_mut::dependency::PathSource;
31+
use crate::util::toml_mut::dependency::Source;
32+
use crate::util::toml_mut::dependency::WorkspaceSource;
33+
use crate::util::toml_mut::manifest::DepTable;
34+
use crate::util::toml_mut::manifest::LocalManifest;
2935
use crate::CargoResult;
3036
use crate::Config;
3137
use crate_spec::CrateSpec;
32-
use dependency::Dependency;
33-
use dependency::GitSource;
34-
use dependency::PathSource;
35-
use dependency::RegistrySource;
36-
use dependency::Source;
37-
use manifest::LocalManifest;
38-
39-
use crate::ops::cargo_add::dependency::{MaybeWorkspace, WorkspaceSource};
40-
pub use manifest::DepTable;
4138

4239
/// Information on what dependencies should be added
4340
#[derive(Clone, Debug)]

Diff for: src/cargo/util/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub mod rustc;
5757
mod semver_ext;
5858
pub mod to_semver;
5959
pub mod toml;
60+
pub mod toml_mut;
6061
mod vcs;
6162
mod workspace;
6263

0 commit comments

Comments
 (0)