Skip to content

Commit 99fafbc

Browse files
committed
feat(trim-paths): parsing in mainfest and config
1 parent 6e748f8 commit 99fafbc

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/cargo/core/profiles.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::core::dependency::Artifact;
2626
use crate::core::resolver::features::FeaturesFor;
2727
use crate::core::{PackageId, PackageIdSpec, Resolve, Shell, Target, Workspace};
2828
use crate::util::interning::InternedString;
29+
use crate::util::toml::ProfileTrimPaths;
2930
use crate::util::toml::{
3031
ProfilePackageSpec, StringOrBool, TomlDebugInfo, TomlProfile, TomlProfiles,
3132
};
@@ -555,6 +556,9 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
555556
if let Some(flags) = &toml.rustflags {
556557
profile.rustflags = flags.clone();
557558
}
559+
if let Some(trim_paths) = &toml.trim_paths {
560+
profile.trim_paths = trim_paths.clone();
561+
}
558562
profile.strip = match toml.strip {
559563
Some(StringOrBool::Bool(true)) => Strip::Named(InternedString::new("symbols")),
560564
None | Some(StringOrBool::Bool(false)) => Strip::None,
@@ -598,6 +602,9 @@ pub struct Profile {
598602
#[serde(skip_serializing_if = "Vec::is_empty")] // remove when `rustflags` is stablized
599603
// Note that `rustflags` is used for the cargo-feature `profile_rustflags`
600604
pub rustflags: Vec<InternedString>,
605+
#[serde(skip_serializing_if = "Vec::is_empty")]
606+
// remove when `-Ztrim-paths` is stablized
607+
pub trim_paths: Vec<ProfileTrimPaths>,
601608
}
602609

603610
impl Default for Profile {
@@ -618,6 +625,7 @@ impl Default for Profile {
618625
panic: PanicStrategy::Unwind,
619626
strip: Strip::None,
620627
rustflags: vec![],
628+
trim_paths: Vec::new(),
621629
}
622630
}
623631
}
@@ -646,6 +654,7 @@ compact_debug! {
646654
panic
647655
strip
648656
rustflags
657+
trim_paths
649658
)]
650659
}
651660
}
@@ -712,6 +721,7 @@ impl Profile {
712721
self.rpath,
713722
(self.incremental, self.panic, self.strip),
714723
&self.rustflags,
724+
&self.trim_paths,
715725
)
716726
}
717727
}

src/cargo/util/toml/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,36 @@ pub struct TomlProfile {
519519
// requires all non-tables to be listed first.
520520
pub package: Option<BTreeMap<ProfilePackageSpec, TomlProfile>>,
521521
pub build_override: Option<Box<TomlProfile>>,
522+
/// Unstable feature `-Ztrim-paths`.
523+
pub trim_paths: Option<Vec<ProfileTrimPaths>>,
524+
}
525+
526+
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
527+
#[serde(rename_all = "kebab-case")]
528+
pub enum ProfileTrimPaths {
529+
None,
530+
Macro,
531+
Diagnostics,
532+
Object,
533+
All,
534+
}
535+
536+
impl ProfileTrimPaths {
537+
pub fn as_str(&self) -> &str {
538+
match self {
539+
ProfileTrimPaths::None => "none",
540+
ProfileTrimPaths::Macro => "macro",
541+
ProfileTrimPaths::Diagnostics => "diagnostics",
542+
ProfileTrimPaths::Object => "object",
543+
ProfileTrimPaths::All => "all",
544+
}
545+
}
546+
}
547+
548+
impl fmt::Display for ProfileTrimPaths {
549+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
550+
write!(f, "{}", self.as_str())
551+
}
522552
}
523553

524554
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
@@ -759,6 +789,15 @@ impl TomlProfile {
759789
_ => {}
760790
}
761791
}
792+
if self.trim_paths.is_some() {
793+
match (
794+
features.require(Feature::trim_paths()),
795+
cli_unstable.trim_paths,
796+
) {
797+
(Err(e), false) => return Err(e),
798+
_ => {}
799+
}
800+
}
762801
Ok(())
763802
}
764803

tests/testsuite/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ fn all_profile_options() {
15181518
package: None,
15191519
build_override: None,
15201520
rustflags: None,
1521+
trim_paths: None,
15211522
};
15221523
let mut overrides = BTreeMap::new();
15231524
let key = cargo_toml::ProfilePackageSpec::Spec(PackageIdSpec::parse("foo").unwrap());

0 commit comments

Comments
 (0)