Skip to content

Commit 8b96eea

Browse files
committed
cleanup
1 parent f3f742e commit 8b96eea

File tree

10 files changed

+25
-25
lines changed

10 files changed

+25
-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
}
@@ -1138,7 +1138,8 @@ pub struct Regex {
11381138
}
11391139

11401140
impl Regex {
1141-
pub(crate) fn new(
1141+
#[cfg(feature = "serde")]
1142+
pub(crate) fn from_strings(
11421143
pattern: impl AsRef<str>,
11431144
options: impl AsRef<str>,
11441145
) -> crate::error::Result<Self> {

src/de/serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<'de> Visitor<'de> for BsonVisitor {
434434
"$regularExpression" => {
435435
let re = visitor.next_value::<extjson::models::RegexBody>()?;
436436
return Ok(Bson::RegularExpression(
437-
Regex::new(re.pattern, re.options).map_err(Error::custom)?,
437+
Regex::from_strings(re.pattern, re.options).map_err(Error::custom)?,
438438
));
439439
}
440440

src/extjson/models.rs

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

136136
impl Regex {
137137
pub(crate) fn parse(self) -> crate::error::Result<crate::Regex> {
138-
crate::Regex::new(self.body.pattern, self.body.options)
138+
crate::Regex::from_strings(self.body.pattern, self.body.options)
139139
}
140140
}
141141

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
@@ -1,7 +1,6 @@
11
use serde::{ser::Impossible, Serialize};
22

33
use crate::{
4-
raw::write_cstring,
54
ser::{Error, Result},
65
serialize_to_bson,
76
Bson,
@@ -266,7 +265,8 @@ impl serde::Serializer for KeySerializer<'_> {
266265

267266
#[inline]
268267
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
269-
Ok(write_cstring(&mut self.root_serializer.bytes, v)?)
268+
crate::raw::CStr::from_str(v)?.append_to(&mut self.root_serializer.bytes);
269+
Ok(())
270270
}
271271

272272
#[inline]

0 commit comments

Comments
 (0)