Skip to content

Commit 2604f7c

Browse files
committed
feat(trim-paths): parsing in mainfest and config
1 parent d00b4a4 commit 2604f7c

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
};
@@ -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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,8 @@ pub struct TomlProfile {
26822682
// requires all non-tables to be listed first.
26832683
pub package: Option<BTreeMap<ProfilePackageSpec, TomlProfile>>,
26842684
pub build_override: Option<Box<TomlProfile>>,
2685+
/// Unstable feature `-Ztrim-paths`.
2686+
pub trim_paths: Option<TomlTrimPaths>,
26852687
}
26862688

26872689
impl TomlProfile {
@@ -2882,6 +2884,15 @@ impl TomlProfile {
28822884
_ => {}
28832885
}
28842886
}
2887+
if self.trim_paths.is_some() {
2888+
match (
2889+
features.require(Feature::trim_paths()),
2890+
cli_unstable.trim_paths,
2891+
) {
2892+
(Err(e), false) => return Err(e),
2893+
_ => {}
2894+
}
2895+
}
28852896
Ok(())
28862897
}
28872898

@@ -3157,6 +3168,122 @@ impl<'de> de::Deserialize<'de> for TomlDebugInfo {
31573168
}
31583169
}
31593170

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