Skip to content

Commit 04c6d31

Browse files
maintain binary structure
1 parent 9444b87 commit 04c6d31

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Binary {
5656
input: impl AsRef<str>,
5757
subtype: impl Into<Option<BinarySubtype>>,
5858
) -> Result<Self> {
59-
let bytes = base64::decode(input.as_ref()).map_err(Error::invalid_value)?;
59+
let bytes = base64::decode(input.as_ref()).map_err(Error::binary)?;
6060
let subtype = match subtype.into() {
6161
Some(s) => s,
6262
None => BinarySubtype::Generic,

src/binary/vector.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ impl PackedBitVector {
9393
pub fn new(vector: Vec<u8>, padding: impl Into<Option<u8>>) -> Result<Self> {
9494
let padding = padding.into().unwrap_or(0);
9595
if !(0..8).contains(&padding) {
96-
return Err(Error::invalid_value(format!(
97-
"padding must be within 0-7 inclusive, got {padding}"
96+
return Err(Error::binary(format!(
97+
"vector padding must be within 0-7 inclusive, got {padding}"
9898
)));
9999
}
100100
if padding != 0 && vector.is_empty() {
101-
return Err(Error::invalid_value(format!(
101+
return Err(Error::binary(format!(
102102
"cannot specify non-zero padding if the provided vector is empty, got {padding}",
103103
)));
104104
}
@@ -114,16 +114,16 @@ impl Vector {
114114
let bytes = bytes.as_ref();
115115

116116
if bytes.len() < 2 {
117-
return Err(Error::invalid_value(format!(
118-
"the provided bytes must have a length of at least 2, got {}",
117+
return Err(Error::binary(format!(
118+
"the provided vector bytes must have a length of at least 2, got {}",
119119
bytes.len()
120120
)));
121121
}
122122

123123
let d_type = bytes[0];
124124
let padding = bytes[1];
125125
if d_type != PACKED_BIT && padding != 0 {
126-
return Err(Error::invalid_value(format!(
126+
return Err(Error::binary(format!(
127127
"padding can only be specified for a packed bit vector (data type {}), got type {}",
128128
PACKED_BIT, d_type
129129
)));
@@ -144,7 +144,7 @@ impl Vector {
144144
let mut vector = Vec::new();
145145
for chunk in number_bytes.chunks(F32_BYTES) {
146146
let bytes: [u8; F32_BYTES] = chunk.try_into().map_err(|_| {
147-
Error::invalid_value(format!(
147+
Error::binary(format!(
148148
"f32 vector values must be {} bytes, got {:?}",
149149
F32_BYTES, chunk,
150150
))
@@ -157,7 +157,7 @@ impl Vector {
157157
let packed_bit_vector = PackedBitVector::new(number_bytes.to_vec(), padding)?;
158158
Ok(Self::PackedBit(packed_bit_vector))
159159
}
160-
other => Err(Error::invalid_value(format!(
160+
other => Err(Error::binary(format!(
161161
"unsupported vector data type: {other}"
162162
))),
163163
}
@@ -222,7 +222,7 @@ impl TryFrom<&Binary> for Vector {
222222

223223
fn try_from(binary: &Binary) -> Result<Self> {
224224
if binary.subtype != BinarySubtype::Vector {
225-
return Err(Error::invalid_value(format!(
225+
return Err(Error::binary(format!(
226226
"expected vector binary subtype, got {:?}",
227227
binary.subtype
228228
)));

src/error.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ impl std::fmt::Display for Error {
4242
#[derive(Clone, Debug, Error)]
4343
#[non_exhaustive]
4444
pub enum ErrorKind {
45+
/// An error related to the [`Binary`](crate::Binary) type occurred.
46+
#[error("A Binary-related error occurred: {message}")]
47+
Binary {
48+
/// A message describing the error.
49+
message: String,
50+
},
51+
4552
/// An error related to the [`DateTime`](crate::DateTime) type occurred.
4653
#[error("A DateTime-related error occurred: {kind}")]
4754
DateTime {
@@ -113,15 +120,15 @@ impl Error {
113120
self
114121
}
115122

116-
pub(crate) fn malformed_bytes(message: impl ToString) -> Self {
117-
ErrorKind::MalformedBytes {
123+
pub(crate) fn binary(message: impl ToString) -> Self {
124+
ErrorKind::Binary {
118125
message: message.to_string(),
119126
}
120127
.into()
121128
}
122129

123-
pub(crate) fn invalid_value(message: impl ToString) -> Self {
124-
ErrorKind::InvalidValue {
130+
pub(crate) fn malformed_bytes(message: impl ToString) -> Self {
131+
ErrorKind::MalformedBytes {
125132
message: message.to_string(),
126133
}
127134
.into()

0 commit comments

Comments
 (0)