@@ -225,6 +225,27 @@ where
225225 } )
226226}
227227
228+ /// Obtain a status line from a line containing the target type as its first
229+ /// entry. Return an error if the line does not have the expected target type.
230+ /// Precondition: The line's first entry is the target type, followed by a
231+ /// space, followed by the actual status values.
232+ pub fn get_status_line < ' a , ' b > (
233+ status_line : & ' a str ,
234+ expected_target_type : & ' b str ,
235+ ) -> DmResult < & ' a str > {
236+ let target_status_pair: Vec < & str > = status_line. splitn ( 2 , ' ' ) . collect ( ) ;
237+ let target_type = target_status_pair[ 0 ] ;
238+ if target_type != expected_target_type {
239+ let err_msg = format ! (
240+ "Expected a \" {}\" target entry but found target type \" {}\" in \" {}\" " ,
241+ expected_target_type, target_type, status_line
242+ ) ;
243+ return Err ( DmError :: Dm ( ErrorEnum :: Invalid , err_msg) ) ;
244+ }
245+
246+ Ok ( target_status_pair[ 1 ] )
247+ }
248+
228249/// Get fields for a single status line.
229250/// Return an error if an insufficient number of fields are obtained.
230251pub fn get_status_line_fields < ' a > (
@@ -255,15 +276,14 @@ pub fn get_status(status_lines: &[(u64, u64, String, String)]) -> DmResult<Strin
255276 format ! (
256277 "Incorrect number of lines for status; expected 1, found {} in status result \" {}\" " ,
257278 length,
258- status_lines. iter( ) . map( |( s, l, t, v) | format!( "{} {} {} {}" , s, l, t. to_string ( ) , v) ) . collect:: <Vec <String >>( ) . join( ", " )
279+ status_lines. iter( ) . map( |( s, l, t, v) | format!( "{} {} {} {}" , s, l, t, v) ) . collect:: <Vec <String >>( ) . join( ", " )
259280 ) ,
260281 ) ) ;
261282 }
262- Ok ( status_lines
283+ let line = status_lines
263284 . first ( )
264- . expect ( "if length != 1, already returned" )
265- . 3
266- . to_owned ( ) )
285+ . expect ( "if length != 1, already returned" ) ;
286+ Ok ( format ! ( "{} {}" , line. 2 , line. 3 ) )
267287}
268288
269289/// Construct an error when parsing yields an unexpected value.
0 commit comments