Skip to content

Commit 4d29af1

Browse files
committed
feat(trim-paths): parsing in mainfest and config
1 parent 28b169b commit 4d29af1

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-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
};
@@ -556,6 +557,9 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
556557
if let Some(flags) = &toml.rustflags {
557558
profile.rustflags = flags.iter().map(InternedString::from).collect();
558559
}
560+
if let Some(trim_paths) = &toml.trim_paths {
561+
profile.trim_paths = Some(trim_paths.clone());
562+
}
559563
profile.strip = match toml.strip {
560564
Some(StringOrBool::Bool(true)) => Strip::Named(InternedString::new("symbols")),
561565
None | Some(StringOrBool::Bool(false)) => Strip::None,
@@ -599,6 +603,9 @@ pub struct Profile {
599603
#[serde(skip_serializing_if = "Vec::is_empty")] // remove when `rustflags` is stablized
600604
// Note that `rustflags` is used for the cargo-feature `profile_rustflags`
601605
pub rustflags: Vec<InternedString>,
606+
// remove when `-Ztrim-paths` is stablized
607+
#[serde(skip_serializing_if = "Option::is_none")]
608+
pub trim_paths: Option<TomlTrimPaths>,
602609
}
603610

604611
impl Default for Profile {
@@ -619,6 +626,7 @@ impl Default for Profile {
619626
panic: PanicStrategy::Unwind,
620627
strip: Strip::None,
621628
rustflags: vec![],
629+
trim_paths: None,
622630
}
623631
}
624632
}
@@ -647,6 +655,7 @@ compact_debug! {
647655
panic
648656
strip
649657
rustflags
658+
trim_paths
650659
)]
651660
}
652661
}
@@ -713,6 +722,7 @@ impl Profile {
713722
self.rpath,
714723
(self.incremental, self.panic, self.strip),
715724
&self.rustflags,
725+
&self.trim_paths,
716726
)
717727
}
718728
}

src/cargo/util/toml/mod.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,6 +2692,8 @@ pub struct TomlProfile {
26922692
// requires all non-tables to be listed first.
26932693
pub package: Option<BTreeMap<ProfilePackageSpec, TomlProfile>>,
26942694
pub build_override: Option<Box<TomlProfile>>,
2695+
/// Unstable feature `-Ztrim-paths`.
2696+
pub trim_paths: Option<TomlTrimPaths>,
26952697
}
26962698

26972699
impl TomlProfile {
@@ -2892,6 +2894,15 @@ impl TomlProfile {
28922894
_ => {}
28932895
}
28942896
}
2897+
if self.trim_paths.is_some() {
2898+
match (
2899+
features.require(Feature::trim_paths()),
2900+
cli_unstable.trim_paths,
2901+
) {
2902+
(Err(e), false) => return Err(e),
2903+
_ => {}
2904+
}
2905+
}
28952906
Ok(())
28962907
}
28972908

@@ -3167,6 +3178,122 @@ impl<'de> de::Deserialize<'de> for TomlDebugInfo {
31673178
}
31683179
}
31693180

3181+
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize)]
3182+
#[serde(untagged, rename_all = "kebab-case")]
3183+
pub enum TomlTrimPaths {
3184+
Values(Vec<TomlTrimPathsValue>),
3185+
All,
3186+
}
3187+
3188+
impl TomlTrimPaths {
3189+
pub fn none() -> Self {
3190+
TomlTrimPaths::Values(Vec::new())
3191+
}
3192+
3193+
pub fn is_none(&self) -> bool {
3194+
match self {
3195+
TomlTrimPaths::Values(v) => v.is_empty(),
3196+
TomlTrimPaths::All => false,
3197+
}
3198+
}
3199+
}
3200+
3201+
impl<'de> de::Deserialize<'de> for TomlTrimPaths {
3202+
fn deserialize<D>(d: D) -> Result<TomlTrimPaths, D::Error>
3203+
where
3204+
D: de::Deserializer<'de>,
3205+
{
3206+
use serde::de::Error as _;
3207+
let expecting = r#"a boolean, "none", "diagnostics", "macro", "object", "all", or an array with these options"#;
3208+
UntaggedEnumVisitor::new()
3209+
.expecting(expecting)
3210+
.bool(|value| {
3211+
Ok(if value {
3212+
TomlTrimPaths::All
3213+
} else {
3214+
TomlTrimPaths::none()
3215+
})
3216+
})
3217+
.string(|v| match v {
3218+
"none" => Ok(TomlTrimPaths::none()),
3219+
"all" => Ok(TomlTrimPaths::All),
3220+
v => {
3221+
let d = v.into_deserializer();
3222+
let err = |_: D::Error| {
3223+
serde_untagged::de::Error::custom(format!("expected {expecting}"))
3224+
};
3225+
TomlTrimPathsValue::deserialize(d)
3226+
.map_err(err)
3227+
.map(|v| v.into())
3228+
}
3229+
})
3230+
.seq(|seq| {
3231+
let seq: Vec<String> = seq.deserialize()?;
3232+
let seq: Vec<_> = seq
3233+
.into_iter()
3234+
.map(|s| TomlTrimPathsValue::deserialize(s.into_deserializer()))
3235+
.collect::<Result<_, _>>()?;
3236+
Ok(seq.into())
3237+
})
3238+
.deserialize(d)
3239+
}
3240+
}
3241+
3242+
impl fmt::Display for TomlTrimPaths {
3243+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3244+
match self {
3245+
TomlTrimPaths::All => write!(f, "all"),
3246+
TomlTrimPaths::Values(v) if v.is_empty() => write!(f, "none"),
3247+
TomlTrimPaths::Values(v) => {
3248+
let mut iter = v.iter();
3249+
if let Some(value) = iter.next() {
3250+
write!(f, "{value}")?;
3251+
}
3252+
for value in iter {
3253+
write!(f, ",{value}")?;
3254+
}
3255+
Ok(())
3256+
}
3257+
}
3258+
}
3259+
}
3260+
3261+
impl From<TomlTrimPathsValue> for TomlTrimPaths {
3262+
fn from(value: TomlTrimPathsValue) -> Self {
3263+
TomlTrimPaths::Values(vec![value])
3264+
}
3265+
}
3266+
3267+
impl From<Vec<TomlTrimPathsValue>> for TomlTrimPaths {
3268+
fn from(value: Vec<TomlTrimPathsValue>) -> Self {
3269+
TomlTrimPaths::Values(value)
3270+
}
3271+
}
3272+
3273+
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
3274+
#[serde(rename_all = "kebab-case")]
3275+
pub enum TomlTrimPathsValue {
3276+
Diagnostics,
3277+
Macro,
3278+
Object,
3279+
}
3280+
3281+
impl TomlTrimPathsValue {
3282+
pub fn as_str(&self) -> &'static str {
3283+
match self {
3284+
TomlTrimPathsValue::Diagnostics => "diagnostics",
3285+
TomlTrimPathsValue::Macro => "macro",
3286+
TomlTrimPathsValue::Object => "object",
3287+
}
3288+
}
3289+
}
3290+
3291+
impl fmt::Display for TomlTrimPathsValue {
3292+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3293+
write!(f, "{}", self.as_str())
3294+
}
3295+
}
3296+
31703297
type TomlLibTarget = TomlTarget;
31713298
type TomlBinTarget = TomlTarget;
31723299
type TomlExampleTarget = TomlTarget;

tests/testsuite/config.rs

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

0 commit comments

Comments
 (0)