Skip to content

Commit dd3d341

Browse files
committed
name field in raw malformed value error
1 parent 6c19642 commit dd3d341

File tree

2 files changed

+75
-62
lines changed

2 files changed

+75
-62
lines changed

src/raw/elem.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ impl<'a> RawBson<'a> {
5050
pub fn as_f64(self) -> RawResult<f64> {
5151
if let ElementType::Double = self.element_type {
5252
Ok(f64::from_bits(u64::from_le_bytes(
53-
self.data
54-
.try_into()
55-
.map_err(|_| RawError::MalformedValue("f64 should be 8 bytes long".into()))?,
53+
self.data.try_into().map_err(|_| RawError::MalformedValue {
54+
message: "f64 should be 8 bytes long".into(),
55+
})?,
5656
)))
5757
} else {
5858
Err(RawError::UnexpectedType)
@@ -92,22 +92,22 @@ impl<'a> RawBson<'a> {
9292
let length = i32_from_slice(&self.data[0..4]);
9393
let subtype = BinarySubtype::from(self.data[4]); // TODO: This mishandles reserved values
9494
if self.data.len() as i32 != length + 5 {
95-
return Err(RawError::MalformedValue(
96-
"binary bson has wrong declared length".into(),
97-
));
95+
return Err(RawError::MalformedValue {
96+
message: "binary bson has wrong declared length".into(),
97+
});
9898
}
9999
let data = match subtype {
100100
BinarySubtype::BinaryOld => {
101101
if length < 4 {
102-
return Err(RawError::MalformedValue(
103-
"old binary subtype has no inner declared length".into(),
104-
));
102+
return Err(RawError::MalformedValue {
103+
message: "old binary subtype has no inner declared length".into(),
104+
});
105105
}
106106
let oldlength = i32_from_slice(&self.data[5..9]);
107107
if oldlength + 4 != length {
108-
return Err(RawError::MalformedValue(
109-
"old binary subtype has wrong inner declared length".into(),
110-
));
108+
return Err(RawError::MalformedValue {
109+
message: "old binary subtype has wrong inner declared length".into(),
110+
});
111111
}
112112
&self.data[9..]
113113
}
@@ -123,7 +123,9 @@ impl<'a> RawBson<'a> {
123123
pub fn as_object_id(self) -> RawResult<ObjectId> {
124124
if let ElementType::ObjectId = self.element_type {
125125
Ok(ObjectId::with_bytes(self.data.try_into().map_err(
126-
|_| RawError::MalformedValue("object id should be 12 bytes long".into()),
126+
|_| RawError::MalformedValue {
127+
message: "object id should be 12 bytes long".into(),
128+
},
127129
)?))
128130
} else {
129131
Err(RawError::UnexpectedType)
@@ -134,14 +136,16 @@ impl<'a> RawBson<'a> {
134136
pub fn as_bool(self) -> RawResult<bool> {
135137
if let ElementType::Boolean = self.element_type {
136138
if self.data.len() != 1 {
137-
Err(RawError::MalformedValue("boolean has length != 1".into()))
139+
Err(RawError::MalformedValue {
140+
message: "boolean has length != 1".into(),
141+
})
138142
} else {
139143
match self.data[0] {
140144
0 => Ok(false),
141145
1 => Ok(true),
142-
_ => Err(RawError::MalformedValue(
143-
"boolean value was not 0 or 1".into(),
144-
)),
146+
_ => Err(RawError::MalformedValue {
147+
message: "boolean value was not 0 or 1".into(),
148+
}),
145149
}
146150
}
147151
} else {
@@ -371,9 +375,9 @@ impl<'a> RawRegex<'a> {
371375
options: opts,
372376
})
373377
} else {
374-
Err(RawError::MalformedValue(
375-
"expected two null-terminated strings".into(),
376-
))
378+
Err(RawError::MalformedValue {
379+
message: "expected two null-terminated strings".into(),
380+
})
377381
}
378382
}
379383

src/raw/mod.rs

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ pub enum RawError {
126126
UnexpectedType,
127127

128128
/// A BSON value did not fit the proper format.
129-
MalformedValue(String),
129+
MalformedValue { message: String },
130130

131-
/// Improper UTF-8 bytes were found when proper UTF-7 was expected. The error value contains
131+
/// Improper UTF-8 bytes were found when proper UTF-8 was expected. The error value contains
132132
/// the malformed data as bytes.
133133
Utf8EncodingError(Vec<u8>),
134134
}
@@ -137,7 +137,7 @@ impl std::fmt::Display for RawError {
137137
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
138138
match self {
139139
Self::UnexpectedType => write!(f, "unexpected type"),
140-
Self::MalformedValue(s) => write!(f, "malformed value: {:?}", s),
140+
Self::MalformedValue { message } => write!(f, "malformed value: {:?}", message),
141141
Self::Utf8EncodingError(_) => write!(f, "utf-8 encoding error"),
142142
}
143143
}
@@ -205,19 +205,23 @@ impl RawDocument {
205205
/// ```
206206
pub fn new(data: Vec<u8>) -> RawResult<RawDocument> {
207207
if data.len() < 5 {
208-
return Err(RawError::MalformedValue("document too short".into()));
208+
return Err(RawError::MalformedValue {
209+
message: "document too short".into(),
210+
});
209211
}
210212

211213
let length = i32_from_slice(&data[..4]);
212214

213215
if data.len() as i32 != length {
214-
return Err(RawError::MalformedValue("document length incorrect".into()));
216+
return Err(RawError::MalformedValue {
217+
message: "document length incorrect".into(),
218+
});
215219
}
216220

217221
if data[data.len() - 1] != 0 {
218-
return Err(RawError::MalformedValue(
219-
"document not null-terminated".into(),
220-
));
222+
return Err(RawError::MalformedValue {
223+
message: "document not null-terminated".into(),
224+
});
221225
}
222226

223227
Ok(Self {
@@ -389,19 +393,23 @@ impl RawDocumentRef {
389393
let data = data.as_ref();
390394

391395
if data.len() < 5 {
392-
return Err(RawError::MalformedValue("document too short".into()));
396+
return Err(RawError::MalformedValue {
397+
message: "document too short".into(),
398+
});
393399
}
394400

395401
let length = i32_from_slice(&data[..4]);
396402

397403
if data.len() as i32 != length {
398-
return Err(RawError::MalformedValue("document length incorrect".into()));
404+
return Err(RawError::MalformedValue {
405+
message: "document length incorrect".into(),
406+
});
399407
}
400408

401409
if data[data.len() - 1] != 0 {
402-
return Err(RawError::MalformedValue(
403-
"document not null-terminated".into(),
404-
));
410+
return Err(RawError::MalformedValue {
411+
message: "document not null-terminated".into(),
412+
});
405413
}
406414

407415
Ok(RawDocumentRef::new_unchecked(data))
@@ -803,9 +811,9 @@ impl<'a> Iterator for RawDocumentIter<'a> {
803811
// end of document marker
804812
return None;
805813
} else {
806-
return Some(Err(RawError::MalformedValue(
807-
"document not null terminated".into(),
808-
)));
814+
return Some(Err(RawError::MalformedValue {
815+
message: "document not null terminated".into(),
816+
}));
809817
}
810818
}
811819

@@ -819,10 +827,9 @@ impl<'a> Iterator for RawDocumentIter<'a> {
819827
let element_type = match ElementType::from(self.doc.data[self.offset]) {
820828
Some(et) => et,
821829
None => {
822-
return Some(Err(RawError::MalformedValue(format!(
823-
"invalid tag: {}",
824-
self.doc.data[self.offset]
825-
))))
830+
return Some(Err(RawError::MalformedValue {
831+
message: format!("invalid tag: {}", self.doc.data[self.offset]),
832+
}))
826833
}
827834
};
828835

@@ -833,9 +840,9 @@ impl<'a> Iterator for RawDocumentIter<'a> {
833840
4 + i32_from_slice(&self.doc.data[valueoffset..valueoffset + 4]) as usize;
834841

835842
if self.doc.data[valueoffset + size - 1] != 0 {
836-
return Some(Err(RawError::MalformedValue(
837-
"string not null terminated".into(),
838-
)));
843+
return Some(Err(RawError::MalformedValue {
844+
message: "string not null terminated".into(),
845+
}));
839846
}
840847

841848
size
@@ -844,9 +851,9 @@ impl<'a> Iterator for RawDocumentIter<'a> {
844851
let size = i32_from_slice(&self.doc.data[valueoffset..valueoffset + 4]) as usize;
845852

846853
if self.doc.data[valueoffset + size - 1] != 0 {
847-
return Some(Err(RawError::MalformedValue(
848-
"document not null terminated".into(),
849-
)));
854+
return Some(Err(RawError::MalformedValue {
855+
message: "document not null terminated".into(),
856+
}));
850857
}
851858

852859
size
@@ -855,9 +862,9 @@ impl<'a> Iterator for RawDocumentIter<'a> {
855862
let size = i32_from_slice(&self.doc.data[valueoffset..valueoffset + 4]) as usize;
856863

857864
if self.doc.data[valueoffset + size - 1] != 0 {
858-
return Some(Err(RawError::MalformedValue(
859-
"array not null terminated".into(),
860-
)));
865+
return Some(Err(RawError::MalformedValue {
866+
message: "array not null terminated".into(),
867+
}));
861868
}
862869

863870
size
@@ -891,9 +898,9 @@ impl<'a> Iterator for RawDocumentIter<'a> {
891898
let id_size = 12;
892899

893900
if self.doc.data[valueoffset + string_size - 1] != 0 {
894-
return Some(Err(RawError::MalformedValue(
895-
"DBPointer string not null-terminated".into(),
896-
)));
901+
return Some(Err(RawError::MalformedValue {
902+
message: "DBPointer string not null-terminated".into(),
903+
}));
897904
}
898905

899906
string_size + id_size
@@ -903,9 +910,9 @@ impl<'a> Iterator for RawDocumentIter<'a> {
903910
4 + i32_from_slice(&self.doc.data[valueoffset..valueoffset + 4]) as usize;
904911

905912
if self.doc.data[valueoffset + size - 1] != 0 {
906-
return Some(Err(RawError::MalformedValue(
907-
"javascript code not null-terminated".into(),
908-
)));
913+
return Some(Err(RawError::MalformedValue {
914+
message: "javascript code not null-terminated".into(),
915+
}));
909916
}
910917

911918
size
@@ -917,9 +924,9 @@ impl<'a> Iterator for RawDocumentIter<'a> {
917924
let size = i32_from_slice(&self.doc.data[valueoffset..valueoffset + 4]) as usize;
918925

919926
if self.doc.data[valueoffset + size - 1] != 0 {
920-
return Some(Err(RawError::MalformedValue(
921-
"javascript with scope not null-terminated".into(),
922-
)));
927+
return Some(Err(RawError::MalformedValue {
928+
message: "javascript with scope not null-terminated".into(),
929+
}));
923930
}
924931

925932
size
@@ -1135,13 +1142,15 @@ fn d128_from_slice(val: &[u8]) -> Decimal128 {
11351142

11361143
fn read_nullterminated(buf: &[u8]) -> RawResult<&str> {
11371144
let mut splits = buf.splitn(2, |x| *x == 0);
1138-
let value = splits
1139-
.next()
1140-
.ok_or_else(|| RawError::MalformedValue("no value".into()))?;
1145+
let value = splits.next().ok_or_else(|| RawError::MalformedValue {
1146+
message: "no value".into(),
1147+
})?;
11411148
if splits.next().is_some() {
11421149
Ok(try_to_str(value)?)
11431150
} else {
1144-
Err(RawError::MalformedValue("expected null terminator".into()))
1151+
Err(RawError::MalformedValue {
1152+
message: "expected null terminator".into(),
1153+
})
11451154
}
11461155
}
11471156

0 commit comments

Comments
 (0)