Skip to content

Commit 74a4f44

Browse files
committed
use when syntax instead of ;if
1 parent 14c6078 commit 74a4f44

File tree

11 files changed

+428
-101
lines changed

11 files changed

+428
-101
lines changed

crates/rattler_conda_types/src/match_spec/mod.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ pub struct MatchSpec {
172172
pub track_features: Option<Vec<String>>,
173173
}
174174

175+
/// Escapes a string value for use in bracket syntax.
176+
/// Escapes double quotes and backslashes.
177+
fn escape_bracket_value(s: &str) -> String {
178+
let mut result = String::with_capacity(s.len());
179+
for c in s.chars() {
180+
match c {
181+
'"' => result.push_str("\\\""),
182+
'\\' => result.push_str("\\\\"),
183+
_ => result.push(c),
184+
}
185+
}
186+
result
187+
}
188+
175189
impl Display for MatchSpec {
176190
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
177191
if let Some(channel) = &self.channel {
@@ -239,12 +253,13 @@ impl Display for MatchSpec {
239253
));
240254
}
241255

242-
if !keys.is_empty() {
243-
write!(f, "[{}]", keys.join(", "))?;
256+
if let Some(condition) = &self.condition {
257+
let condition_str = condition.to_string();
258+
keys.push(format!("when=\"{}\"", escape_bracket_value(&condition_str)));
244259
}
245260

246-
if let Some(condition) = &self.condition {
247-
write!(f, "; if {condition}")?;
261+
if !keys.is_empty() {
262+
write!(f, "[{}]", keys.join(", "))?;
248263
}
249264

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

361-
if !keys.is_empty() {
362-
write!(f, "[{}]", keys.join(", "))?;
376+
if let Some(condition) = &self.condition {
377+
let condition_str = condition.to_string();
378+
keys.push(format!("when=\"{}\"", escape_bracket_value(&condition_str)));
363379
}
364380

365-
if let Some(condition) = &self.condition {
366-
write!(f, "; if {condition}")?;
381+
if !keys.is_empty() {
382+
write!(f, "[{}]", keys.join(", "))?;
367383
}
368384

369385
Ok(())

0 commit comments

Comments
 (0)