@@ -12,7 +12,7 @@ use std::fmt;
12
12
use std:: result;
13
13
14
14
/// The error type for command line building operations.
15
- #[ derive( PartialEq , Debug ) ]
15
+ #[ derive( Debug , PartialEq ) ]
16
16
pub enum Error {
17
17
/// Operation would have resulted in a non-printable ASCII character.
18
18
InvalidAscii ,
@@ -30,16 +30,17 @@ impl fmt::Display for Error {
30
30
f,
31
31
"{}" ,
32
32
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. " ,
37
37
}
38
38
)
39
39
}
40
40
}
41
41
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
43
44
pub type Result < T > = result:: Result < T , Error > ;
44
45
45
46
fn valid_char ( c : char ) -> bool {
@@ -69,17 +70,37 @@ fn valid_element(s: &str) -> Result<()> {
69
70
}
70
71
}
71
72
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
+ /// ```
75
85
pub struct Cmdline {
76
86
line : String ,
77
87
capacity : usize ,
78
88
}
79
89
80
90
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
83
104
pub fn new ( capacity : usize ) -> Cmdline {
84
105
assert_ne ! ( capacity, 0 ) ;
85
106
Cmdline {
@@ -109,7 +130,23 @@ impl Cmdline {
109
130
assert ! ( self . line. len( ) < self . capacity) ;
110
131
}
111
132
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
+ /// ```
113
150
pub fn insert < T : AsRef < str > > ( & mut self , key : T , val : T ) -> Result < ( ) > {
114
151
let k = key. as_ref ( ) ;
115
152
let v = val. as_ref ( ) ;
@@ -127,7 +164,22 @@ impl Cmdline {
127
164
Ok ( ( ) )
128
165
}
129
166
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
+ /// ```
131
183
pub fn insert_str < T : AsRef < str > > ( & mut self , slug : T ) -> Result < ( ) > {
132
184
let s = slug. as_ref ( ) ;
133
185
valid_str ( s) ?;
@@ -141,7 +193,16 @@ impl Cmdline {
141
193
Ok ( ( ) )
142
194
}
143
195
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
+ /// ```
145
206
pub fn as_str ( & self ) -> & str {
146
207
self . line . as_str ( )
147
208
}
@@ -159,7 +220,7 @@ mod tests {
159
220
use std:: ffi:: CString ;
160
221
161
222
#[ test]
162
- fn insert_hello_world ( ) {
223
+ fn test_insert_hello_world ( ) {
163
224
let mut cl = Cmdline :: new ( 100 ) ;
164
225
assert_eq ! ( cl. as_str( ) , "" ) ;
165
226
assert ! ( cl. insert( "hello" , "world" ) . is_ok( ) ) ;
@@ -170,15 +231,15 @@ mod tests {
170
231
}
171
232
172
233
#[ test]
173
- fn insert_multi ( ) {
234
+ fn test_insert_multi ( ) {
174
235
let mut cl = Cmdline :: new ( 100 ) ;
175
236
assert ! ( cl. insert( "hello" , "world" ) . is_ok( ) ) ;
176
237
assert ! ( cl. insert( "foo" , "bar" ) . is_ok( ) ) ;
177
238
assert_eq ! ( cl. as_str( ) , "hello=world foo=bar" ) ;
178
239
}
179
240
180
241
#[ test]
181
- fn insert_space ( ) {
242
+ fn test_insert_space ( ) {
182
243
let mut cl = Cmdline :: new ( 100 ) ;
183
244
assert_eq ! ( cl. insert( "a " , "b" ) , Err ( Error :: HasSpace ) ) ;
184
245
assert_eq ! ( cl. insert( "a" , "b " ) , Err ( Error :: HasSpace ) ) ;
@@ -188,7 +249,7 @@ mod tests {
188
249
}
189
250
190
251
#[ test]
191
- fn insert_equals ( ) {
252
+ fn test_insert_equals ( ) {
192
253
let mut cl = Cmdline :: new ( 100 ) ;
193
254
assert_eq ! ( cl. insert( "a=" , "b" ) , Err ( Error :: HasEquals ) ) ;
194
255
assert_eq ! ( cl. insert( "a" , "b=" ) , Err ( Error :: HasEquals ) ) ;
@@ -199,15 +260,15 @@ mod tests {
199
260
}
200
261
201
262
#[ test]
202
- fn insert_emoji ( ) {
263
+ fn test_insert_emoji ( ) {
203
264
let mut cl = Cmdline :: new ( 100 ) ;
204
265
assert_eq ! ( cl. insert( "heart" , "💖" ) , Err ( Error :: InvalidAscii ) ) ;
205
266
assert_eq ! ( cl. insert( "💖" , "love" ) , Err ( Error :: InvalidAscii ) ) ;
206
267
assert_eq ! ( cl. as_str( ) , "" ) ;
207
268
}
208
269
209
270
#[ test]
210
- fn insert_string ( ) {
271
+ fn test_insert_string ( ) {
211
272
let mut cl = Cmdline :: new ( 13 ) ;
212
273
assert_eq ! ( cl. as_str( ) , "" ) ;
213
274
assert ! ( cl. insert_str( "noapic" ) . is_ok( ) ) ;
@@ -217,7 +278,7 @@ mod tests {
217
278
}
218
279
219
280
#[ test]
220
- fn insert_too_large ( ) {
281
+ fn test_insert_too_large ( ) {
221
282
let mut cl = Cmdline :: new ( 4 ) ;
222
283
assert_eq ! ( cl. insert( "hello" , "world" ) , Err ( Error :: TooLarge ) ) ;
223
284
assert_eq ! ( cl. insert( "a" , "world" ) , Err ( Error :: TooLarge ) ) ;
0 commit comments