Skip to content

Commit 49d9135

Browse files
committed
cleanup
1 parent cbc8dae commit 49d9135

File tree

10 files changed

+26
-25
lines changed

10 files changed

+26
-25
lines changed

src/bson.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ impl Bson {
848848
if let Ok(regex) = doc.get_document("$regularExpression") {
849849
if let Ok(pattern) = regex.get_str("pattern") {
850850
if let Ok(options) = regex.get_str("options") {
851-
if let Ok(regex) = Regex::new(pattern, options) {
851+
if let Ok(regex) = Regex::from_strings(pattern, options) {
852852
return Bson::RegularExpression(regex);
853853
}
854854
}
@@ -1168,7 +1168,8 @@ pub struct Regex {
11681168
}
11691169

11701170
impl Regex {
1171-
pub(crate) fn new(
1171+
#[cfg(feature = "serde")]
1172+
pub(crate) fn from_strings(
11721173
pattern: impl AsRef<str>,
11731174
options: impl AsRef<str>,
11741175
) -> crate::error::Result<Self> {

src/de/serde.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ impl<'de> Visitor<'de> for BsonVisitor {
443443
"$regularExpression" => {
444444
let re = visitor.next_value::<extjson::models::RegexBody>()?;
445445
return Ok(Bson::RegularExpression(
446-
Regex::new(re.pattern, re.options).map_err(serde::de::Error::custom)?,
446+
Regex::from_strings(re.pattern, re.options)
447+
.map_err(serde::de::Error::custom)?,
447448
));
448449
}
449450

src/extjson/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub(crate) struct RegexBody {
123123

124124
impl Regex {
125125
pub(crate) fn parse(self) -> crate::error::Result<crate::Regex> {
126-
crate::Regex::new(self.body.pattern, self.body.options)
126+
crate::Regex::from_strings(self.body.pattern, self.body.options)
127127
}
128128
}
129129

src/raw.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,3 @@ pub(crate) fn write_string(buf: &mut Vec<u8>, s: &str) {
318318
buf.extend(s.as_bytes());
319319
buf.push(0);
320320
}
321-
322-
pub(crate) fn write_cstring(buf: &mut Vec<u8>, s: &str) -> Result<()> {
323-
if s.contains('\0') {
324-
return Err(Error::malformed_bytes(format!(
325-
"cstring with interior null: {:?}",
326-
s
327-
)));
328-
}
329-
buf.extend(s.as_bytes());
330-
buf.push(0);
331-
Ok(())
332-
}

src/raw/cstr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ impl<'a> TryFrom<&'a str> for &'a CStr {
2424
}
2525

2626
impl CStr {
27+
// Convenience shorthand for making the types of TryFrom line up
28+
#[cfg(feature = "serde")]
29+
pub(crate) fn from_str(value: &str) -> Result<&CStr> {
30+
value.try_into()
31+
}
32+
2733
const fn from_str_unchecked(value: &str) -> &Self {
2834
// Safety: the conversion is safe because CStr is repr(transparent), and the deref is safe
2935
// because the pointer came from a safe reference.

src/raw/serde/seeded_visitor.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{
66
};
77

88
use crate::{
9-
raw::{write_cstring, write_string, RAW_BSON_NEWTYPE},
9+
raw::{write_string, RAW_BSON_NEWTYPE},
1010
spec::{BinarySubtype, ElementType},
1111
RawBson,
1212
RawBsonRef,
@@ -119,7 +119,9 @@ impl<'a, 'de> SeededVisitor<'a, 'de> {
119119

120120
/// Appends a cstring to the buffer. Returns an error if the given string contains a null byte.
121121
fn append_cstring(&mut self, key: &str) -> Result<(), String> {
122-
write_cstring(self.buffer.get_owned_buffer(), key).map_err(|e| e.to_string())
122+
Ok(crate::raw::CStr::from_str(key)
123+
.map_err(|e| e.to_string())?
124+
.append_to(self.buffer.get_owned_buffer()))
123125
}
124126

125127
/// Appends a string and its length to the buffer.

src/raw/test/append.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,14 @@ fn undefined() {
243243
#[test]
244244
fn regex() {
245245
let expected = doc! {
246-
"regex": Regex::new("some pattern", "abc").unwrap(),
246+
"regex": Regex::from_strings("some pattern", "abc").unwrap(),
247247
};
248248

249249
append_test(expected, |doc| {
250-
doc.append(cstr!("regex"), Regex::new("some pattern", "abc").unwrap())
250+
doc.append(
251+
cstr!("regex"),
252+
Regex::from_strings("some pattern", "abc").unwrap(),
253+
)
251254
});
252255
}
253256

src/raw/test/props.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) fn arbitrary_bson() -> impl Strategy<Value = Bson> {
2222
any::<i32>().prop_map(Bson::Int32),
2323
any::<i64>().prop_map(Bson::Int64),
2424
any::<(String, String)>().prop_map(|(pattern, options)| {
25-
Bson::RegularExpression(Regex::new(pattern, options).unwrap())
25+
Bson::RegularExpression(Regex::from_strings(pattern, options).unwrap())
2626
}),
2727
any::<[u8; 12]>().prop_map(|bytes| Bson::ObjectId(crate::oid::ObjectId::from_bytes(bytes))),
2828
(arbitrary_binary_subtype(), any::<Vec<u8>>()).prop_map(|(subtype, bytes)| {

src/ser/raw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde::{
99
use self::value_serializer::{ValueSerializer, ValueType};
1010

1111
use crate::{
12-
raw::{write_cstring, CStr, RAW_ARRAY_NEWTYPE, RAW_DOCUMENT_NEWTYPE},
12+
raw::{CStr, RAW_ARRAY_NEWTYPE, RAW_DOCUMENT_NEWTYPE},
1313
ser::{Error, Result},
1414
serde_helpers::HUMAN_READABLE_NEWTYPE,
1515
spec::{BinarySubtype, ElementType},
@@ -469,7 +469,7 @@ impl<'a> VariantSerializer<'a> {
469469
T: Serialize + ?Sized,
470470
{
471471
self.root_serializer.reserve_element_type();
472-
write_cstring(&mut self.root_serializer.bytes, k)?;
472+
CStr::from_str(k)?.append_to(&mut self.root_serializer.bytes);
473473
v.serialize(&mut *self.root_serializer)?;
474474

475475
self.num_elements_serialized += 1;

src/ser/raw/document_serializer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use serde::{ser::Impossible, Serialize};
22

33
use crate::{
44
error::{Error, Result},
5-
raw::write_cstring,
65
RawBsonRef,
76
};
87

@@ -258,7 +257,8 @@ impl serde::Serializer for KeySerializer<'_> {
258257

259258
#[inline]
260259
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
261-
write_cstring(&mut self.root_serializer.bytes, v)
260+
crate::raw::CStr::from_str(v)?.append_to(&mut self.root_serializer.bytes);
261+
Ok(())
262262
}
263263

264264
#[inline]

0 commit comments

Comments
 (0)