Skip to content

Commit fe5c0a3

Browse files
committed
Add --public support for cargo add
1 parent 463f307 commit fe5c0a3

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/bin/cargo/commands/add.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ Example uses:
7676
"ignore-rust-version",
7777
"Ignore `rust-version` specification in packages (unstable)"
7878
),
79+
flag("public", "Mark the dependency as public")
80+
.long_help("Mark the dependency as public
81+
82+
The dependnecy will be visible in both of your crate and outside."),
83+
flag("no-public", "Mark the dependency as private")
84+
.conflicts_with("dev")
85+
.long_help("Mark the dependency as private
86+
87+
The dependnecy will be only visible in your crate other than outside.")
88+
.conflicts_with("dev")
89+
.overrides_with("public"),
90+
7991
])
8092
.arg_manifest_path_without_unsupported_path_tip()
8193
.arg_package("Package to modify")
@@ -235,6 +247,7 @@ fn parse_dependencies(config: &Config, matches: &ArgMatches) -> CargoResult<Vec<
235247
};
236248
let default_features = default_features(matches);
237249
let optional = optional(matches);
250+
let public = public(matches);
238251

239252
let mut crates = matches
240253
.get_many::<String>("crates")
@@ -325,6 +338,7 @@ fn parse_dependencies(config: &Config, matches: &ArgMatches) -> CargoResult<Vec<
325338
features,
326339
default_features,
327340
optional,
341+
public,
328342
registry: registry.clone(),
329343
path: path.map(String::from),
330344
git: git.map(String::from),
@@ -353,6 +367,10 @@ fn optional(matches: &ArgMatches) -> Option<bool> {
353367
resolve_bool_arg(matches.flag("optional"), matches.flag("no-optional"))
354368
}
355369

370+
fn public(matches: &ArgMatches) -> Option<bool> {
371+
resolve_bool_arg(matches.flag("public"), matches.flag("no-public"))
372+
}
373+
356374
fn resolve_bool_arg(yes: bool, no: bool) -> Option<bool> {
357375
match (yes, no) {
358376
(true, false) => Some(true),

src/cargo/ops/cargo_add/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ pub struct DepOp {
244244
/// Whether dependency is optional
245245
pub optional: Option<bool>,
246246

247+
/// Whether dependency is public
248+
pub public: Option<bool>,
249+
247250
/// Registry for looking up dependency version
248251
pub registry: Option<String>,
249252

@@ -758,6 +761,13 @@ fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency {
758761
dependency.optional = None;
759762
}
760763
}
764+
if let Some(value) = arg.public {
765+
if value {
766+
dependency.public = Some(true);
767+
} else {
768+
dependency.public = None;
769+
}
770+
}
761771
if let Some(value) = arg.default_features {
762772
if value {
763773
dependency.default_features = None;
@@ -945,6 +955,9 @@ fn print_action_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) -
945955
if dep.optional().unwrap_or(false) {
946956
write!(message, " optional")?;
947957
}
958+
if dep.public().unwrap_or(false) {
959+
write!(message, " public")?;
960+
}
948961
let section = if section.len() == 1 {
949962
section[0].clone()
950963
} else {

src/cargo/util/toml_mut/dependency.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pub struct Dependency {
2525
/// Whether the dependency is opted-in with a feature flag.
2626
pub optional: Option<bool>,
2727

28+
/// Whether the dependency is marked as public.
29+
pub public: Option<bool>,
30+
2831
/// List of features to add (or None to keep features unchanged).
2932
pub features: Option<IndexSet<String>>,
3033
/// Whether default features are enabled.
@@ -54,6 +57,7 @@ impl Dependency {
5457
source: None,
5558
registry: None,
5659
rename: None,
60+
public: None,
5761
}
5862
}
5963

@@ -163,6 +167,11 @@ impl Dependency {
163167
self.optional
164168
}
165169

170+
/// Get whether the dep is public.
171+
pub fn public(&self) -> Option<bool> {
172+
self.public
173+
}
174+
166175
/// Get the SourceID for this dependency.
167176
pub fn source_id(&self, config: &Config) -> CargoResult<MaybeWorkspace<SourceId>> {
168177
match &self.source.as_ref() {
@@ -325,6 +334,7 @@ impl Dependency {
325334
};
326335

327336
let optional = table.get("optional").and_then(|v| v.as_bool());
337+
let public = table.get("public").and_then(|v| v.as_bool());
328338

329339
let dep = Self {
330340
name,
@@ -334,6 +344,7 @@ impl Dependency {
334344
default_features,
335345
features,
336346
optional,
347+
public,
337348
inherited_features: None,
338349
};
339350
Ok(dep)
@@ -366,6 +377,7 @@ impl Dependency {
366377
crate_root.display()
367378
);
368379
let table: toml_edit::Item = match (
380+
self.public.unwrap_or(false),
369381
self.optional.unwrap_or(false),
370382
self.features.as_ref(),
371383
self.default_features.unwrap_or(true),
@@ -375,21 +387,22 @@ impl Dependency {
375387
) {
376388
// Extra short when version flag only
377389
(
390+
false,
378391
false,
379392
None,
380393
true,
381394
Some(Source::Registry(RegistrySource { version: v })),
382395
None,
383396
None,
384397
) => toml_edit::value(v),
385-
(false, None, true, Some(Source::Workspace(WorkspaceSource {})), None, None) => {
398+
(false, false, None, true, Some(Source::Workspace(WorkspaceSource {})), None, None) => {
386399
let mut table = toml_edit::InlineTable::default();
387400
table.set_dotted(true);
388401
table.insert("workspace", true.into());
389402
toml_edit::value(toml_edit::Value::InlineTable(table))
390403
}
391404
// Other cases are represented as an inline table
392-
(_, _, _, _, _, _) => {
405+
(_, _, _, _, _, _, _) => {
393406
let mut table = toml_edit::InlineTable::default();
394407

395408
match &self.source {
@@ -442,6 +455,9 @@ impl Dependency {
442455
if let Some(v) = self.optional {
443456
table.insert("optional", v.into());
444457
}
458+
if let Some(v) = self.public {
459+
table.insert("public", v.into());
460+
}
445461

446462
toml_edit::value(toml_edit::Value::InlineTable(table))
447463
}
@@ -579,6 +595,15 @@ impl Dependency {
579595
table.remove("optional");
580596
}
581597
}
598+
match self.public {
599+
Some(v) => {
600+
table.set_dotted(false);
601+
overwrite_value(table, "public", v);
602+
}
603+
None => {
604+
table.remove("public");
605+
}
606+
}
582607
} else {
583608
unreachable!("Invalid dependency type: {}", item.type_name());
584609
}

0 commit comments

Comments
 (0)