Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions crates/rattler_conda_types/src/match_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ pub struct MatchSpec {
pub track_features: Option<Vec<String>>,
}

/// Escapes a string value for use in bracket syntax.
/// Escapes double quotes and backslashes.
fn escape_bracket_value(s: &str) -> String {
let mut result = String::with_capacity(s.len());
for c in s.chars() {
match c {
'"' => result.push_str("\\\""),
'\\' => result.push_str("\\\\"),
_ => result.push(c),
}
}
result
}

impl Display for MatchSpec {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(channel) = &self.channel {
Expand Down Expand Up @@ -239,12 +253,13 @@ impl Display for MatchSpec {
));
}

if !keys.is_empty() {
write!(f, "[{}]", keys.join(", "))?;
if let Some(condition) = &self.condition {
let condition_str = condition.to_string();
keys.push(format!("when=\"{}\"", escape_bracket_value(&condition_str)));
}

if let Some(condition) = &self.condition {
write!(f, "; if {condition}")?;
if !keys.is_empty() {
write!(f, "[{}]", keys.join(", "))?;
}

Ok(())
Expand Down Expand Up @@ -358,12 +373,13 @@ impl Display for NamelessMatchSpec {
keys.push(format!("sha256={sha256:x}"));
}

if !keys.is_empty() {
write!(f, "[{}]", keys.join(", "))?;
if let Some(condition) = &self.condition {
let condition_str = condition.to_string();
keys.push(format!("when=\"{}\"", escape_bracket_value(&condition_str)));
}

if let Some(condition) = &self.condition {
write!(f, "; if {condition}")?;
if !keys.is_empty() {
write!(f, "[{}]", keys.join(", "))?;
}

Ok(())
Expand Down
Loading
Loading