Skip to content

Commit

Permalink
Code Quality, Tests, Documentation (#9)
Browse files Browse the repository at this point in the history
* Refactoring of most of the internal structs and functions
  • Loading branch information
Srylax authored May 7, 2023
1 parent 4a7b740 commit aa4d857
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 313 deletions.
12 changes: 6 additions & 6 deletions examples/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn main() {
.expect("Unable to insert data");

// query page 1, 2 at a time
let mut options = create_options(2, 0);
let mut options = create_options(2, 0, doc! { "name": 1 });
let mut find_results: FindResult<MyFruit> = PaginatedCursor::new(Some(options), None, None)
.find(&db.collection("myfruits"), None)
.await
Expand All @@ -44,7 +44,7 @@ async fn main() {
print_details("First page", &find_results);

// get the second page
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
let mut cursor = find_results.page_info.next_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Next))
.find(&db.collection("myfruits"), None)
Expand All @@ -57,7 +57,7 @@ async fn main() {
print_details("Second page", &find_results);

// get previous page
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
cursor = find_results.page_info.start_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Previous))
.find(&db.collection("myfruits"), None)
Expand All @@ -70,7 +70,7 @@ async fn main() {
print_details("Previous page", &find_results);

// with a skip
options = create_options(2, 3);
options = create_options(2, 3, doc! { "name": 1 });
find_results = PaginatedCursor::new(Some(options), None, None)
.find(&db.collection("myfruits"), None)
.await
Expand All @@ -85,7 +85,7 @@ async fn main() {
);

// backwards from skipping
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
cursor = find_results.page_info.start_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Previous))
.find(&db.collection("myfruits"), None)
Expand All @@ -98,7 +98,7 @@ async fn main() {
print_details("Previous from skip", &find_results);

// backwards one more time and we are all the way back
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
cursor = find_results.page_info.start_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Previous))
.find(&db.collection("myfruits"), None)
Expand Down
6 changes: 3 additions & 3 deletions examples/helper/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bson::doc;
use bson::{doc, Document};
use mongodb::options::FindOptions;
use mongodb_cursor_pagination::FindResult;
use serde::Deserialize;
Expand All @@ -20,11 +20,11 @@ impl MyFruit {
}
}

pub fn create_options(limit: i64, skip: u64) -> FindOptions {
pub fn create_options(limit: i64, skip: u64, sort: Document) -> FindOptions {
FindOptions::builder()
.limit(limit)
.skip(skip)
.sort(doc! { "name": 1 })
.sort(sort)
.build()
}

Expand Down
32 changes: 16 additions & 16 deletions examples/multisort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ async fn main() {
.expect("Unable to insert data");

// query page 1, 2 at a time
let mut options = create_options(3, 0);
let mut options = create_options(3, 0, doc! { "how_many": 1, "name": -1, "non_existent": 1 });
let mut find_results: FindResult<MyFruit> = PaginatedCursor::new(Some(options), None, None)
.find(&db.collection("myfruits"), None)
.await
.expect("Unable to find data");
assert_eq!(
find_results.items,
vec![
MyFruit::new("Apple", 5),
MyFruit::new("Orange", 3),
MyFruit::new("Avocado", 5),
MyFruit::new("Bananas", 10)
MyFruit::new("Apple", 5)
]
);
print_details("First page", &find_results);

// get the second page
options = create_options(3, 0);
options = create_options(3, 0, doc! { "how_many": 1, "name": -1, "non_existent": 1 });
let mut cursor = find_results.page_info.next_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Next))
.find(&db.collection("myfruits"), None)
Expand All @@ -68,15 +68,15 @@ async fn main() {
assert_eq!(
find_results.items,
vec![
MyFruit::new("Blackberry", 12),
MyFruit::new("Blueberry", 10),
MyFruit::new("Bananas", 10),
MyFruit::new("Grapes", 12)
]
);
print_details("Second page", &find_results);

// get previous page
options = create_options(3, 0);
options = create_options(3, 0, doc! { "how_many": 1, "name": -1, "non_existent": 1 });
cursor = find_results.page_info.start_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Previous))
.find(&db.collection("myfruits"), None)
Expand All @@ -85,25 +85,25 @@ async fn main() {
assert_eq!(
find_results.items,
vec![
MyFruit::new("Apple", 5),
MyFruit::new("Orange", 3),
MyFruit::new("Avocado", 5),
MyFruit::new("Bananas", 10)
MyFruit::new("Apple", 5)
]
);
print_details("Previous page", &find_results);

// with a skip
options = create_options(3, 4);
options = create_options(3, 4, doc! { "how_many": 1, "name": -1, "non_existent": 1 });
find_results = PaginatedCursor::new(Some(options), None, None)
.find(&db.collection("myfruits"), None)
.await
.expect("Unable to find data");
assert_eq!(
find_results.items,
vec![
MyFruit::new("Blueberry", 10),
MyFruit::new("Bananas", 10),
MyFruit::new("Grapes", 12),
MyFruit::new("Orange", 3)
MyFruit::new("Blackberry", 12)
]
);
print_details(
Expand All @@ -112,7 +112,7 @@ async fn main() {
);

// backwards from skipping
options = create_options(3, 0);
options = create_options(3, 0, doc! { "how_many": 1, "name": -1, "non_existent": 1 });
cursor = find_results.page_info.start_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Previous))
.find(&db.collection("myfruits"), None)
Expand All @@ -122,20 +122,20 @@ async fn main() {
find_results.items,
vec![
MyFruit::new("Avocado", 5),
MyFruit::new("Bananas", 10),
MyFruit::new("Blackberry", 12),
MyFruit::new("Apple", 5),
MyFruit::new("Blueberry", 10),
]
);
print_details("Previous from skip", &find_results);

