@@ -104,7 +104,8 @@ pub struct CreateCollectionOptions {
104
104
/// Options for supporting change stream pre- and post-images.
105
105
pub change_stream_pre_and_post_images : Option < ChangeStreamPreAndPostImages > ,
106
106
107
- /// Options for clustered collections.
107
+ /// Options for clustered collections. This option is only available on server versions 5.3+.
108
+ #[ serde( default , deserialize_with = "ClusteredIndex::deserialize_self_or_true" ) ]
108
109
pub clustered_index : Option < ClusteredIndex > ,
109
110
110
111
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
@@ -176,6 +177,35 @@ impl Default for ClusteredIndex {
176
177
}
177
178
}
178
179
180
+ impl ClusteredIndex {
181
+ /// When creating a time-series collection on MongoDB Atlas the `clusteredIndex` field of the
182
+ /// collection options is given as `true` instead of as an object that deserializes to
183
+ /// `ClusteredIndex`. This custom deserializer handles that case by using the default value for
184
+ /// `ClusteredIndex`.
185
+ fn deserialize_self_or_true < ' de , D > ( deserializer : D ) -> Result < Option < ClusteredIndex > , D :: Error >
186
+ where
187
+ D : serde:: Deserializer < ' de > ,
188
+ {
189
+ #[ derive( Debug , Deserialize ) ]
190
+ #[ serde( untagged) ]
191
+ enum ValueUnion {
192
+ Bool ( bool ) ,
193
+ ClusteredIndex ( ClusteredIndex ) ,
194
+ }
195
+
196
+ let value_option: Option < ValueUnion > = Deserialize :: deserialize ( deserializer) ?;
197
+ value_option
198
+ . map ( |value| match value {
199
+ ValueUnion :: Bool ( true ) => Ok ( ClusteredIndex :: default ( ) ) ,
200
+ ValueUnion :: Bool ( false ) => Err ( serde:: de:: Error :: custom (
201
+ "if clusteredIndex is a boolean it must be `true`" ,
202
+ ) ) ,
203
+ ValueUnion :: ClusteredIndex ( value) => Ok ( value) ,
204
+ } )
205
+ . transpose ( )
206
+ }
207
+ }
208
+
179
209
/// Specifies default configuration for indexes created on a collection, including the _id index.
180
210
#[ derive( Clone , Debug , TypedBuilder , PartialEq , Serialize , Deserialize ) ]
181
211
#[ builder( field_defaults( default , setter( into) ) ) ]
0 commit comments