Skip to content

Commit a271fbe

Browse files
author
Alexandra Iordache
committed
cosmetic changes & doctests
Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
1 parent 0c754f3 commit a271fbe

File tree

4 files changed

+289
-128
lines changed

4 files changed

+289
-128
lines changed

src/cmdline/mod.rs

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fmt;
1212
use std::result;
1313

1414
/// The error type for command line building operations.
15-
#[derive(PartialEq, Debug)]
15+
#[derive(Debug, PartialEq)]
1616
pub enum Error {
1717
/// Operation would have resulted in a non-printable ASCII character.
1818
InvalidAscii,
@@ -30,16 +30,17 @@ impl fmt::Display for Error {
3030
f,
3131
"{}",
3232
match *self {
33-
Error::InvalidAscii => "string contains non-printable ASCII character",
34-
Error::HasSpace => "string contains a space",
35-
Error::HasEquals => "string contains an equals sign",
36-
Error::TooLarge => "inserting string would make command line too long",
33+
Error::InvalidAscii => "String contains a non-printable ASCII character.",
34+
Error::HasSpace => "String contains a space.",
35+
Error::HasEquals => "String contains an equals sign.",
36+
Error::TooLarge => "Inserting string would make command line too long.",
3737
}
3838
)
3939
}
4040
}
4141

42-
/// Specialized Result type for command line operations.
42+
/// Specialized [`Result`] type for command line operations.
43+
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
4344
pub type Result<T> = result::Result<T, Error>;
4445

4546
fn valid_char(c: char) -> bool {
@@ -69,17 +70,37 @@ fn valid_element(s: &str) -> Result<()> {
6970
}
7071
}
7172

72-
/// A builder for a kernel command line string that validates the string as its being built. A
73-
/// `CString` can be constructed from this directly using `CString::new`.
74-
#[derive(Clone)]
73+
/// A builder for a kernel command line string that validates the string as it's being built.
74+
/// A `CString` can be constructed from this directly using `CString::new`.
75+
///
76+
/// # Examples
77+
///
78+
/// ```rust
79+
/// # use linux_loader::cmdline::*;
80+
/// # use std::ffi::CString;
81+
/// let cl = Cmdline::new(100);
82+
/// let cl_cstring = CString::new(cl).unwrap();
83+
/// assert_eq!(cl_cstring.to_str().unwrap(), "");
84+
/// ```
7585
pub struct Cmdline {
7686
line: String,
7787
capacity: usize,
7888
}
7989

