Skip to content

Commit a10b680

Browse files
binary
1 parent 6380979 commit a10b680

File tree

2 files changed

+33
-61
lines changed

2 files changed

+33
-61
lines changed

src/binary.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#! Module containing functionality related to BSON binary values.
2-
32
mod vector;
43

5-
use crate::{base64, spec::BinarySubtype, Document, RawBinaryRef};
64
use std::{
75
convert::TryFrom,
8-
error,
96
fmt::{self, Display},
107
};
118

9+
use crate::{
10+
base64,
11+
error::{Error, Result},
12+
spec::BinarySubtype,
13+
Document,
14+
RawBinaryRef,
15+
};
16+
1217
pub use vector::{PackedBitVector, Vector};
1318

1419
/// Represents a BSON binary value.
@@ -51,9 +56,7 @@ impl Binary {
5156
input: impl AsRef<str>,
5257
subtype: impl Into<Option<BinarySubtype>>,
5358
) -> Result<Self> {
54-
let bytes = base64::decode(input.as_ref()).map_err(|e| Error::DecodingError {
55-
message: e.to_string(),
56-
})?;
59+
let bytes = base64::decode(input.as_ref()).map_err(Error::invalid_value)?;
5760
let subtype = match subtype.into() {
5861
Some(s) => s,
5962
None => BinarySubtype::Generic,
@@ -97,27 +100,3 @@ impl Binary {
97100
}
98101
}
99102
}
100-
101-
/// Possible errors that can arise during [`Binary`] construction.
102-
#[derive(Clone, Debug)]
103-
#[non_exhaustive]
104-
pub enum Error {
105-
/// While trying to decode from base64, an error was returned.
106-
DecodingError { message: String },
107-
108-
/// A [`Vector`]-related error occurred.
109-
Vector { message: String },
110-
}
111-
112-
impl error::Error for Error {}
113-
114-
impl std::fmt::Display for Error {
115-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
116-
match self {
117-
Error::DecodingError { message } => fmt.write_str(message),
118-
Error::Vector { message } => fmt.write_str(message),
119-
}
120-
}
121-
}
122-
123-
pub type Result<T> = std::result::Result<T, Error>;

src/binary/vector.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,14 @@ 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::Vector {
97-
message: format!("padding must be within 0-7 inclusive, got {}", padding),
98-
});
96+
return Err(Error::invalid_value(format!(
97+
"padding must be within 0-7 inclusive, got {padding}"
98+
)));
9999
}
100100
if padding != 0 && vector.is_empty() {
101-
return Err(Error::Vector {
102-
message: format!(
103-
"cannot specify non-zero padding if the provided vector is empty, got {}",
104-
padding
105-
),
106-
});
101+
return Err(Error::invalid_value(format!(
102+
"cannot specify non-zero padding if the provided vector is empty, got {padding}",
103+
)));
107104
}
108105
Ok(Self { vector, padding })
109106
}
@@ -117,24 +114,19 @@ impl Vector {
117114
let bytes = bytes.as_ref();
118115

119116
if bytes.len() < 2 {
120-
return Err(Error::Vector {
121-
message: format!(
122-
"the provided bytes must have a length of at least 2, got {}",
123-
bytes.len()
124-
),
125-
});
117+
return Err(Error::invalid_value(format!(
118+
"the provided bytes must have a length of at least 2, got {}",
119+
bytes.len()
120+
)));
126121
}
127122

128123
let d_type = bytes[0];
129124
let padding = bytes[1];
130125
if d_type != PACKED_BIT && padding != 0 {
131-
return Err(Error::Vector {
132-
message: format!(
133-
"padding can only be specified for a packed bit vector (data type {}), got \
134-
type {}",
135-
PACKED_BIT, d_type
136-
),
137-
});
126+
return Err(Error::invalid_value(format!(
127+
"padding can only be specified for a packed bit vector (data type {}), got type {}",
128+
PACKED_BIT, d_type
129+
)));
138130
}
139131
let number_bytes = &bytes[2..];
140132

@@ -151,11 +143,11 @@ impl Vector {
151143

152144
let mut vector = Vec::new();
153145
for chunk in number_bytes.chunks(F32_BYTES) {
154-
let bytes: [u8; F32_BYTES] = chunk.try_into().map_err(|_| Error::Vector {
155-
message: format!(
146+
let bytes: [u8; F32_BYTES] = chunk.try_into().map_err(|_| {
147+
Error::invalid_value(format!(
156148
"f32 vector values must be {} bytes, got {:?}",
157149
F32_BYTES, chunk,
158-
),
150+
))
159151
})?;
160152
vector.push(f32::from_le_bytes(bytes));
161153
}
@@ -165,9 +157,9 @@ impl Vector {
165157
let packed_bit_vector = PackedBitVector::new(number_bytes.to_vec(), padding)?;
166158
Ok(Self::PackedBit(packed_bit_vector))
167159
}
168-
other => Err(Error::Vector {
169-
message: format!("unsupported vector data type: {}", other),
170-
}),
160+
other => Err(Error::invalid_value(format!(
161+
"unsupported vector data type: {other}"
162+
))),
171163
}
172164
}
173165

@@ -230,9 +222,10 @@ impl TryFrom<&Binary> for Vector {
230222

231223
fn try_from(binary: &Binary) -> Result<Self> {
232224
if binary.subtype != BinarySubtype::Vector {
233-
return Err(Error::Vector {
234-
message: format!("expected vector binary subtype, got {:?}", binary.subtype),
235-
});
225+
return Err(Error::invalid_value(format!(
226+
"expected vector binary subtype, got {:?}",
227+
binary.subtype
228+
)));
236229
}
237230
Self::from_bytes(&binary.bytes)
238231
}

0 commit comments

Comments
 (0)