Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract getting unique status result into a single method #430

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/cachedev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use crate::{
lineardev::{LinearDev, LinearDevTargetParams},
result::{DmError, DmResult, ErrorEnum},
shared::{
device_create, device_exists, device_match, get_status, get_status_line_fields,
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
TargetTable, TargetTypeBuf,
device_create, device_exists, device_match, get_status, get_status_line,
get_status_line_fields, make_unexpected_value_error, parse_device, parse_value, DmDevice,
TargetLine, TargetParams, TargetTable, TargetTypeBuf,
},
units::{DataBlocks, MetaBlocks, Sectors},
};
Expand Down Expand Up @@ -369,6 +369,8 @@ impl FromStr for CacheDevStatus {
// Note: This method is not entirely complete. In particular, *_args values
// may require more or better checking or processing.
fn from_str(status_line: &str) -> DmResult<CacheDevStatus> {
let status_line = get_status_line(status_line, &CACHE_TARGET_NAME)?;

if status_line.starts_with("Error") {
return Ok(CacheDevStatus::Error);
}
Expand Down
28 changes: 24 additions & 4 deletions src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ where
})
}

/// Obtain a status line from a line containing the target type as its first
/// entry. Return an error if the line does not have the expected target type.
/// Precondition: The line's first entry is the target type, followed by a
/// space, followed by the actual status values.
pub fn get_status_line<'a, 'b>(
status_line: &'a str,
expected_target_type: &'b str,
) -> DmResult<&'a str> {
let target_status_pair: Vec<&str> = status_line.splitn(2, ' ').collect();
let target_type = target_status_pair[0];
if target_type != expected_target_type {
let err_msg = format!(
"Expected a \"{}\" target entry but found target type \"{}\" in \"{}\"",
expected_target_type, target_type, status_line
);
return Err(DmError::Dm(ErrorEnum::Invalid, err_msg));
}

Ok(target_status_pair[1])
}

/// Get fields for a single status line.
/// Return an error if an insufficient number of fields are obtained.
pub fn get_status_line_fields<'a>(
Expand Down Expand Up @@ -260,11 +281,10 @@ pub fn get_status(status_lines: &[(u64, u64, String, String)]) -> DmResult<Strin
),
));
}
Ok(status_lines
let line = status_lines
.first()
.expect("if length != 1, already returned")
.3
.to_owned())
.expect("if length != 1, already returned");
Ok(format!("{} {}", line.2, line.3))
}

/// Construct an error when parsing yields an unexpected value.
Expand Down
7 changes: 5 additions & 2 deletions src/thindev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use crate::{
core::{DevId, Device, DeviceInfo, DmCookie, DmFlags, DmName, DmOptions, DmUuid, DM},
result::{DmError, DmResult, ErrorEnum},
shared::{
device_create, device_exists, device_match, get_status, get_status_line_fields, message,
parse_device, parse_value, DmDevice, TargetLine, TargetParams, TargetTable, TargetTypeBuf,
device_create, device_exists, device_match, get_status, get_status_line,
get_status_line_fields, message, parse_device, parse_value, DmDevice, TargetLine,
TargetParams, TargetTable, TargetTypeBuf,
},
thindevid::ThinDevId,
thinpooldev::ThinPoolDev,
Expand Down Expand Up @@ -219,6 +220,8 @@ impl FromStr for ThinStatus {
type Err = DmError;

fn from_str(status_line: &str) -> DmResult<ThinStatus> {
let status_line = get_status_line(status_line, &THIN_TARGET_NAME)?;

if status_line.starts_with("Error") {
return Ok(ThinStatus::Error);
}
Expand Down
8 changes: 5 additions & 3 deletions src/thinpooldev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::{
lineardev::{LinearDev, LinearDevTargetParams},
result::{DmError, DmResult, ErrorEnum},
shared::{
device_create, device_exists, device_match, get_status, get_status_line_fields,
make_unexpected_value_error, parse_device, parse_value, DmDevice, TargetLine, TargetParams,
TargetTable, TargetTypeBuf,
device_create, device_exists, device_match, get_status, get_status_line,
get_status_line_fields, make_unexpected_value_error, parse_device, parse_value, DmDevice,
TargetLine, TargetParams, TargetTable, TargetTypeBuf,
},
units::{DataBlocks, MetaBlocks, Sectors},
};
Expand Down Expand Up @@ -337,6 +337,8 @@ impl FromStr for ThinPoolStatus {
type Err = DmError;

fn from_str(status_line: &str) -> DmResult<ThinPoolStatus> {
let status_line = get_status_line(status_line, &THINPOOL_TARGET_NAME)?;

if status_line.starts_with("Error") {
return Ok(ThinPoolStatus::Error);
}
Expand Down