Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 431d52c

Browse files
committedNov 14, 2023
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 <a.rigo@virtualopensystems.com>
1 parent 35cb0d0 commit 431d52c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎src/cmdline/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,33 @@ 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!(cl.as_string().unwrap(), "foo -- bar");
370+
/// ```
371+
pub fn as_string(&self) -> Result<String> {
372+
if self.boot_args.is_empty() && self.init_args.is_empty() {
373+
Ok("".to_string())
374+
} else if self.boot_args.is_empty() {
375+
Err(Error::NoBootArgsInserted)
376+
} else if self.init_args.is_empty() {
377+
Ok(self.boot_args.to_string())
378+
} else {
379+
Ok(format!(
380+
"{}{}{}",
381+
self.boot_args, INIT_ARGS_SEPARATOR, self.init_args
382+
)
383+
.to_owned())
384+
}
385+
}
386+
360387
/// Adds a virtio MMIO device to the kernel command line.
361388
///
362389
/// Multiple devices can be specified, with multiple `virtio_mmio.device=` options. This

0 commit comments

Comments
 (0)