diff --git a/Cargo.toml b/Cargo.toml index fb67f3a..8e7c9d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,10 @@ async-trait = "0.1" tokio = "1.14.0" pretty_assertions = "1.0.0" chrono = { version = "0.4.38", features = [ "serde" ] } + +[lints.clippy] +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 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 } }