From e6dee5707fb938c49bd73821f29edd477b72c203 Mon Sep 17 00:00:00 2001 From: Allan Zhang <6740989+allan2@users.noreply.github.com> Date: Mon, 15 Dec 2025 09:38:43 -0500 Subject: [PATCH 1/2] chore: fix pedantic lints This applies fixes for a bunch of pedantic lints. A clippy configuration is added for `too_many_lines`. --- Cargo.toml | 5 ++++ clippy.toml | 2 ++ src/index.rs | 75 +++++++++++++++++++++++------------------------ src/repository.rs | 11 ++++--- 4 files changed, 48 insertions(+), 45 deletions(-) create mode 100644 clippy.toml diff --git a/Cargo.toml b/Cargo.toml index fb67f3a..302d8e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,8 @@ async-trait = "0.1" tokio = "1.14.0" pretty_assertions = "1.0.0" chrono = { version = "0.4.38", features = [ "serde" ] } + +[lints.clippy] +must_use_candidate = { level = "allow" } +missing_errors_doc = { level = "allow" } +return_self_not_must_use = { level = "allow" } \ No newline at end of file diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..29a17eb --- /dev/null +++ b/clippy.toml @@ -0,0 +1,2 @@ +doc-valid-idents = ["MongoDB", "MongODM"] +too-many-lines-threshold = 130 \ No newline at end of file diff --git a/src/index.rs b/src/index.rs index 602b842..5d3645b 100644 --- a/src/index.rs +++ b/src/index.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; /// Index sort order (useful for compound indexes). /// /// [Mongo manual](https://docs.mongodb.com/manual/core/index-compound/#sort-order) -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SortOrder { Ascending, Descending, @@ -41,26 +41,26 @@ enum IndexKey { impl IndexKey { fn get_key_name(&self) -> String { match self { - IndexKey::SortIndex(s) => match s.direction { + Self::SortIndex(s) => match s.direction { SortOrder::Ascending => format!("{}_1", s.name), SortOrder::Descending => format!("{}_-1", s.name), }, - IndexKey::TextIndex(t) => format!("{}_text", t.name), + Self::TextIndex(t) => format!("{}_text", t.name), } } fn get_name(&self) -> String { match self { - IndexKey::SortIndex(s) => s.name.to_string(), - IndexKey::TextIndex(t) => t.name.to_string(), + Self::SortIndex(s) => s.name.to_string(), + Self::TextIndex(t) => t.name.to_string(), } } fn get_value(&self) -> Bson { match self { - IndexKey::SortIndex(s) => s.direction.into(), - IndexKey::TextIndex(_) => "text".into(), + Self::SortIndex(s) => s.direction.into(), + Self::TextIndex(_) => "text".into(), } } } @@ -135,7 +135,7 @@ impl Index { /// /// [Mongo manual](https://docs.mongodb.com/manual/core/index-compound/). pub fn add_key(&mut self, key: impl Into>) { - self.add_key_with_direction(key, SortOrder::Ascending) + self.add_key_with_direction(key, SortOrder::Ascending); } /// Builder style method for `add_key`. @@ -297,37 +297,35 @@ pub enum IndexOption { impl IndexOption { pub fn name(&self) -> &str { match self { - IndexOption::Background => "background", - IndexOption::Unique => "unique", - IndexOption::Name(..) => "name", - IndexOption::PartialFilterExpression(..) => "partialFilterExpression", - IndexOption::Sparse => "sparse", - IndexOption::ExpireAfterSeconds(..) => "expireAfterSeconds", - IndexOption::StorageEngine(..) => "storageEngine", - IndexOption::Collation(..) => "collation", - IndexOption::Weights(..) => "weights", - IndexOption::Custom { name, .. } => name.as_str(), + Self::Background => "background", + Self::Unique => "unique", + Self::Name(..) => "name", + Self::PartialFilterExpression(..) => "partialFilterExpression", + Self::Sparse => "sparse", + Self::ExpireAfterSeconds(..) => "expireAfterSeconds", + Self::StorageEngine(..) => "storageEngine", + Self::Collation(..) => "collation", + Self::Weights(..) => "weights", + Self::Custom { name, .. } => name.as_str(), } } pub fn into_value(self) -> Bson { match self { - IndexOption::Background | IndexOption::Unique | IndexOption::Sparse => { - Bson::Boolean(true) - } - IndexOption::Name(val) => Bson::String(val), - IndexOption::ExpireAfterSeconds(val) => Bson::Int32(val), - IndexOption::PartialFilterExpression(doc) - | IndexOption::StorageEngine(doc) - | IndexOption::Collation(doc) => Bson::Document(doc), - IndexOption::Weights(w) => { + Self::Background | Self::Unique | Self::Sparse => Bson::Boolean(true), + Self::Name(val) => Bson::String(val), + Self::ExpireAfterSeconds(val) => Bson::Int32(val), + Self::PartialFilterExpression(doc) + | Self::StorageEngine(doc) + | Self::Collation(doc) => Bson::Document(doc), + Self::Weights(w) => { let mut doc = Document::new(); - w.into_iter().for_each(|(k, v)| { + for (k, v) in w { doc.insert(k, Bson::from(v)); - }); + } Bson::Document(doc) } - IndexOption::Custom { value, .. } => value, + Self::Custom { value, .. } => value, } } @@ -511,15 +509,14 @@ async fn h_run_command( .run_command(command_doc) .with_options(primary_options) .await?; - if let Ok(err) = - deserialize_from_bson::(Bson::Document(ret.clone())) - { - Err(mongodb::error::Error::from( - mongodb::error::ErrorKind::Command(err), - )) - } else { - Ok(ret) - } + deserialize_from_bson::(Bson::Document(ret.clone())).map_or_else( + |_| Ok(ret), + |err| { + Err(mongodb::error::Error::from( + mongodb::error::ErrorKind::Command(err), + )) + }, + ) } #[derive(Deserialize)] diff --git a/src/repository.rs b/src/repository.rs index 0be0ad9..c325fe0 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -11,7 +11,7 @@ use mongodb::bson::{deserialize_from_document, serialize_to_bson}; use mongodb::bson::{from_document as deserialize_from_document, to_bson as serialize_to_bson}; use mongodb::error::Result; -use mongodb::options::*; +use mongodb::options::{CollectionOptions, UpdateOptions}; use serde::Deserialize; use std::borrow::Borrow; use std::ops::Deref; @@ -73,11 +73,10 @@ impl Clone for Repository { impl Repository { /// Create a new repository from the given mongo client. pub fn new(db: mongodb::Database) -> Self { - let coll = if let Some(options) = M::CollConf::collection_options() { - db.collection_with_options(M::CollConf::collection_name(), options) - } else { - db.collection(M::CollConf::collection_name()) - }; + let coll = M::CollConf::collection_options().map_or_else( + || db.collection(M::CollConf::collection_name()), + |options| db.collection_with_options(M::CollConf::collection_name(), options), + ); Self { db, coll } } From 45a2e55c67e9ebbd9ddbbb17bb94d6a2e16a104a Mon Sep 17 00:00:00 2001 From: Allan Zhang <6740989+allan2@users.noreply.github.com> Date: Mon, 15 Dec 2025 09:48:53 -0500 Subject: [PATCH 2/2] Add doc_markdown --- Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 302d8e0..8e7c9d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,8 @@ pretty_assertions = "1.0.0" chrono = { version = "0.4.38", features = [ "serde" ] } [lints.clippy] -must_use_candidate = { level = "allow" } +doc_markdown = { level = "deny", priority = -1 } +missing_const_for_fn = { level = "allow" } missing_errors_doc = { level = "allow" } +must_use_candidate = { level = "allow" } return_self_not_must_use = { level = "allow" } \ No newline at end of file