Skip to content

Commit

Permalink
Abstract getting unique status result into a single method
Browse files Browse the repository at this point in the history
Removes a possibly failing assertion and some unnecessary code
duplication.

Signed-off-by: mulhern <amulhern@redhat.com>
  • Loading branch information
mulkieran committed Feb 26, 2019
1 parent b25bcd0 commit 9d204d9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
11 changes: 2 additions & 9 deletions src/cachedev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::dm_options::DmOptions;
use crate::lineardev::{LinearDev, LinearDevTargetParams};
use crate::result::{DmError, DmResult, ErrorEnum};
use crate::shared::{
device_create, device_exists, device_match, get_status_line_fields,
device_create, device_exists, device_match, get_status, get_status_line_fields,
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
TargetTable,
};
Expand Down Expand Up @@ -718,14 +718,7 @@ impl CacheDev {
/// Get the current status of the cache device.
pub fn status(&self, dm: &DM) -> DmResult<CacheDevStatus> {
let (_, status) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;

assert_eq!(
status.len(),
1,
"Kernel must return 1 line from cache dev status"
);

status.first().expect("assertion above holds").3.parse()
get_status(&status)?.parse()
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,27 @@ pub fn get_status_line_fields<'a>(
Ok(status_vals)
}

/// Get unique status element from a status result.
/// Return an error if an incorrect number of lines are obtained.
pub fn get_status(status_lines: &[(Sectors, Sectors, TargetTypeBuf, String)]) -> DmResult<String> {
let length = status_lines.len();
if length != 1 {
return Err(DmError::Dm(
ErrorEnum::Invalid,
format!(
"Incorrect number of lines for status; expected 1, found {} in status result \"{}\"",
length,
status_lines.iter().map(|(s, l, t, v)| format!("{} {} {} {}", *s, *l, t.to_string(), v)).collect::<Vec<String>>().join(", ")
),
));
}
Ok(status_lines
.first()
.expect("if length != 1, already returned")
.3
.to_owned())
}

/// Construct an error when parsing yields an unexpected value.
/// Indicate the location of the unexpected value, 1-indexed, its actual
/// value, and the name of the expected thing.
Expand Down
13 changes: 3 additions & 10 deletions src/thindev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::dm_flags::{DmCookie, DmFlags};
use crate::dm_options::DmOptions;
use crate::result::{DmError, DmResult, ErrorEnum};
use crate::shared::{
device_create, device_exists, device_match, get_status_line_fields, message, parse_device,
parse_value, DmDevice, TargetLine, TargetParams, TargetTable,
device_create, device_exists, device_match, get_status, get_status_line_fields, message,
parse_device, parse_value, DmDevice, TargetLine, TargetParams, TargetTable,
};
use crate::thindevid::ThinDevId;
use crate::thinpooldev::ThinPoolDev;
Expand Down Expand Up @@ -385,14 +385,7 @@ impl ThinDev {
/// Get the current status of the thin device.
pub fn status(&self, dm: &DM) -> DmResult<ThinStatus> {
let (_, table) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;

assert_eq!(
table.len(),
1,
"Kernel must return 1 line table for thin status"
);

table.first().expect("assertion above holds").3.parse()
get_status(&table)?.parse()
}

/// Set the table for the thin device's target
Expand Down
11 changes: 2 additions & 9 deletions src/thinpooldev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::dm_options::DmOptions;
use crate::lineardev::{LinearDev, LinearDevTargetParams};
use crate::result::{DmError, DmResult, ErrorEnum};
use crate::shared::{
device_create, device_exists, device_match, get_status_line_fields,
device_create, device_exists, device_match, get_status, get_status_line_fields,
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
TargetTable,
};
Expand Down Expand Up @@ -552,14 +552,7 @@ impl ThinPoolDev {
/// Returns an error if there was an error getting the status value.
pub fn status(&self, dm: &DM) -> DmResult<ThinPoolStatus> {
let (_, status) = dm.table_status(&DevId::Name(self.name()), &DmOptions::new())?;

assert_eq!(
status.len(),
1,
"Kernel must return 1 line from thin pool status"
);

status.first().expect("assertion above holds").3.parse()
get_status(&status)?.parse()
}

/// Set the table for the existing metadata device.
Expand Down

0 comments on commit 9d204d9

Please sign in to comment.