// backwards one more time and we are all the way back
options = create_options(3, 0);
options = create_options(3, 0, doc! { "how_many": 1, "name": -1, "non_existent": 1 });
cursor = find_results.page_info.start_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Previous))
.find(&db.collection("myfruits"), None)
.await
.expect("Unable to find data");
assert_eq!(find_results.items, vec![MyFruit::new("Apple", 5),]);
assert_eq!(find_results.items, vec![MyFruit::new("Orange", 3),]);
print_details(
"Previous again - at beginning, but cursor was for before Avocado (so only Apple)",
&find_results,
Expand Down
12 changes: 6 additions & 6 deletions examples/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn main() {
.expect("Unable to insert data");

// query page 1, 2 at a time
let mut options = create_options(2, 0);
let mut options = create_options(2, 0, doc! { "name": 1 });
let filter = doc! { "$or": [
{ "name": Bson::RegularExpression(Regex { pattern: String::from("berry"), options: String::from("i") })},
{ "spanish": Bson::RegularExpression(Regex { pattern: String::from("ana"), options: String::from("i") })},
Expand All @@ -63,7 +63,7 @@ async fn main() {
print_details("First page", &find_results);

// get the second page
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
let mut cursor = find_results.page_info.next_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Next))
.find(&db.collection("myfruits"), Some(&filter))
Expand All @@ -79,7 +79,7 @@ async fn main() {
print_details("Second page", &find_results);

// get previous page
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
cursor = find_results.page_info.start_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Previous))
.find(&db.collection("myfruits"), Some(&filter))
Expand All @@ -92,7 +92,7 @@ async fn main() {
print_details("Previous (first) page", &find_results);

// get the second page again
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
cursor = find_results.page_info.next_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Next))
.find(&db.collection("myfruits"), Some(&filter))
Expand All @@ -108,7 +108,7 @@ async fn main() {
print_details("Second page (again)", &find_results);

// get the third page
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
cursor = find_results.page_info.next_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Next))
.find(&db.collection("myfruits"), Some(&filter))
Expand All @@ -121,7 +121,7 @@ async fn main() {
print_details("Third page", &find_results);

// get previous page
options = create_options(2, 0);
options = create_options(2, 0, doc! { "name": 1 });
cursor = find_results.page_info.start_cursor;
find_results = PaginatedCursor::new(Some(options), cursor, Some(CursorDirections::Previous))
.find(&db.collection("myfruits"), Some(&filter))
Expand Down
34 changes: 17 additions & 17 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ pub enum CursorError {
}

impl From<io::Error> for CursorError {
fn from(err: io::Error) -> CursorError {
CursorError::IoError(err)
fn from(err: io::Error) -> Self {
Self::IoError(err)
}
}

impl From<Utf8Error> for CursorError {
fn from(err: Utf8Error) -> CursorError {
CursorError::InvalidCursor(err.to_string())
fn from(err: Utf8Error) -> Self {
Self::InvalidCursor(err.to_string())
}
}

impl From<base64::DecodeError> for CursorError {
fn from(err: base64::DecodeError) -> CursorError {
CursorError::InvalidCursor(err.to_string())
fn from(err: base64::DecodeError) -> Self {
Self::InvalidCursor(err.to_string())
}
}

impl fmt::Display for CursorError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
CursorError::IoError(ref inner) => inner.fmt(fmt),
CursorError::InvalidCursor(ref cursor) => {
write!(fmt, "Invalid cursor - unable to parse: {:?}", cursor)
Self::IoError(ref inner) => inner.fmt(fmt),
Self::InvalidCursor(ref cursor) => {
write!(fmt, "Invalid cursor - unable to parse: {cursor:?}")
}
CursorError::Unknown(ref inner) => inner.fmt(fmt),
CursorError::InvalidId(ref id) => write!(fmt, "Invalid id - {:?}", id),
Self::Unknown(ref inner) => inner.fmt(fmt),
Self::InvalidId(ref id) => write!(fmt, "Invalid id - {id:?}"),
}
}
}
Expand All @@ -44,16 +44,16 @@ impl fmt::Display for CursorError {
impl error::Error for CursorError {
fn description(&self) -> &str {
match *self {
CursorError::IoError(ref inner) => inner.description(),
CursorError::InvalidCursor(_) => "Invalid cursor value",
CursorError::Unknown(ref inner) => inner,
CursorError::InvalidId(_) => "Invalid mongodbid",
Self::IoError(ref inner) => inner.description(),
Self::InvalidCursor(_) => "Invalid cursor value",
Self::Unknown(ref inner) => inner,
Self::InvalidId(_) => "Invalid mongodbid",
}
}

fn cause(&self) -> Option<&dyn error::Error> {
match *self {
CursorError::IoError(ref inner) => Some(inner),
Self::IoError(ref inner) => Some(inner),
_ => None,
}
}
Expand Down
Loading

0 comments on commit aa4d857

Please sign in to comment.