@@ -31,7 +31,7 @@ use std::{
3131use serde_json:: { json, Value } ;
3232
3333pub use crate :: document:: Document ;
34- use crate :: { base64, oid, spec:: ElementType , Binary , Decimal128 } ;
34+ use crate :: { base64, oid, raw :: CString , spec:: ElementType , Binary , Decimal128 } ;
3535
3636/// Possible BSON value types.
3737#[ derive( Clone , Default , PartialEq ) ]
@@ -268,6 +268,12 @@ impl From<String> for Bson {
268268 }
269269}
270270
271+ impl From < crate :: raw:: CString > for Bson {
272+ fn from ( a : crate :: raw:: CString ) -> Bson {
273+ Bson :: String ( a. into_string ( ) )
274+ }
275+ }
276+
271277impl From < Document > for Bson {
272278 fn from ( a : Document ) -> Bson {
273279 Bson :: Document ( a)
@@ -480,14 +486,14 @@ impl Bson {
480486 Bson :: Boolean ( v) => json ! ( v) ,
481487 Bson :: Null => Value :: Null ,
482488 Bson :: RegularExpression ( Regex { pattern, options } ) => {
483- let mut chars: Vec < _ > = options. chars ( ) . collect ( ) ;
489+ let mut chars: Vec < _ > = options. as_str ( ) . chars ( ) . collect ( ) ;
484490 chars. sort_unstable ( ) ;
485491
486492 let options: String = chars. into_iter ( ) . collect ( ) ;
487493
488494 json ! ( {
489495 "$regularExpression" : {
490- "pattern" : pattern,
496+ "pattern" : pattern. into_string ( ) ,
491497 "options" : options,
492498 }
493499 } )
@@ -619,7 +625,7 @@ impl Bson {
619625 ref pattern,
620626 ref options,
621627 } ) => {
622- let mut chars: Vec < _ > = options. chars ( ) . collect ( ) ;
628+ let mut chars: Vec < _ > = options. as_str ( ) . chars ( ) . collect ( ) ;
623629 chars. sort_unstable ( ) ;
624630
625631 let options: String = chars. into_iter ( ) . collect ( ) ;
@@ -842,7 +848,9 @@ impl Bson {
842848 if let Ok ( regex) = doc. get_document ( "$regularExpression" ) {
843849 if let Ok ( pattern) = regex. get_str ( "pattern" ) {
844850 if let Ok ( options) = regex. get_str ( "options" ) {
845- return Bson :: RegularExpression ( Regex :: new ( pattern, options) ) ;
851+ if let Ok ( regex) = Regex :: from_strings ( pattern, options) {
852+ return Bson :: RegularExpression ( regex) ;
853+ }
846854 }
847855 }
848856 }
@@ -1147,7 +1155,7 @@ impl Timestamp {
11471155#[ derive( Debug , Clone , Eq , PartialEq , Hash ) ]
11481156pub struct Regex {
11491157 /// The regex pattern to match.
1150- pub pattern : String ,
1158+ pub pattern : CString ,
11511159
11521160 /// The options for the regex.
11531161 ///
@@ -1156,18 +1164,22 @@ pub struct Regex {
11561164 /// multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent,
11571165 /// 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match
11581166 /// unicode.
1159- pub options : String ,
1167+ pub options : CString ,
11601168}
11611169
11621170impl Regex {
1163- pub ( crate ) fn new ( pattern : impl AsRef < str > , options : impl AsRef < str > ) -> Self {
1171+ #[ cfg( any( test, feature = "serde" ) ) ]
1172+ pub ( crate ) fn from_strings (
1173+ pattern : impl AsRef < str > ,
1174+ options : impl AsRef < str > ,
1175+ ) -> crate :: error:: Result < Self > {
11641176 let mut chars: Vec < _ > = options. as_ref ( ) . chars ( ) . collect ( ) ;
11651177 chars. sort_unstable ( ) ;
11661178 let options: String = chars. into_iter ( ) . collect ( ) ;
1167- Self {
1168- pattern : pattern. as_ref ( ) . to_string ( ) ,
1169- options,
1170- }
1179+ Ok ( Self {
1180+ pattern : pattern. as_ref ( ) . to_string ( ) . try_into ( ) ? ,
1181+ options : options . try_into ( ) ? ,
1182+ } )
11711183 }
11721184}
11731185
0 commit comments