8090
impl Cmdline {
81-
/// Constructs an empty Cmdline with the given capacity, which includes the nul terminator.
82-
/// Capacity must be greater than 0.
91+
/// Constructs an empty [`Cmdline`] with the given capacity, including the nul terminator.
92+
///
93+
/// # Arguments
94+
///
95+
/// * `capacity` - Command line capacity. Must be greater than 0.
96+
///
97+
/// # Examples
98+
///
99+
/// ```rust
100+
/// # use linux_loader::cmdline::*;
101+
/// let cl = Cmdline::new(100);
102+
/// ```
103+
/// [`Cmdline`]: struct.Cmdline.html
83104
pub fn new(capacity: usize) -> Cmdline {
84105
assert_ne!(capacity, 0);
85106
Cmdline {
@@ -109,7 +130,23 @@ impl Cmdline {
109130
assert!(self.line.len() < self.capacity);
110131
}
111132

112-
/// Validates and inserts a key value pair into this command line
133+
/// Validates and inserts a key-value pair into this command line.
134+
///
135+
/// # Arguments
136+
///
137+
/// * `key` - Key to be inserted in the command line string.
138+
/// * `val` - Value corresponding to `key`.
139+
///
140+
/// # Examples
141+
///
142+
/// ```rust
143+
/// # use linux_loader::cmdline::*;
144+
/// # use std::ffi::CString;
145+
/// let mut cl = Cmdline::new(100);
146+
/// cl.insert("foo", "bar");
147+
/// let cl_cstring = CString::new(cl).unwrap();
148+
/// assert_eq!(cl_cstring.to_str().unwrap(), "foo=bar");
149+
/// ```
113150
pub fn insert<T: AsRef<str>>(&mut self, key: T, val: T) -> Result<()> {
114151
let k = key.as_ref();
115152
let v = val.as_ref();
@@ -127,7 +164,22 @@ impl Cmdline {
127164
Ok(())
128165
}
129166

130-
/// Validates and inserts a string to the end of the current command line
167+
/// Validates and inserts a string to the end of the current command line.
168+
///
169+
/// # Arguments
170+
///
171+
/// * `slug` - String to be appended to the command line.
172+
///
173+
/// # Examples
174+
///
175+
/// ```rust
176+
/// # use linux_loader::cmdline::*;
177+
/// # use std::ffi::CString;
178+
/// let mut cl = Cmdline::new(100);
179+
/// cl.insert_str("foobar");
180+
/// let cl_cstring = CString::new(cl).unwrap();
181+
/// assert_eq!(cl_cstring.to_str().unwrap(), "foobar");
182+
/// ```
131183
pub fn insert_str<T: AsRef<str>>(&mut self, slug: T) -> Result<()> {
132184
let s = slug.as_ref();
133185
valid_str(s)?;
@@ -141,7 +193,16 @@ impl Cmdline {
141193
Ok(())
142194
}
143195

144-
/// Returns the cmdline in progress without nul termination
196+
/// Returns the string representation of the command line without the nul terminator.
197+
///
198+
/// # Examples
199+
///
200+
/// ```rust
201+
/// # use linux_loader::cmdline::*;
202+
/// let mut cl = Cmdline::new(10);
203+
/// cl.insert_str("foobar");
204+
/// assert_eq!(cl.as_str(), "foobar");
205+
/// ```
145206
pub fn as_str(&self) -> &str {
146207
self.line.as_str()
147208
}
@@ -159,7 +220,7 @@ mod tests {
159220
use std::ffi::CString;
160221

161222
#[test]
162-
fn insert_hello_world() {
223+
fn test_insert_hello_world() {
163224
let mut cl = Cmdline::new(100);
164225
assert_eq!(cl.as_str(), "");
165226
assert!(cl.insert("hello", "world").is_ok());
@@ -170,15 +231,15 @@ mod tests {
170231
}
171232

172233
#[test]
173-
fn insert_multi() {
234+
fn test_insert_multi() {
174235
let mut cl = Cmdline::new(100);
175236
assert!(cl.insert("hello", "world").is_ok());
176237
assert!(cl.insert("foo", "bar").is_ok());
177238
assert_eq!(cl.as_str(), "hello=world foo=bar");
178239
}
179240

180241
#[test]
181-
fn insert_space() {
242+
fn test_insert_space() {
182243
let mut cl = Cmdline::new(100);
183244
assert_eq!(cl.insert("a ", "b"), Err(Error::HasSpace));
184245
assert_eq!(cl.insert("a", "b "), Err(Error::HasSpace));
@@ -188,7 +249,7 @@ mod tests {
188249
}
189250

190251
#[test]
191-
fn insert_equals() {
252+
fn test_insert_equals() {
192253
let mut cl = Cmdline::new(100);
193254
assert_eq!(cl.insert("a=", "b"), Err(Error::HasEquals));
194255
assert_eq!(cl.insert("a", "b="), Err(Error::HasEquals));
@@ -199,15 +260,15 @@ mod tests {
199260
}
200261

201262
#[test]
202-
fn insert_emoji() {
263+
fn test_insert_emoji() {
203264
let mut cl = Cmdline::new(100);
204265
assert_eq!(cl.insert("heart", "💖"), Err(Error::InvalidAscii));
205266
assert_eq!(cl.insert("💖", "love"), Err(Error::InvalidAscii));
206267
assert_eq!(cl.as_str(), "");
207268
}
208269

209270
#[test]
210-
fn insert_string() {
271+
fn test_insert_string() {
211272
let mut cl = Cmdline::new(13);
212273
assert_eq!(cl.as_str(), "");
213274
assert!(cl.insert_str("noapic").is_ok());
@@ -217,7 +278,7 @@ mod tests {
217278
}
218279

219280
#[test]
220-
fn insert_too_large() {
281+
fn test_insert_too_large() {
221282
let mut cl = Cmdline::new(4);
222283
assert_eq!(cl.insert("hello", "world"), Err(Error::TooLarge));
223284
assert_eq!(cl.insert("a", "world"), Err(Error::TooLarge));

src/lib.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@
1111

1212
//! A Linux kernel image loading crate.
1313
//!
14-
//! This crate offers support for loading raw ELF (vmlinux) and compressed
15-
//! big zImage (bzImage) kernel images.
16-
//! Support for any other kernel image format can be added by implementing
17-
//! the KernelLoader.
14+
//! This crate offers support for loading raw ELF (vmlinux) and compressed big zImage (bzImage)
15+
//! kernel images.
16+
//! Support for any other kernel image format can be added by implementing the [`KernelLoader`].
1817
//!
1918
//! # Platform support
2019
//!
21-
//! - x86_64
20+
//! - `x86_64`
2221
//!
23-
//! This crates only supports x86_64 platforms because it implements support
24-
//! for kernel image formats (vmlinux and bzImage) that are x86 specific.
22+
//! Extending this crate to support other kernel image formats (e.g. ARM's Image) will make it
23+
//! consumable by other platforms.
2524
//!
26-
//! Extending it to support other kernel image formats (e.g. ARM's Image)
27-
//! will make it consumable by other platforms.
25+
//! [`KernelLoader`]: trait.KernelLoader.html
2826
2927
pub mod cmdline;
3028
pub mod loader;

0 commit comments

Comments
 (0)