Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SearchOptions to work like a more usual builder #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions examples/all-basic-lands.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
3 changes: 1 addition & 2 deletions examples/top-printings/main.rs → examples/top-printings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
8 changes: 2 additions & 6 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
29 changes: 19 additions & 10 deletions src/search/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -68,56 +68,65 @@ impl SearchOptions {
}

/// Sets the query to use for this search.
pub fn query(&mut self, query: Query) -> &mut Self {
#[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.
pub fn page(&mut self, page: usize) -> &mut Self {
#[must_use]
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 {
#[must_use]
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 {
#[must_use]
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 {
#[must_use]
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 {
#[must_use]
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 {
#[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.
pub fn multilingual(&mut self, include_multilingual: bool) -> &mut Self {
#[must_use]
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 {
#[must_use]
pub fn variations(mut self, include_variations: bool) -> Self {
self.include_variations = include_variations;
self
}
Expand Down
Loading