From b343290c5cba0f27f7aed567212bca797a03ccc1 Mon Sep 17 00:00:00 2001 From: Maximilian Remming Date: Fri, 24 May 2024 22:46:32 +0300 Subject: [PATCH 1/4] Chaneg `SearchOptions` to work like a more usual builder --- examples/top-printings/main.rs | 3 +-- src/search/advanced.rs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/examples/top-printings/main.rs b/examples/top-printings/main.rs index 4e71a11..939d1d0 100644 --- a/examples/top-printings/main.rs +++ b/examples/top-printings/main.rs @@ -8,8 +8,7 @@ use scryfall::Card; async fn main() -> scryfall::Result<()> { let card_name = std::env::args().nth(1).expect("expected a card name param"); - let mut search_options = SearchOptions::new(); - search_options + let search_options = SearchOptions::new() .unique(UniqueStrategy::Prints) .sort(SortOrder::Usd, SortDirection::Descending) .query(exact(card_name).and(in_game(Game::Paper))); diff --git a/src/search/advanced.rs b/src/search/advanced.rs index acd9445..02fb141 100644 --- a/src/search/advanced.rs +++ b/src/search/advanced.rs @@ -12,7 +12,7 @@ use crate::search::Search; /// documentation on each option, refer to this struct's methods. /// /// For more information, refer to the [official docs](https://scryfall.com/docs/api/cards/search). -#[derive(Serialize, Default, Debug)] +#[derive(Serialize, Default, Debug, Clone, PartialEq)] pub struct SearchOptions { #[serde(skip_serializing_if = "is_default")] unique: UniqueStrategy, @@ -68,56 +68,56 @@ impl SearchOptions { } /// Sets the query to use for this search. - pub fn query(&mut self, query: Query) -> &mut Self { + pub fn query(mut self, query: Query) -> Self { self.query = query; self } /// Sets the page number to start with. Page 0 is equivalent to page 1. - pub fn page(&mut self, page: usize) -> &mut Self { + pub fn page(mut self, page: usize) -> Self { self.page = page; self } /// Sets the strategy for omitting similar cards. - pub fn unique(&mut self, unique: UniqueStrategy) -> &mut Self { + pub fn unique(mut self, unique: UniqueStrategy) -> Self { self.unique = unique; self } /// Sets the sort order and direction for returned cards. #[inline] - pub fn sort(&mut self, order: SortOrder, dir: SortDirection) -> &mut Self { + pub fn sort(mut self, order: SortOrder, dir: SortDirection) -> Self { self.order(order).direction(dir) } /// Sets the sort order for returned cards. - pub fn order(&mut self, order: SortOrder) -> &mut Self { + pub fn order(mut self, order: SortOrder) -> Self { self.order = order; self } /// Sets the sort direction for returned cards. - pub fn direction(&mut self, dir: SortDirection) -> &mut Self { + pub fn direction(mut self, dir: SortDirection) -> Self { self.dir = dir; self } /// If true, extra cards (tokens, planes, etc) will be included. - pub fn extras(&mut self, include_extras: bool) -> &mut Self { + pub fn extras(mut self, include_extras: bool) -> Self { self.include_extras = include_extras; self } /// If true, cards in every language supported by Scryfall will be included. - pub fn multilingual(&mut self, include_multilingual: bool) -> &mut Self { + pub fn multilingual(mut self, include_multilingual: bool) -> Self { self.include_multilingual = include_multilingual; self } /// If true, rare care variants will be included, like the /// [Hairy Runesword](https://scryfall.com/card/drk/107%E2%80%A0/runesword). - pub fn variations(&mut self, include_variations: bool) -> &mut Self { + pub fn variations(mut self, include_variations: bool) -> Self { self.include_variations = include_variations; self } From b1995009f47053483d2b3c82aca55ac4d1f9e310 Mon Sep 17 00:00:00 2001 From: Maximilian Remming Date: Fri, 24 May 2024 22:56:07 +0300 Subject: [PATCH 2/4] Add an additional example --- examples/all-basic-lands.rs | 24 +++++++++++++++++++ .../main.rs => top-printings.rs} | 0 2 files changed, 24 insertions(+) create mode 100644 examples/all-basic-lands.rs rename examples/{top-printings/main.rs => top-printings.rs} (100%) diff --git a/examples/all-basic-lands.rs b/examples/all-basic-lands.rs new file mode 100644 index 0000000..2a69ccb --- /dev/null +++ b/examples/all-basic-lands.rs @@ -0,0 +1,24 @@ +use futures::StreamExt; +use scryfall::{ + search::advanced::{SearchOptions, UniqueStrategy}, + search::prelude::*, + Card, +}; + +#[tokio::main] +async fn main() -> scryfall::Result<()> { + let opts = SearchOptions::new() + .query(type_line("Basic").and(type_line("Land"))) + .unique(UniqueStrategy::Prints) + .extras(true) + .variations(true) + .multilingual(true); + + let mut cards = Card::search(opts).await?.into_stream(); + while let Some(card) = cards.next().await { + let card = card.expect("card should be deserialized succesfully"); + println!("{} - {}", card.id, card.printed_name.unwrap_or(card.name)); + } + + Ok(()) +} diff --git a/examples/top-printings/main.rs b/examples/top-printings.rs similarity index 100% rename from examples/top-printings/main.rs rename to examples/top-printings.rs From 11752232f17f761d911555c94527a70f1507036a Mon Sep 17 00:00:00 2001 From: Maximilian Remming Date: Fri, 24 May 2024 23:00:48 +0300 Subject: [PATCH 3/4] Add `must_use` to all builder methods in `SearchOptions` --- src/search/advanced.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/search/advanced.rs b/src/search/advanced.rs index 02fb141..422abd9 100644 --- a/src/search/advanced.rs +++ b/src/search/advanced.rs @@ -68,18 +68,21 @@ impl SearchOptions { } /// Sets the query to use for this search. + #[must_use] pub fn query(mut self, query: Query) -> Self { self.query = query; self } /// Sets the page number to start with. Page 0 is equivalent to page 1. + #[must_use] pub fn page(mut self, page: usize) -> Self { self.page = page; self } /// Sets the strategy for omitting similar cards. + #[must_use] pub fn unique(mut self, unique: UniqueStrategy) -> Self { self.unique = unique; self @@ -87,29 +90,34 @@ impl SearchOptions { /// Sets the sort order and direction for returned cards. #[inline] + #[must_use] pub fn sort(mut self, order: SortOrder, dir: SortDirection) -> Self { self.order(order).direction(dir) } /// Sets the sort order for returned cards. + #[must_use] pub fn order(mut self, order: SortOrder) -> Self { self.order = order; self } /// Sets the sort direction for returned cards. + #[must_use] pub fn direction(mut self, dir: SortDirection) -> Self { self.dir = dir; self } /// If true, extra cards (tokens, planes, etc) will be included. + #[must_use] pub fn extras(mut self, include_extras: bool) -> Self { self.include_extras = include_extras; self } /// If true, cards in every language supported by Scryfall will be included. + #[must_use] pub fn multilingual(mut self, include_multilingual: bool) -> Self { self.include_multilingual = include_multilingual; self @@ -117,6 +125,7 @@ impl SearchOptions { /// If true, rare care variants will be included, like the /// [Hairy Runesword](https://scryfall.com/card/drk/107%E2%80%A0/runesword). + #[must_use] pub fn variations(mut self, include_variations: bool) -> Self { self.include_variations = include_variations; self From afd9f0807c64ea1aa820710a6bde1c86335eb0fb Mon Sep 17 00:00:00 2001 From: Maximilian Remming Date: Fri, 24 May 2024 23:29:47 +0300 Subject: [PATCH 4/4] Fix tests --- src/search.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/search.rs b/src/search.rs index 368640a..b8f5945 100644 --- a/src/search.rs +++ b/src/search.rs @@ -215,9 +215,7 @@ mod tests { #[test] fn finds_alpha_lotus() { - let mut search = SearchOptions::new(); - - search + let search = SearchOptions::new() .query(exact("Black Lotus")) .unique(UniqueStrategy::Prints) .sort(SortOrder::Released, SortDirection::Ascending); @@ -244,9 +242,7 @@ mod tests { #[test] fn finds_alpha_lotus_buffered() { - let mut search = SearchOptions::new(); - - search + let search = SearchOptions::new() .query(exact("Black Lotus")) .unique(UniqueStrategy::Prints) .sort(SortOrder::Released, SortDirection::Ascending);