Skip to content

RUST-1891 Implement Default on results structs #1289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/action/insert_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ impl<'a> Action for InsertMany<'a> {
Ok(result) => {
let current_batch_size = result.inserted_ids.len();

let cumulative_result =
cumulative_result.get_or_insert_with(InsertManyResult::new);
let cumulative_result = cumulative_result.get_or_insert_with(Default::default);
for (index, id) in result.inserted_ids {
cumulative_result
.inserted_ids
Expand Down Expand Up @@ -184,7 +183,7 @@ impl<'a> Action for InsertMany<'a> {
ErrorKind::InsertMany(failure),
Some(error_labels),
)),
None => Ok(cumulative_result.unwrap_or_else(InsertManyResult::new)),
None => Ok(cumulative_result.unwrap_or_default()),
}
}
}
29 changes: 11 additions & 18 deletions src/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use bulk_write::*;

/// The result of a [`Collection::insert_one`](../struct.Collection.html#method.insert_one)
/// operation.
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InsertOneResult {
Expand All @@ -37,26 +37,18 @@ impl InsertOneResult {

/// The result of a [`Collection::insert_many`](../struct.Collection.html#method.insert_many)
/// operation.
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InsertManyResult {
/// The `_id` field of the documents inserted.
pub inserted_ids: HashMap<usize, Bson>,
}

impl InsertManyResult {
pub(crate) fn new() -> Self {
InsertManyResult {
inserted_ids: HashMap::new(),
}
}
}

/// The result of a [`Collection::update_one`](../struct.Collection.html#method.update_one) or
/// [`Collection::update_many`](../struct.Collection.html#method.update_many) operation.
#[skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct UpdateResult {
Expand All @@ -74,7 +66,7 @@ pub struct UpdateResult {

/// The result of a [`Collection::delete_one`](../struct.Collection.html#method.delete_one) or
/// [`Collection::delete_many`](../struct.Collection.html#method.delete_many) operation.
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DeleteResult {
Expand All @@ -85,7 +77,7 @@ pub struct DeleteResult {

/// Information about the index created as a result of a
/// [`Collection::create_index`](../struct.Collection.html#method.create_index).
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct CreateIndexResult {
/// The name of the index created in the `createIndex` command.
Expand All @@ -94,7 +86,7 @@ pub struct CreateIndexResult {

/// Information about the indexes created as a result of a
/// [`Collection::create_indexes`](../struct.Collection.html#method.create_indexes).
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct CreateIndexesResult {
/// The list containing the names of all indexes created in the `createIndexes` command.
Expand All @@ -120,14 +112,15 @@ pub(crate) struct GetMoreResult {

/// Describes the type of data store returned when executing
/// [`Database::list_collections`](../struct.Database.html#method.list_collections).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum CollectionType {
/// Indicates that the data store is a view.
View,

/// Indicates that the data store is a collection.
#[default]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this felt like a reasonable default; don't think the choice here is that critical, though, since these impls are for mocking

Collection,

/// Indicates that the data store is a timeseries.
Expand All @@ -140,7 +133,7 @@ pub enum CollectionType {
///
/// See the MongoDB [manual](https://www.mongodb.com/docs/manual/reference/command/listCollections/#listCollections.cursor)
/// for more information.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CollectionSpecificationInfo {
Expand All @@ -155,7 +148,7 @@ pub struct CollectionSpecificationInfo {

/// Information about a collection as reported by
/// [`Database::list_collections`](../struct.Database.html#method.list_collections).
#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CollectionSpecification {
Expand All @@ -179,7 +172,7 @@ pub struct CollectionSpecification {

/// A struct modeling the information about an individual database returned from
/// [`Client::list_databases`](../struct.Client.html#method.list_databases).
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DatabaseSpecification {
Expand Down