Skip to content
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
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 2 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
doc-valid-idents = ["MongoDB", "MongODM"]
too-many-lines-threshold = 130
75 changes: 36 additions & 39 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
}
}
}
Expand Down Expand Up @@ -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<Cow<'static, str>>) {
self.add_key_with_direction(key, SortOrder::Ascending)
self.add_key_with_direction(key, SortOrder::Ascending);
}

/// Builder style method for `add_key`.
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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::<mongodb::error::CommandError>(Bson::Document(ret.clone()))
{
Err(mongodb::error::Error::from(
mongodb::error::ErrorKind::Command(err),
))
} else {
Ok(ret)
}
deserialize_from_bson::<mongodb::error::CommandError>(Bson::Document(ret.clone())).map_or_else(
|_| Ok(ret),
|err| {
Err(mongodb::error::Error::from(
mongodb::error::ErrorKind::Command(err),
))
},
)
}

#[derive(Deserialize)]
Expand Down
11 changes: 5 additions & 6 deletions src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,11 +73,10 @@ impl<M: Model> Clone for Repository<M> {
impl<M: Model> Repository<M> {
/// 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 }
}
Expand Down
Loading