Skip to content

Commit e052a14

Browse files
committed
Have TargetType error types be outer-layer types
Extend the macro to allow this variation. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent eef99a0 commit e052a14

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

src/core/types.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ use crate::result::{DmError, DmResult};
1111
use crate::core::deviceinfo::{DM_NAME_LEN, DM_UUID_LEN};
1212
use crate::core::errors::ErrorKind;
1313

14+
/// An error function to construct an error when creating a new string id.
15+
fn err_func(err_msg: &str) -> DmError {
16+
DmError::Core(ErrorKind::InvalidArgument(err_msg.into()).into())
17+
}
18+
1419
/// A devicemapper name. Really just a string, but also the argument type of
1520
/// DevId::Name. Used in function arguments to indicate that the function
1621
/// takes only a name, not a devicemapper uuid.
17-
str_id!(DmName, DmNameBuf, DM_NAME_LEN);
22+
str_id!(DmName, DmNameBuf, DM_NAME_LEN, err_func);
1823

1924
/// A devicemapper uuid. A devicemapper uuid has a devicemapper-specific
2025
/// format.
21-
str_id!(DmUuid, DmUuidBuf, DM_UUID_LEN);
26+
str_id!(DmUuid, DmUuidBuf, DM_UUID_LEN, err_func);
2227

2328
/// Used as a parameter for functions that take either a Device name
2429
/// or a Device UUID.

src/id_macros.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ macro_rules! str_check {
3333
// This implementation follows the example of Path/PathBuf as closely as
3434
// possible.
3535
macro_rules! str_id {
36-
($B:ident, $O:ident, $MAX:ident) => {
36+
($B:ident, $O:ident, $MAX:ident, $err_func:ident) => {
3737
/// The borrowed version of the DM identifier.
3838
#[derive(Debug, PartialEq, Eq, Hash)]
3939
pub struct $B {
@@ -51,9 +51,7 @@ macro_rules! str_id {
5151
#[allow(clippy::new_ret_no_self)]
5252
pub fn new(value: &str) -> DmResult<&$B> {
5353
if let Some(err_msg) = str_check!(value, $MAX - 1) {
54-
return Err(DmError::Core(
55-
ErrorKind::InvalidArgument(err_msg.into()).into(),
56-
));
54+
return Err($err_func(&err_msg));
5755
}
5856
Ok(unsafe { &*(value as *const str as *const $B) })
5957
}
@@ -84,9 +82,7 @@ macro_rules! str_id {
8482
#[allow(clippy::new_ret_no_self)]
8583
pub fn new(value: String) -> DmResult<$O> {
8684
if let Some(err_msg) = str_check!(&value, $MAX - 1) {
87-
return Err(DmError::Core(
88-
ErrorKind::InvalidArgument(err_msg.into()).into(),
89-
));
85+
return Err($err_func(&err_msg));
9086
}
9187
Ok($O { inner: value })
9288
}
@@ -124,8 +120,12 @@ mod tests {
124120

125121
use crate::core::errors::{Error, ErrorKind};
126122

123+
fn err_func(err_msg: &str) -> DmError {
124+
DmError::Core(ErrorKind::InvalidArgument(err_msg.into()).into())
125+
}
126+
127127
const TYPE_LEN: usize = 12;
128-
str_id!(Id, IdBuf, TYPE_LEN);
128+
str_id!(Id, IdBuf, TYPE_LEN, err_func);
129129

130130
#[test]
131131
/// Test for errors on an empty name.

src/shared.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ use crate::core::{
1818
use crate::result::{DmError, DmResult, ErrorEnum};
1919
use crate::units::Sectors;
2020

21-
use crate::core::errors::ErrorKind;
21+
fn err_func(err_msg: &str) -> DmError {
22+
DmError::Dm(ErrorEnum::Invalid, err_msg.into())
23+
}
2224

2325
/// Number of bytes in Struct_dm_target_spec::target_type field.
2426
const DM_TARGET_TYPE_LEN: usize = 16;
2527

26-
str_id!(TargetType, TargetTypeBuf, DM_TARGET_TYPE_LEN);
28+
str_id!(TargetType, TargetTypeBuf, DM_TARGET_TYPE_LEN, err_func);
2729

2830
/// The trait for properties of the params string of TargetType
2931
pub trait TargetParams: Clone + fmt::Debug + fmt::Display + Eq + FromStr + PartialEq {

0 commit comments

Comments
 (0)