|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +// A module to contain functionality used for generating DM ids which |
| 6 | +// are restricted in length and format by devicemapper. |
| 7 | + |
| 8 | +// Evaluates to an error string if the value does not match the requirements. |
| 9 | +macro_rules! str_check { |
| 10 | + ($value:expr, $max_allowed_chars:expr) => {{ |
| 11 | + let value = $value; |
| 12 | + let max_allowed_chars = $max_allowed_chars; |
| 13 | + if !value.is_ascii() { |
| 14 | + Some(format!("value {} has some non-ascii characters", value)) |
| 15 | + } else { |
| 16 | + let num_chars = value.len(); |
| 17 | + if num_chars == 0 { |
| 18 | + Some("value has zero characters".into()) |
| 19 | + } else if num_chars > max_allowed_chars { |
| 20 | + Some(format!( |
| 21 | + "value {} has {} chars which is greater than maximum allowed {}", |
| 22 | + value, num_chars, max_allowed_chars |
| 23 | + )) |
| 24 | + } else { |
| 25 | + None |
| 26 | + } |
| 27 | + } |
| 28 | + }}; |
| 29 | +} |
| 30 | + |
| 31 | +/// Define borrowed and owned versions of string types that guarantee |
| 32 | +/// conformance to DM restrictions, such as maximum length. |
| 33 | +// This implementation follows the example of Path/PathBuf as closely as |
| 34 | +// possible. |
| 35 | +macro_rules! str_id { |
| 36 | + ($B:ident, $O:ident, $MAX:ident) => { |
| 37 | + /// The borrowed version of the DM identifier. |
| 38 | + #[derive(Debug, PartialEq, Eq, Hash)] |
| 39 | + pub struct $B { |
| 40 | + inner: str, |
| 41 | + } |
| 42 | + |
| 43 | + /// The owned version of the DM identifier. |
| 44 | + #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 45 | + pub struct $O { |
| 46 | + inner: String, |
| 47 | + } |
| 48 | + |
| 49 | + impl $B { |
| 50 | + /// Create a new borrowed identifier from a `&str`. |
| 51 | + #[allow(clippy::new_ret_no_self)] |
| 52 | + pub fn new(value: &str) -> DmResult<&$B> { |
| 53 | + if let Some(err_msg) = str_check!(value, $MAX - 1) { |
| 54 | + return Err(DmError::Core( |
| 55 | + ErrorKind::InvalidArgument(err_msg.into()).into(), |
| 56 | + )); |
| 57 | + } |
| 58 | + Ok(unsafe { &*(value as *const str as *const $B) }) |
| 59 | + } |
| 60 | + |
| 61 | + /// Get the inner value as bytes |
| 62 | + pub fn as_bytes(&self) -> &[u8] { |
| 63 | + self.inner.as_bytes() |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + impl ToOwned for $B { |
| 68 | + type Owned = $O; |
| 69 | + fn to_owned(&self) -> $O { |
| 70 | + $O { |
| 71 | + inner: self.inner.to_owned(), |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + impl fmt::Display for $B { |
| 77 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 78 | + write!(f, "{}", &self.inner) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + impl $O { |
| 83 | + /// Construct a new owned identifier. |
| 84 | + #[allow(clippy::new_ret_no_self)] |
| 85 | + pub fn new(value: String) -> DmResult<$O> { |
| 86 | + if let Some(err_msg) = str_check!(&value, $MAX - 1) { |
| 87 | + return Err(DmError::Core( |
| 88 | + ErrorKind::InvalidArgument(err_msg.into()).into(), |
| 89 | + )); |
| 90 | + } |
| 91 | + Ok($O { inner: value }) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + impl AsRef<$B> for $O { |
| 96 | + fn as_ref(&self) -> &$B { |
| 97 | + self |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + impl Borrow<$B> for $O { |
| 102 | + fn borrow(&self) -> &$B { |
| 103 | + self.deref() |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + impl Deref for $O { |
| 108 | + type Target = $B; |
| 109 | + fn deref(&self) -> &$B { |
| 110 | + $B::new(&self.inner).expect("inner satisfies all correctness criteria for $B::new") |
| 111 | + } |
| 112 | + } |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +#[cfg(test)] |
| 117 | +mod tests { |
| 118 | + use std::borrow::Borrow; |
| 119 | + use std::fmt; |
| 120 | + use std::iter; |
| 121 | + use std::ops::Deref; |
| 122 | + |
| 123 | + use crate::errors::{Error, ErrorKind}; |
| 124 | + use crate::result::{DmError, DmResult}; |
| 125 | + |
| 126 | + const TYPE_LEN: usize = 12; |
| 127 | + str_id!(Id, IdBuf, TYPE_LEN); |
| 128 | + |
| 129 | + #[test] |
| 130 | + /// Test for errors on an empty name. |
| 131 | + fn test_empty_name() { |
| 132 | + assert!(match Id::new("") { |
| 133 | + Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true, |
| 134 | + _ => false, |
| 135 | + }); |
| 136 | + assert!(match IdBuf::new("".into()) { |
| 137 | + Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true, |
| 138 | + _ => false, |
| 139 | + }) |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + /// Test for errors on an overlong name. |
| 144 | + fn test_too_long_name() { |
| 145 | + let name = iter::repeat('a').take(TYPE_LEN + 1).collect::<String>(); |
| 146 | + assert!(match Id::new(&name) { |
| 147 | + Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true, |
| 148 | + _ => false, |
| 149 | + }); |
| 150 | + assert!(match IdBuf::new(name) { |
| 151 | + Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true, |
| 152 | + _ => false, |
| 153 | + }) |
| 154 | + } |
| 155 | + |
| 156 | + #[test] |
| 157 | + /// Test the concrete methods and traits of the interface. |
| 158 | + fn test_interface() { |
| 159 | + let id = Id::new("id").expect("is valid id"); |
| 160 | + let id_buf = IdBuf::new("id".into()).expect("is valid id"); |
| 161 | + |
| 162 | + // Test as_bytes. |
| 163 | + assert_eq!(id.as_bytes(), &[105u8, 100u8]); |
| 164 | + assert_eq!(id_buf.as_bytes(), &[105u8, 100u8]); |
| 165 | + |
| 166 | + // Test ToOwned implementation. |
| 167 | + // $B.to_owned() == $O |
| 168 | + assert_eq!(id.to_owned(), id_buf); |
| 169 | + |
| 170 | + // Test Display implementation |
| 171 | + // X.to_string() = (*X).to_string() |
| 172 | + assert_eq!(id.to_string(), (*id).to_string()); |
| 173 | + assert_eq!(id_buf.to_string(), (*id_buf).to_string()); |
| 174 | + |
| 175 | + // Test Deref |
| 176 | + assert_eq!(id_buf.deref(), id); |
| 177 | + assert_eq!(*id_buf, *id); |
| 178 | + } |
| 179 | +} |
0 commit comments