-
Notifications
You must be signed in to change notification settings - Fork 126
Added a builder like struct for the search function (Related with issue ) #364
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
Open
GartoxFR
wants to merge
9
commits into
ramsayleung:master
Choose a base branch
from
GartoxFR:type-safe-search
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
82878ed
Added a builder like struct for the search function
GartoxFR 7995a07
New type safe system with traits
GartoxFR 320d0f5
Revert "New type safe system with traits"
GartoxFR 5b5e60d
Fixed comments :
GartoxFR 87c9c2d
SearchQuery :
GartoxFR 92ae1a4
Removed useless example
GartoxFR 6044d8f
Fix english mistakes
GartoxFR 04e4f8a
Changed "any" query to append when used multiple times
GartoxFR 3462c77
Fix missing import in doc
GartoxFR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| use std::collections::BTreeMap; | ||
|
|
||
| use rspotify_model::SearchFilter; | ||
|
|
||
| /// Builder used to create search query. | ||
| /// | ||
| /// Note that when calling the same function multiple times, the filter will be the text from the | ||
| /// last call | ||
| /// | ||
| /// This is converted to the query string using into() | ||
| /// | ||
| /// Exemple | ||
| /// ```rust | ||
| /// use rspotify::search::SearchQuery; | ||
| /// | ||
| /// SearchQuery::default() | ||
| /// .any("foo") | ||
| /// .album("bar") | ||
| /// // Filter on album containing "bar" and anything containing "foo" | ||
| /// ``` | ||
| /// | ||
| /// For more information on the different filters, look at the [Soptify | ||
| /// documentation](https://developer.spotify.com/documentation/web-api/reference/#/operations/search) | ||
| #[derive(Debug, Default)] | ||
| pub struct SearchQuery<'a> { | ||
| no_filter_query: String, | ||
| query_map: BTreeMap<SearchFilter, &'a str>, | ||
| } | ||
|
|
||
| impl<'a> SearchQuery<'a> { | ||
| /// Basic filter where the given string can be anything | ||
| pub fn any(&mut self, str: &'a str) -> &mut Self { | ||
| self.no_filter_query.push_str(str); | ||
| self.no_filter_query.push(' '); | ||
| self | ||
| } | ||
|
|
||
| pub fn album(&mut self, str: &'a str) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Album, str); | ||
| self | ||
| } | ||
|
|
||
| pub fn artist(&mut self, str: &'a str) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Artist, str); | ||
| self | ||
| } | ||
|
|
||
| pub fn track(&mut self, str: &'a str) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Track, str); | ||
| self | ||
| } | ||
|
|
||
| pub fn year(&mut self, str: &'a str) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Year, str); | ||
| self | ||
| } | ||
|
|
||
| pub fn upc(&mut self, str: &'a str) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Upc, str); | ||
| self | ||
| } | ||
|
|
||
| pub fn tag_new(&mut self) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::TagNew, ""); | ||
| self | ||
| } | ||
|
|
||
| pub fn tag_hipster(&mut self) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::TagHipster, ""); | ||
| self | ||
| } | ||
|
|
||
| pub fn isrc(&mut self, str: &'a str) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Isrc, str); | ||
| self | ||
| } | ||
|
|
||
| pub fn genre(&mut self, str: &'a str) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Genre, str); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl From<&SearchQuery<'_>> for String { | ||
| fn from(val: &SearchQuery) -> Self { | ||
| let mut rep = val.no_filter_query.clone(); | ||
|
|
||
| if val.query_map.is_empty() { | ||
| return rep; | ||
| } | ||
|
|
||
| rep.push_str( | ||
| val.query_map | ||
| .iter() | ||
| .map(|entry| match entry.0 { | ||
| SearchFilter::TagNew | SearchFilter::TagHipster => format!("{} ", entry.0), | ||
| _ => format!("{}:{} ", entry.0, entry.1), | ||
| }) | ||
| .collect::<String>() | ||
| .trim(), | ||
| ); | ||
|
|
||
| rep | ||
| } | ||
| } | ||
|
|
||
| impl From<&mut SearchQuery<'_>> for String { | ||
| fn from(val: &mut SearchQuery) -> Self { | ||
| String::from(&(*val)) | ||
| } | ||
| } | ||
|
|
||
| impl From<SearchQuery<'_>> for String { | ||
| fn from(val: SearchQuery) -> Self { | ||
| String::from(&val) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::SearchQuery; | ||
|
|
||
| #[test] | ||
| fn test_search_query() { | ||
| let query: String = SearchQuery::default() | ||
| .any("foo") | ||
| .any("bar") | ||
| .album("wrong album") | ||
| .album("arrival") | ||
| .artist("abba") | ||
| .tag_new() | ||
| .tag_hipster() | ||
| .track("foo") | ||
| .year("2020") | ||
| .upc("bar") | ||
| .isrc("foo") | ||
| .genre("metal") | ||
| .into(); | ||
|
|
||
| let expected = "foo bar album:arrival artist:abba track:foo year:2020 upc:bar \ | ||
| tag:hipster tag:new isrc:foo genre:metal"; | ||
|
|
||
| assert_eq!(expected, query); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_empty_query() { | ||
| let query: String = SearchQuery::default().into(); | ||
|
|
||
| assert_eq!(query, ""); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.