Skip to content

Commit be41b62

Browse files
committed
add as_string() to the Cmdline crate
In some cases, having a String representation of the Linux command line can be useful. This is the case of vmm-reference which otherwise would require a conversion dance between string types. Signed-off-by: Alvise Rigo <[email protected]>
1 parent eedcf57 commit be41b62

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/cmdline/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,36 @@ impl Cmdline {
357357
}
358358
}
359359

360+
/// Returns a String representation of the command line
361+
///
362+
/// # Examples
363+
///
364+
/// ```rust
365+
/// # use linux_loader::cmdline::*;
366+
/// let mut cl = Cmdline::new(20).unwrap();
367+
/// cl.insert_str("foo").unwrap();
368+
/// cl.insert_init_args("bar").unwrap();
369+
/// assert_eq!(
370+
/// cl.as_string().unwrap(),
371+
/// "foo -- bar"
372+
/// );
373+
/// ```
374+
pub fn as_string(&self) -> Result<String> {
375+
if self.boot_args.is_empty() && self.init_args.is_empty() {
376+
Ok("".to_string())
377+
} else if self.boot_args.is_empty() {
378+
Err(Error::NoBootArgsInserted)
379+
} else if self.init_args.is_empty() {
380+
Ok(self.boot_args.to_string())
381+
} else {
382+
Ok(format!(
383+
"{}{}{}",
384+
self.boot_args, INIT_ARGS_SEPARATOR, self.init_args
385+
)
386+
.to_owned())
387+
}
388+
}
389+
360390
/// Adds a virtio MMIO device to the kernel command line.
361391
///
362392
/// Multiple devices can be specified, with multiple `virtio_mmio.device=` options. This

0 commit comments

Comments
 (0)