Skip to content

Commit 14494ba

Browse files
committed
feat(trim-paths): parsing in mainfest and config
1 parent 5849d7e commit 14494ba

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-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::TomlTrimPaths;
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 = Some(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+
// remove when `-Ztrim-paths` is stablized
606+
#[serde(skip_serializing_if = "Option::is_none")]
607+
pub trim_paths: Option<TomlTrimPaths>,
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: None,
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: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,8 @@ pub struct TomlProfile {
26712671
// requires all non-tables to be listed first.
26722672
pub package: Option<BTreeMap<ProfilePackageSpec, TomlProfile>>,
26732673
pub build_override: Option<Box<TomlProfile>>,
2674+
/// Unstable feature `-Ztrim-paths`.
2675+
pub trim_paths: Option<TomlTrimPaths>,
26742676
}
26752677

26762678
impl TomlProfile {
@@ -2871,6 +2873,15 @@ impl TomlProfile {
28712873
_ => {}
28722874
}
28732875
}
2876+
if self.trim_paths.is_some() {
2877+
match (
2878+
features.require(Feature::trim_paths()),
2879+
cli_unstable.trim_paths,
2880+
) {
2881+
(Err(e), false) => return Err(e),
2882+
_ => {}
2883+
}
2884+
}
28742885
Ok(())
28752886
}
28762887

@@ -3146,6 +3157,120 @@ impl<'de> de::Deserialize<'de> for TomlDebugInfo {
31463157
}
31473158
}
31483159

3160+
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize)]
3161+
#[serde(untagged)]
3162+
pub enum TomlTrimPaths {
3163+
One(TomlTrimPathsValue),
3164+
Many(Vec<TomlTrimPathsValue>),
3165+
}
3166+
3167+
impl<'de> de::Deserialize<'de> for TomlTrimPaths {
3168+
fn deserialize<D>(d: D) -> Result<TomlTrimPaths, D::Error>
3169+
where
3170+
D: de::Deserializer<'de>,
3171+
{
3172+
let expecting = r#"a boolean, "none", "macro", "diagnostics", "object", "all", or a list of these options"#;
3173+
UntaggedEnumVisitor::new()
3174+
.expecting(expecting)
3175+
.bool(|value| {
3176+
Ok(if value {
3177+
TomlTrimPaths::all()
3178+
} else {
3179+
TomlTrimPaths::none()
3180+
})
3181+
})
3182+
.string(|value| {
3183+
TomlTrimPathsValue::deserialize(value.into_deserializer()).map(TomlTrimPaths::One)
3184+
})
3185+
.seq(|seq| {
3186+
let seq: Vec<String> = seq.deserialize()?;
3187+
let seq = seq
3188+
.into_iter()
3189+
.map(|s| TomlTrimPathsValue::deserialize(s.into_deserializer()))
3190+
.collect::<Result<_, _>>()?;
3191+
Ok(TomlTrimPaths::Many(seq))
3192+
})
3193+
.deserialize(d)
3194+
}
3195+
}
3196+
3197+
impl TomlTrimPaths {
3198+
pub fn none() -> TomlTrimPaths {
3199+
TomlTrimPaths::One(TomlTrimPathsValue::None)
3200+
}
3201+
3202+
pub fn all() -> TomlTrimPaths {
3203+
TomlTrimPaths::One(TomlTrimPathsValue::All)
3204+
}
3205+
3206+
pub fn diagnostics() -> TomlTrimPaths {
3207+
TomlTrimPaths::One(TomlTrimPathsValue::Diagnostics)
3208+
}
3209+
3210+
pub fn r#macro() -> TomlTrimPaths {
3211+
TomlTrimPaths::One(TomlTrimPathsValue::Macro)
3212+
}
3213+
3214+
pub fn object() -> TomlTrimPaths {
3215+
TomlTrimPaths::One(TomlTrimPathsValue::Object)
3216+
}
3217+
3218+
pub fn is_none(&self) -> bool {
3219+
match self {
3220+
TomlTrimPaths::One(TomlTrimPathsValue::None) => true,
3221+
TomlTrimPaths::Many(list) if list.is_empty() => true,
3222+
TomlTrimPaths::Many(list) => list.iter().any(|v| matches!(v, TomlTrimPathsValue::None)),
3223+
_ => false,
3224+
}
3225+
}
3226+
}
3227+
3228+
impl fmt::Display for TomlTrimPaths {
3229+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3230+
match self {
3231+
TomlTrimPaths::One(value) => write!(f, "{value}"),
3232+
TomlTrimPaths::Many(values) => {
3233+
let mut iter = values.iter();
3234+
if let Some(value) = iter.next() {
3235+
write!(f, "{value}")?;
3236+
}
3237+
for value in iter {
3238+
write!(f, ",{value}")?;
3239+
}
3240+
Ok(())
3241+
}
3242+
}
3243+
}
3244+
}
3245+
3246+
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
3247+
#[serde(rename_all = "kebab-case")]
3248+
pub enum TomlTrimPathsValue {
3249+
None,
3250+
Diagnostics,
3251+
Macro,
3252+
Object,
3253+
All,
3254+
}
3255+
3256+
impl TomlTrimPathsValue {
3257+
pub fn as_str(&self) -> &'static str {
3258+
match self {
3259+
TomlTrimPathsValue::None => "none",
3260+
TomlTrimPathsValue::Diagnostics => "diagnostics",
3261+
TomlTrimPathsValue::Macro => "macro",
3262+
TomlTrimPathsValue::Object => "object",
3263+
TomlTrimPathsValue::All => "all",
3264+
}
3265+
}
3266+
}
3267+
3268+
impl fmt::Display for TomlTrimPathsValue {
3269+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3270+
write!(f, "{}", self.as_str())
3271+
}
3272+
}
3273+
31493274
type TomlLibTarget = TomlTarget;
31503275
type TomlBinTarget = TomlTarget;
31513276
type TomlExampleTarget = TomlTarget;

tests/testsuite/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,7 @@ fn all_profile_options() {
15411541
package: None,
15421542
build_override: None,
15431543
rustflags: None,
1544+
trim_paths: None,
15441545
};
15451546
let mut overrides = BTreeMap::new();
15461547
let key = cargo_toml::ProfilePackageSpec::Spec(PackageIdSpec::parse("foo").unwrap());

0 commit comments

Comments
 (0)