Skip to content

Commit 5df9ab8

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 f6812bd commit 5df9ab8

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

crates/kernel_cmdline/src/utf8.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ impl<'a> AsRef<str> for Cmdline<'a> {
232232

233233
impl<'a> std::fmt::Display for Cmdline<'a> {
234234
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
235-
let as_str: &str = self.as_ref();
236-
write!(f, "{as_str}")
235+
f.write_str(self)
237236
}
238237
}
239238

@@ -267,7 +266,7 @@ impl<'a, 'other> Extend<Parameter<'other>> for Cmdline<'a> {
267266
#[derive(Clone, Debug, Eq)]
268267
pub struct ParameterKey<'a>(bytes::ParameterKey<'a>);
269268

270-
impl<'a> std::ops::Deref for ParameterKey<'a> {
269+
impl<'a> Deref for ParameterKey<'a> {
271270
type Target = str;
272271

273272
fn deref(&self) -> &Self::Target {
@@ -305,8 +304,7 @@ impl<'a, T: AsRef<str> + ?Sized> From<&'a T> for ParameterKey<'a> {
305304

306305
impl<'a> std::fmt::Display for ParameterKey<'a> {
307306
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
308-
let as_str: &str = self;
309-
write!(f, "{as_str}")
307+
f.write_str(self)
310308
}
311309
}
312310

@@ -378,20 +376,11 @@ impl<'a> TryFrom<bytes::Parameter<'a>> for Parameter<'a> {
378376

379377
impl<'a> std::fmt::Display for Parameter<'a> {
380378
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
381-
match self.value() {
382-
Some(v) => {
383-
if v.contains(|ch: char| ch.is_ascii_whitespace()) {
384-
write!(f, "{}=\"{}\"", self.key(), v)
385-
} else {
386-
write!(f, "{}={}", self.key(), v)
387-
}
388-
}
389-
None => write!(f, "{}", self.key()),
390-
}
379+
f.write_str(self)
391380
}
392381
}
393382

394-
impl<'a> std::ops::Deref for Parameter<'a> {
383+
impl<'a> Deref for Parameter<'a> {
395384
type Target = str;
396385

397386
fn deref(&self) -> &Self::Target {
@@ -459,6 +448,18 @@ mod tests {
459448
assert_eq!(outside_quoted, value_quoted);
460449
}
461450

451+
#[test]
452+
fn test_parameter_display() {
453+
// Basically this should always return the original data
454+
// without modification.
455+
456+
// unquoted stays unquoted
457+
assert_eq!(param("foo").to_string(), "foo");
458+
459+
// quoted stays quoted
460+
assert_eq!(param("\"foo\"").to_string(), "\"foo\"");
461+
}
462+
462463
#[test]
463464
fn test_parameter_extra_whitespace() {
464465
let p = param(" foo=bar ");

0 commit comments

Comments
 (0)