Skip to content

Commit 601ba3c

Browse files
committed
kernel_cmdline: Simplify Display impl for utf8::Parameter
Really we want to just return the original slice that the Parameter was created from. This also aligns with how Cmdline and ParameterKey impl Display. Where this really matters the most is to ensure we retain the quoting that the parameter was created with, so I added a test just to sanity check that. Before this change the test would fail because "foo" would be stripped of its quotes and just rendered as foo unquoted. Signed-off-by: John Eckersberg <[email protected]>
1 parent 7f10088 commit 601ba3c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

crates/kernel_cmdline/src/utf8.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,8 @@ impl<'a> TryFrom<bytes::Parameter<'a>> for Parameter<'a> {
368368

369369
impl<'a> std::fmt::Display for Parameter<'a> {
370370
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
371-
match self.value() {
372-
Some(v) => {
373-
if v.contains(|ch: char| ch.is_ascii_whitespace()) {
374-
write!(f, "{}=\"{}\"", self.key(), v)
375-
} else {
376-
write!(f, "{}={}", self.key(), v)
377-
}
378-
}
379-
None => write!(f, "{}", self.key()),
380-
}
371+
let as_str: &str = self.as_ref();
372+
write!(f, "{as_str}")
381373
}
382374
}
383375

@@ -449,6 +441,18 @@ mod tests {
449441
assert_eq!(outside_quoted, value_quoted);
450442
}
451443

444+
#[test]
445+
fn test_parameter_display() {
446+
// Basically this should always return the original data
447+
// without modification.
448+
449+
// unquoted stays unquoted
450+
assert_eq!(param("foo").to_string(), "foo");
451+
452+
// quoted stays quoted
453+
assert_eq!(param("\"foo\"").to_string(), "\"foo\"");
454+
}
455+
452456
#[test]
453457
fn test_parameter_extra_whitespace() {
454458
let p = param(" foo=bar ");

0 commit comments

Comments
 (0)