Skip to content

Commit 9d204d9

Browse files
committed
Abstract getting unique status result into a single method
Removes a possibly failing assertion and some unnecessary code duplication. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent b25bcd0 commit 9d204d9

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

src/cachedev.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::dm_options::DmOptions;
1414
use crate::lineardev::{LinearDev, LinearDevTargetParams};
1515
use crate::result::{DmError, DmResult, ErrorEnum};
1616
use crate::shared::{
17-
device_create, device_exists, device_match, get_status_line_fields,
17+
device_create, device_exists, device_match, get_status, get_status_line_fields,
1818
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
1919
TargetTable,
2020
};
@@ -718,14 +718,7 @@ impl CacheDev {
718718
/// Get the current status of the cache device.
719719
pub fn status(&self, dm: &DM) -> DmResult<CacheDevStatus> {
720720
let (_, status) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;
721-
722-
assert_eq!(
723-
status.len(),
724-
1,
725-
"Kernel must return 1 line from cache dev status"
726-
);
727-
728-
status.first().expect("assertion above holds").3.parse()
721+
get_status(&status)?.parse()
729722
}
730723
}
731724

src/shared.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,27 @@ pub fn get_status_line_fields<'a>(
235235
Ok(status_vals)
236236
}
237237

238+
/// Get unique status element from a status result.
239+
/// Return an error if an incorrect number of lines are obtained.
240+
pub fn get_status(status_lines: &[(Sectors, Sectors, TargetTypeBuf, String)]) -> DmResult<String> {
241+
let length = status_lines.len();
242+
if length != 1 {
243+
return Err(DmError::Dm(
244+
ErrorEnum::Invalid,
245+
format!(
246+
"Incorrect number of lines for status; expected 1, found {} in status result \"{}\"",
247+
length,
248+
status_lines.iter().map(|(s, l, t, v)| format!("{} {} {} {}", *s, *l, t.to_string(), v)).collect::<Vec<String>>().join(", ")
249+
),
250+
));
251+
}
252+
Ok(status_lines
253+
.first()
254+
.expect("if length != 1, already returned")
255+
.3
256+
.to_owned())
257+
}
258+
238259
/// Construct an error when parsing yields an unexpected value.
239260
/// Indicate the location of the unexpected value, 1-indexed, its actual
240261
/// value, and the name of the expected thing.

src/thindev.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::dm_flags::{DmCookie, DmFlags};
1313
use crate::dm_options::DmOptions;
1414
use crate::result::{DmError, DmResult, ErrorEnum};
1515
use crate::shared::{
16-
device_create, device_exists, device_match, get_status_line_fields, message, parse_device,
17-
parse_value, DmDevice, TargetLine, TargetParams, TargetTable,
16+
device_create, device_exists, device_match, get_status, get_status_line_fields, message,
17+
parse_device, parse_value, DmDevice, TargetLine, TargetParams, TargetTable,
1818
};
1919
use crate::thindevid::ThinDevId;
2020
use crate::thinpooldev::ThinPoolDev;
@@ -385,14 +385,7 @@ impl ThinDev {
385385
/// Get the current status of the thin device.
386386
pub fn status(&self, dm: &DM) -> DmResult<ThinStatus> {
387387
let (_, table) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;
388-
389-
assert_eq!(
390-
table.len(),
391-
1,
392-
"Kernel must return 1 line table for thin status"
393-
);
394-
395-
table.first().expect("assertion above holds").3.parse()
388+
get_status(&table)?.parse()
396389
}
397390

398391
/// Set the table for the thin device's target

src/thinpooldev.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::dm_options::DmOptions;
1414
use crate::lineardev::{LinearDev, LinearDevTargetParams};
1515
use crate::result::{DmError, DmResult, ErrorEnum};
1616
use crate::shared::{
17-
device_create, device_exists, device_match, get_status_line_fields,
17+
device_create, device_exists, device_match, get_status, get_status_line_fields,
1818
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
1919
TargetTable,
2020
};
@@ -552,14 +552,7 @@ impl ThinPoolDev {
552552
/// Returns an error if there was an error getting the status value.
553553
pub fn status(&self, dm: &DM) -> DmResult<ThinPoolStatus> {
554554
let (_, status) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;
555-
556-
assert_eq!(
557-
status.len(),
558-
1,
559-
"Kernel must return 1 line from thin pool status"
560-
);
561-
562-
status.first().expect("assertion above holds").3.parse()
555+
get_status(&status)?.parse()
563556
}
564557

565558
/// Set the table for the existing metadata device.

0 commit comments

Comments
 (0)